我解决了我的own question here。但是我的代码有一个巨大的缺陷:当我运行应用程序时, Shiny只显示复选框。在R-Studio中,它向我显示弹出窗口中的复选框和R-Studio-Viewer选项卡中的图表。
我尝试将这两个元素放在sidebarLayout
- 面板中。但无济于事。有没有办法让这项工作?
library(shiny)
library(plotly)
library(dplyr)
shinyApp(
ui = fluidPage(sidebarLayout(
sidebarPanel(checkboxGroupInput("Addtext", 'lines',c('trace 0','trace 1'),''),width = 2),
mainPanel(plotOutput('plot1'),width=9)
)
),
server = function(input, output) {
output$plot1 = renderPlot({
p<-plot_ly(x = c( -2, 0, 1.5 ),y = c( -2, 1, 2.2), type = 'scatter' ,mode = 'lines') %>%
add_trace(x=c(-1,0.4,2.5),y=c(2, 0, -1),type='scatter',mode='lines')
if(!is.null(input$Addtext)){
if('trace 0'%in%input$Addtext){
p<- p %>% add_trace(x=c( -2, 0, 1.5 ),y= c( -2, 1, 2.2),type='scatter',mode='text',
text=c('(-2,-2)','(0,1)','(1.5,2.2)'),
textposition='right',textfont = list(color = '#000000', size = 10),
hoverinfo='skip',showlegend=FALSE)
}
if('trace 1'%in%input$Addtext){
p<- p %>% add_trace(x=c( -1, 0.4, 2.5 ),y= c( 2, 0, -1),type='scatter',mode='text',
text=c('(-1,2)','(0.4,0)','(2.5,-1)'),
textposition='right',textfont = list(color = '#000000', size = 10),
hoverinfo='skip',showlegend=FALSE)
}
}
p
})
}
)
答案 0 :(得分:1)
使用plotlyOutput
包
renderPlotly
和plotly
library(shiny)
library(plotly)
library(dplyr)
shinyApp(
ui = fluidPage(sidebarLayout(
sidebarPanel(checkboxGroupInput("Addtext", 'lines',c('trace 0','trace 1'),''),width = 2),
mainPanel(plotlyOutput('plot1'),width=9)
)
),
server = function(input, output) {
output$plot1 = renderPlotly({
p<-plot_ly(x = c( -2, 0, 1.5 ),y = c( -2, 1, 2.2), type = 'scatter' ,mode = 'lines') %>%
add_trace(x=c(-1,0.4,2.5),y=c(2, 0, -1),type='scatter',mode='lines')
if(!is.null(input$Addtext)){
if('trace 0' %in% input$Addtext){
p<- p %>% add_trace(x=c( -2, 0, 1.5 ),y= c( -2, 1, 2.2),type='scatter',mode='text',
text=c('(-2,-2)','(0,1)','(1.5,2.2)'),
textposition='right',textfont = list(color = '#000000', size = 10),
hoverinfo='skip',showlegend=FALSE)
}
if('trace 1' %in% input$Addtext){
p<- p %>% add_trace(x=c( -1, 0.4, 2.5 ),y= c( 2, 0, -1),type='scatter',mode='text',
text=c('(-1,2)','(0.4,0)','(2.5,-1)'),
textposition='right',textfont = list(color = '#000000', size = 10),
hoverinfo='skip',showlegend=FALSE)
}
}
return(p)
})
}
)