R-基于复选框之间的plotOutput和plotlyOutput之间的切换



我正在尝试构建一个闪亮的应用程序,该应用仅在用户标记交互式数字的复选框时才加载plotly。但是,到目前为止,我尝试的是绘制两个数字的最终,无论复选框值如何:

require('plotly')
require('shiny')
ui <- fluidPage(
  tabsetPanel(
    id = 'mainTab',
    tabPanel(
      'conditionally interactive tab',
      checkboxInput(
        inputId = 'interactive', label = 'Interactive figure', value = FALSE
      ),
      conditionalPanel(
        condition = 'input.interactive == TRUE',
        plotlyOutput('interactivePlot')
      ),
      conditionalPanel(
        condition = 'input.interactive == FALSE',
        plotOutput('staticPlot')
      )
    ),
    tabPanel('unrelated tab')
  )
)
server <- function(input, output, session){
  output$interactivePlot <- renderPlotly({
    plot_ly(iris, x = ~Petal.Length, y = ~Sepal.Length)
  })
  output$staticPlot <- renderPlot({
    plot(Sepal.Length ~ Petal.Length, iris)
  })
}
shinyApp(ui = ui, server = server)

原因是使用绘图时的加载时间较长,以及在手持设备上绘制的不便(试图用反应触摸的图滚动是困难的)。我宁愿不要为他们提供单独的选项卡,但是我意识到,如果没有其他作用,那可能是一种选择。

您非常接近。有条件的condition内部的表达式是JavaScript表达式,也不是R表达式。在JavaScript中,他们使用true/false而不是TRUE/FALSE。因此,只需改变它,它将起作用。

require('plotly')
require('shiny')
ui <- fluidPage(
        tabsetPanel(
                id = 'mainTab',
                tabPanel(
                        'conditionally interactive tab',
                        checkboxInput(
                                inputId = 'interactive', label = 'Interactive figure', value = FALSE
                        ),
                        conditionalPanel(
                                condition = 'input.interactive == true',
                                plotlyOutput('interactivePlot')
                        ),
                        conditionalPanel(
                                condition = 'input.interactive == false',
                                plotOutput('staticPlot')
                        )
                ),
                tabPanel('unrelated tab')
        )
)
server <- function(input, output, session){
        output$interactivePlot <- renderPlotly({
                plot_ly(iris, x = ~Petal.Length, y = ~Sepal.Length)
        })
        output$staticPlot <- renderPlot({
                plot(Sepal.Length ~ Petal.Length, iris)
        })
}
shinyApp(ui = ui, server = server)

最新更新