2017-09-13 69 views
1

我解決了我的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 
    }) 
    } 
) 

回答

1

再次使用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) 
    }) 
    } 
) 

enter image description here

+0

感謝時,請使用plotlyOutputrenderPlotly。不知何故,我不確定是否仍然支持'renderPlotly'。猜猜並非所有的事情都在閃閃發亮的作弊表格上。 – 5th

+0

'plotly'和大多數其他'JS'庫不被'shiny'團隊支持,因此您只需閱讀文檔https://plot.ly/r/shiny-tutorial/ –

+0

意識到您在如果函數,字符串(例如''trace 0'')需要先到達。只有這樣''輸入$ Addtext'。否則兩個註釋都不能顯示 – 5th