在 R Shiny 中显示给定 colnames 的多个复选框

Displaying multiple checkboxes given colnames within R Shiny

我正在尝试从 table 中获取 colnames,然后每一个都生成我需要的许多复选框。但是,我继续收到以下错误:

Error in match.arg(position) : 'arg' must be NULL or a character vector

这是我的代码:

lemon<-read.csv("LemonData.csv")
 csvuploaded<-TRUE
 shinyApp(
   ui = fluidPage(
     sidebarLayout(
       sidebarPanel(
         uiOutput(outputId="factorcheckboxes")
         )    
   )), 
   server = function(input, output) {    
     output$factorcheckboxes <- renderUI({
       if(is.null(csvuploaded))
         return(NULL)
       if(!(is.null(csvuploaded)))
         collen<-length(colnames(lemon))
         factornames<-vector()
         for(i in 1:collen){
           factornames<-c(factornames,colnames(lemon)[i])
         }
         checkboxGroupInput(inputId="variable",label="Variable:",choices=as.list(factornames),selected=NULL,inline=FALSE)
     })    
   })

当我尝试 运行 给定 here 的样本时,我遇到了类似的错误。出现这样的错误我无法查明代码中的源头,也不知道调试Shiny时如何使用断点。

更新: LemonData.csv 的请求样本:

------------------------------------------
Response | Factor 1 | Factor 2 | Factor 3
------------------------------------------
5        | 2        | 5        | 2        
------------------------------------------
7        | 1        | 4        | 3
------------------------------------------

我无法重现错误,但我能够使用以下代码生成复选框。唯一的问题是缺少 mainPanel 参数,因为您使用的是 sidebarLayout.

lemon<-read.csv("LemonData.csv")
csvuploaded<-TRUE
library(shiny)
shinyApp(
  ui = fluidPage(
    sidebarLayout(
      sidebarPanel(
        uiOutput(outputId="factorcheckboxes")
      ),
      mainPanel(
        )
    )), 
  server = function(input, output) {    
    output$factorcheckboxes <- renderUI({
      if(is.null(csvuploaded))
        return(NULL)
      if(!(is.null(csvuploaded)))
        collen<-length(colnames(lemon))
      factornames<-vector()
      for(i in 1:collen){
        factornames<-c(factornames,colnames(lemon)[i])
      }
      checkboxGroupInput(inputId="variable",label="Variable:",choices=as.list(factornames),selected=NULL,inline=FALSE)
    })    
  })

我根据提供的table生成了一个"LemonData.csv",看起来像:

Response, Factor 1, Factor 2, Factor 3
5, 2, 5, 2        
7, 1, 4, 3

你的服务器函数可以做得更简单,你只需将 colnames(lemon) 传递给 choices 参数,不需要那个 for 循环。此外,您正在使用 is.null 检查 TRUE/FALSE 这是不正确的,因为 TRUE 和 FALSE 都不为 NULL。

lemon <- read.table(text="Response, Factor 1, Factor 2, Factor 3
5, 2, 5, 2        
7, 1, 4, 3", header=T, sep=",")
csvuploaded<-FALSE

shinyApp(
    ui = fluidPage(
        sidebarLayout(
            sidebarPanel(
              radioButtons("csvuploaded", "uploaded", c(T, F)),  # change csvuploaded
              uiOutput(outputId="factorcheckboxes")
            ),
            mainPanel()
        )), 
    server = function(input, output) {    
        output$factorcheckboxes <- renderUI({
            if(input$csvuploaded) {
                checkboxGroupInput(inputId="variable",
                                   label="Variable:",
                                   choices=colnames(lemon), selected=NULL, inline=FALSE)
            } else { NULL }
        })
    }
)