【问题标题】:shiny plot comes in two windows (plot does not appear)闪亮的情节有两个窗口(情节不出现)
【发布时间】:2017-09-13 15:41:25
【问题描述】:

我解决了我的own question here。但是我的代码有一个很大的缺陷:当我运行应用程序时,Shiny 只显示复选框。在 R-Studio 中,它显示了弹出窗口中的复选框和 R-Studio-Viewer 选项卡中的绘图。

我尝试将这两个元素放在sidebarLayout-Panel 中。但无济于事。有什么办法可以使这项工作?

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
    })
  }
)

【问题讨论】:

    标签: r shiny plotly


    【解决方案1】:

    使用plotly包时请使用plotlyOutputrenderPlotly

    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)
        })
      }
    )
    

    【讨论】:

    • 再次感谢。不知何故,我不确定是否仍然支持 renderPlotly。猜猜不是所有的东西都在闪亮的备忘单上。
    • plotly 和大多数其他JS 库不受shiny 团队支持,因此您只需阅读文档plot.ly/r/shiny-tutorial
    • 意识到你在 if 函数中犯了一个错误,字符串(例如'trace 0')需要放在第一位。只有这样才会出现input$Addtext。否则两个注解都无法显示
    猜你喜欢
    • 2019-06-19
    • 2017-11-07
    • 2022-01-03
    • 2021-05-10
    • 2017-01-21
    • 1970-01-01
    • 2018-01-31
    • 1970-01-01
    • 2018-04-03
    相关资源
    最近更新 更多