首页 > 解决方案 > 添加单个复选框输入

问题描述

我正在寻找一种添加复选框输入的方法,它应该在勾选时显示此数据集的等待时间和中断。我是 rstudio 的新手,不知道自己在做什么。该程序的代码是:

#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
#    http://shiny.rstudio.com/
#

library(shiny)

library(tidyverse)
# Define UI for application that draws a histogram
ui <- fluidPage(

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

   # Sidebar with a slider input for number of bins 
   sidebarLayout(
      sidebarPanel(
         sliderInput("bins",
                     "Number of bins:",
                     min = 1,
                     max = 50,
                     value = 30)
      ),

      checkboxInput("checkbox", label = "Choice A", value = TRUE),
      # Show a plot of the generated distribution
      mainPanel(
         plotOutput("distPlot")
      )
   )
)

# Define server logic required to draw a histogram
server <- function(input, output) {

   output$distPlot <- renderPlot({
      # generate bins based on input$bins from ui.R
      x    <- faithful[, 2] 
      bins <- seq(min(x), max(x), length.out = input$bins + 1)

      # draw the histogram with the specified number of bins
      hist(x, breaks = bins, col = 'darkgray', border = 'white')
   })
}

# Run the application 
shinyApp(ui = ui, server = server)

到目前为止,在运行代码并尝试修复它之后,我得到了同样的错误。match.arg(position) 中的错误:“arg”必须为 NULL 或字符向量

标签: shinyrstudio

解决方案


问题是您当前将三个参数传递给 function sidebarLayout,尽管只需要两个参数。问题中的 ui 定义如下所示

fluidPage(
  sidebarLayout(
    sidebarPanel(
      sliderInput(...)
    ),
    checkboxInput(...),
    mainPanel(
      plotOutput(...)
    )
  )
)

(我用作...占位符以使代码更具可读性。)checkboxInput应该放在任何一个面板中。例如

fluidPage(
  sidebarLayout(
    sidebarPanel(
      sliderInput(...),
      checkboxInput(...)
    ),
    mainPanel(
      plotOutput(...)
    )
  )
)

推荐阅读