繁体   English   中英

在Shiny中使用Conditionalpanel函数

[英]Using Conditionalpanel Function in Shiny

我正在尝试创建一个使用条件面板的场景,我可以让用户输入复选框以一个接一个地显示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)

我会给你一个替代方案,因为您将需要创建具有不同id新图以使其起作用。 我能想到的最简单的方法是使用shinyjs包及其hideshow功能。 您也可以通过renderUI进行此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)

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM