engineerchange
engineerchange

Reputation: 446

Displaying multiple checkboxes given colnames within R Shiny

I am trying to take colnames from a table and then each of these to produce however many checkboxes I need. However, I continue to get the following error:

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

Here is my code:

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)
     })    
   })

I get a similar error when I try to run the sample given here. I cannot pinpoint the source in the code with such an error, and I'm not sure how to use breakpoints when debugging Shiny.

UPDATE: Requested sample of LemonData.csv:

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

Upvotes: 2

Views: 994

Answers (2)

Rorschach
Rorschach

Reputation: 32426

Your server function can be made much simpler, you just pass colnames(lemon) to the choices argument, there is no need for that for loop. Also, you are checking for TRUE/FALSE with is.null which is incorrect since both TRUE and FALSE are not 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 }
        })
    }
)

Upvotes: 4

Paulo MiraMor
Paulo MiraMor

Reputation: 1610

I couldn't reproduce the error, but I was able to generate the checkboxes with the code below. The only problem was the mainPanel argument missing, since you are using the 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)
    })    
  })

I generated a "LemonData.csv" according to the table provided, which looks like:

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

Upvotes: 0

Related Questions