基于 Shiny R 中的单选按钮显示滑块输入

Show slider inputs based on radio buttons in Shiny R

如何让滑块(例如命名为 "Divide mtcars wt")弹出 "option b" 单选按钮,并将 x 划分为 aes(来自 mtcars 数据集的 wt) "option b" 与下面可重现示例中的滑块值?然后,如果选择了 "option a" 单选按钮,则再次隐藏该滑块。我看过几个 Stack overflow 帖子,但它们没有帮助。当我实现它时,我一直收到 NULL "option b" 的绘图输出错误,所以我把它去掉了。

library(shiny)
library(ggplot2)
options(warn=-1)

#ui.r
ui <- pageWithSidebar(
  headerPanel("many plots app"),

  sidebarPanel(
    sliderInput("width", "Plot Width (%)", min = 0, max = 100, value = 100), 
    sliderInput("height", "Plot Height (px)", min = 0, max = 800, value = 800),
    uiOutput("filter_plot")

  ),

  mainPanel(
    uiOutput("plot")
  )
)
#server.r
server <- function(input, output, session) {

  options(warn = -1) #This doesn't stop console warning

  output$filter_plot<-renderUI({
    radioButtons("rd","Select Option",choices = c("a",
                                                  "b",
                 selected = "a"))
  })

output$plot <- renderUI({
    if(input$rd=="a"){
      output$plot1<- renderPlot({
        ggplot2::ggplot(mtcars, aes(mpg, cyl))+ geom_point()
      })
      plotOutput("plot1", width = paste0(input$width, "%"), height = input$height)
    }
    else if(input$rd=="b"){
      output$plot2<- renderPlot({
        ggplot2::ggplot(mtcars, aes(wt, qsec))+ geom_line()

      })
      plotOutput("plot2", width = paste0(input$width, "%"), height = input$height)
    }
  })
}
shinyApp(ui = ui, server = server)

提前致谢!

可以再帮个忙,为什么我隐藏了控制台里还有警告。我的应用程序可以运行,但存在警告:

#Warning: Error in if: argument is of length zero
#  96: renderUI [C:/Users/YOU/Documents/DIRECTORY/xyz.R#443]
#  95: func
#  82: origRenderFunc
#  81: output$plot
#  1: runApp

当从单选按钮输入中选择 'b' 时,您可以使用 conditionalPanel 到 show/hide 和 sliderInput

为了防止出现警告,您可以在 if 语句中使用它之前要求 req(input$rd)

如果这是您想要的行为,请告诉我。

library(shiny)
library(ggplot2)
options(warn=-1)

#ui.r
ui <- pageWithSidebar(
  headerPanel("many plots app"),

  sidebarPanel(
    sliderInput("width", "Plot Width (%)", min = 0, max = 100, value = 100), 
    sliderInput("height", "Plot Height (px)", min = 0, max = 800, value = 800),
    uiOutput("filter_plot"),
    conditionalPanel(
      "input.rd == 'b'",
      sliderInput("sl_wt", "Divide Weight", min = 1, max = 10, value = 5)
    )
  ),

  mainPanel(
    uiOutput("plot")
  )
)
#server.r
server <- function(input, output, session) {

  options(warn = -1) #This doesn't stop console warning to stop

  output$filter_plot<-renderUI({
    radioButtons("rd","Select Option",choices = c("a",
                                                  "b",
                                                  selected = "a"))
  })

  output$plot1<- renderPlot({
    ggplot2::ggplot(mtcars, aes(mpg, cyl))+ geom_point()
  })

  output$plot2<- renderPlot({
    ggplot2::ggplot(mtcars, aes(wt/input$sl_wt, qsec))+ geom_line()
  })

  output$plot <- renderUI({
    req(input$rd)
    if(input$rd=="a"){
      plotOutput("plot1", width = paste0(input$width, "%"), height = input$height)
    }
    else if(input$rd=="b"){
      plotOutput("plot2", width = paste0(input$width, "%"), height = input$height)
    }
  })
}
shinyApp(ui = ui, server = server)