考虑下面的玩具示例.当复选框为=TRUE但似乎没有效果时,我试图显示不同的图形.我使用"switch"根据其标签显示图形.感谢您提供有关如何使用条件语句(if)显示备用图形的提示.

   library(shiny)

    mytest <- c("first","second")
   # Define UI for application that draws a histogram
    ui <- fluidPage(

    # Application title
    titlePanel("Old Faithful Geyser Data"),

        sidebarLayout(
        sidebarPanel(
          selectInput(inputId = "test",
                      label = "Test",
                      choices = mytest,
                      selected = "first"),
          checkboxInput("checkbox","check this box",FALSE)),

        mainPanel(
           plotOutput("distPlot")
        )))

      server <- function(input, output) {

    output$distPlot <- renderPlot({
      switch(input$test,
             "first" = plot(rnorm(100)),
             
             if("checkbox" == FALSE){
             "second" = plot(rnorm(1000),col="blue")
             } else{
       #I want the graphic below to be displayed when the checkbox = TRUE or checked
            "second" = plot(rnorm(10000),col='red')   
             }
      )
})
}

shinyApp(ui = ui, server = server)

推荐答案

如果需要复选框的值,则必须使用input[["checkbox]]input$checkbox引用它.

我想当下拉列表为"first"时,您需要一个黑色块,当下拉列表为"second"时,您需要一个蓝色/红色块(取决于复选框)?

Select 第一项时,解决方案将禁用该复选框.为此,您可以使用shinyjs包中的函数enabled/disabled.为此,我们必须在ui中初始化shinyjs.

library(shiny)
library(shinyjs)

mytest <- c("first","second")
# Define UI for application that draws a histogram
ui <- fluidPage(
  useShinyjs(), # to initialise shinyjs
  
  # Application title
  titlePanel("Old Faithful Geyser Data"),
  
  sidebarLayout(
    sidebarPanel(
      selectInput(inputId = "test",
                  label = "Test",
                  choices = mytest,
                  selected = "first"),
      disabled( # start as disabled
        checkboxInput("checkbox","check this box", FALSE))
      ),
    
    mainPanel(
      plotOutput("distPlot")
    )))


server <- function(input, output) {
  
  output$distPlot <- renderPlot({
    switch(
      input$test,
      "first" = { 
        plot(rnorm(100))
        disable("checkbox")
      },
      {
        enable("checkbox")
        if(input[["checkbox"]] == FALSE){
         "second" = plot(rnorm(1000), col="blue")
        } else{
         "second" = plot(rnorm(10000), col="red")   
        }
      }
    )
  })
}

shinyApp(ui = ui, server = server)

R相关问答推荐

如何将log 2刻度上的数字转换为自然log

在R中使用GG Plot时如何 suppress 等值线图中的彩色条

次级y轴R gggplot2

格点中指数、双曲和反双曲模型曲线的正确绘制

筛选出以特定顺序患病的个体

以更少间隔的较小表中的聚合离散频率表

如何对2个列表元素的所有组合进行操作?

使用rest从header(h2,h3,table)提取分层信息

仅 Select 超过9行的CSV文件

DEN扩展包中的RECT树形图出现异常行为

用两种 colored颜色 填充方框图

当我添加美学时,geom_point未对齐

R -使用矩阵reshape 列表

R -基线图-图形周围的阴影区域

层次树图的数据树

R仅当存在列时才发生变异

在同一单元格中创建包含整数和百分比的交叉表

如何在刻面和翻转堆叠条形图中对齐geom_text()

图中显示错误 colored颜色 的图例geom_sf

对计算变量所有唯一值的变量进行变异