0

I have a dataframe that is updated each time the user changes the year, let's call it mydata.

In output, I'd like to have a table with the possibility to select columns to show, but I'd like the "choices" argument (in the checkboxGroupInput) to be different according the year.

Short part of my current code :

Ui :

sliderInput("year","Choose the year",min=2013,max=2018,value=2017,step=1,sep=""),
conditionalPanel(condition="input.year==2013",
             checkboxGroupInput("show_vars13", "Choose variables to show:",colnames13, 
                                 selected=c(var1,var2))
),
conditionalPanel(condition = "input.year==2014",
             checkboxGroupInput("show_vars14","Choose variables to show:",colnames14,
                                selected=c(c(var1,var2))
),
conditionalPanel(condition="input.year==2015",
             checkboxGroupInput("show_vars15","Choose variables to show:",colnames15,
                                selected=c(var1,var2))
),
conditionalPanel(condition="input.year==2016",
             checkboxGroupInput("show_vars16","Choose variables to show:",colnames16,
                                selected=c(var1,var2))
),
conditionalPanel(condition="input.year==2017",
             checkboxGroupInput("show_vars17","Choose variables to show:",colnames17,
                                selected=c(var1,var2))
),

tableOutput("data")

server :

output$data <- renderTable({

  year <- as.numeric(substr(input$year,3,4))

  for (i in 13:17){
    if (year==i){
      show_vars <- get(paste("input$show_vars",i,sep=''))
  }
}

  return(mydata[, show_vars, drop = FALSE])
})

This code doesn't work. R Shiny returns "object 'input$show_vars17' not found"

2 Answers 2

1

Try

show_vars <-input[[paste0("show_vars",i)]]
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks again :P very useful
0

in

show_vars <- get(paste("input$show_vars",i,sep='')) ,

"input$show_vars" is just a string ..

if you want to refer to the variable, remove quotes, and write :

show_vars <- get(paste(input$show_vars,i,sep=''))

3 Comments

Ah ok, sorry .. I didn't pay attention to your i .. Try this : get(input$eval(paste("show_vars",i), sep = '')
Still doesn't work... :/ This time, I got "attempt to apply non function"
Adding to the correct answer of @Florian, you can also get rid of i if you call your varables show_vars2014 instead of show_vars14 and just write input[[paste0("show_vars",input$year,sep='')]]and get rid of for andif

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.