【问题标题】:Using Conditionalpanel Function in Shiny在 Shiny 中使用条件面板函数
【发布时间】:2018-09-24 06:38:54
【问题描述】:

我正在尝试创建使用条件面板的场景,我可以让用户输入复选框来显示 1 个或 2 个图,一个接一个。

我的可重现代码可以在下面找到,但是,我无法显示这些图。

有人可以告诉我我在哪里犯了错误吗?

library(shiny)

ui = fluidPage(
  titlePanel("Plot1 or Plot2?"),
  sidebarLayout(
    sidebarPanel(
      checkboxGroupInput("my_choices", "Plot1 or Plot2",choices = c("Plot1", "Plot2"), selected = "Plot1"),width=2),
    mainPanel(
      conditionalPanel(
        condition = "input.my_choices == 'Plot1'",
        plotOutput("plot1")
      ),
      conditionalPanel(
        condition = "input.my_choices == 'Plot2'",
        plotOutput("plot2")
      ), 
      conditionalPanel(
        condition = "input.my_choices.includes('Plot1', 'Plot2')",
        plotOutput("plot1"),
        plotOutput("plot2")
      )
    )
  )
)

server = function(input, output) {

  output$plot1 <- renderPlot({plot(iris)})
  output$plot2 <- renderPlot({plot(mtcars)})
}

shinyApp(ui, server)

更新:

我得到了我想要的,但没有使用 ConditionalPanel 函数。下面是代码:

如果有人可以与我分享使用 ConditionalPanel 函数的正确方法,将不胜感激! (:

library(shiny)

#data
df <- iris

#ui
ui <- fluidPage(
    sidebarPanel(
      checkboxGroupInput(inputId = "Question",
                         label = "Choose the plots",
                         choices = c("Plot1", "Plot2", "Plot3"),
                         selected = "")),
    mainPanel(
      uiOutput('ui_plot') 
    )
  )

#server
server <- function(input, output)
{
  # gen plot containers
  output$ui_plot <- renderUI({ 
    out <- list()
    if (length(input$Question)==0){return(NULL)}
    for (i in 1:length(input$Question)){
      out[[i]] <-  plotOutput(outputId = paste0("plot",i))
    }  
    return(out) 
  })

  # render plots
  observe({  
    for (i in 1:3){  
      local({  #because expressions are evaluated at app init
        ii <- i 
        output[[paste0('plot',ii)]] <- renderPlot({ 
          if ( length(input$Question) > ii-1 ){  
            return(plot(runif(100)))
          } 
          NULL
        })
      })
    }                                  

  })

}

shinyApp(ui, server)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    我会给你一个替代方案,因为你需要用不同的id 创建新的地块才能让它工作。我能想到的最简单的方法是使用shinyjs 包及其hideshow 函数。您也可以通过renderUI 执行此操作,但仅当您显示和隐藏元素时,您不应该给服务器带来不必要的工作

    library(shiny)
    library(shinyjs)
    
    ui = fluidPage(
      useShinyjs(),
      titlePanel("Plot1 or Plot2?"),
      sidebarLayout(
        sidebarPanel(
          checkboxGroupInput("my_choices", "Plot1 or Plot2",choices = c("Plot1", "Plot2"), selected = "Plot1"),width=2),
        mainPanel(
          plotOutput("plot1"),
          plotOutput("plot2")
        )
      )
    )
    
    server = function(input, output,session) {
    
      # hide plots on start
      hide("plot1");hide("plot2")
    
      output$plot1 <- renderPlot({plot(iris)})
      output$plot2 <- renderPlot({plot(mtcars)})
    
      observeEvent(input$my_choices,{
    
        if(is.null(input$my_choices)){
          hide("plot1"); hide("plot2")
        }
    
        else if(length(input$my_choices) == 1){
          if(input$my_choices == "Plot1"){
            show("plot1");hide("plot2")
          }
          if(input$my_choices == "Plot2"){
            hide("plot1");show("plot2")
          }
        }
    
        else{
    
          if(all(c("Plot1","Plot2") %in% input$my_choices)){
            show("plot1");show("plot2")
          }
        }
      },ignoreNULL = F)
    }
    
    shinyApp(ui, server)
    

    【讨论】:

    • 喜欢你的新解决方案!!它工作得很好,我了解了 Shinyjs 包。非常感谢! (:
    猜你喜欢
    • 2016-08-14
    • 2016-06-04
    • 1970-01-01
    • 2018-11-29
    • 2015-07-07
    • 2021-11-05
    • 2014-11-18
    • 2020-06-03
    • 2018-03-25
    相关资源
    最近更新 更多