问题
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"
回答1:
Try
show_vars <-input[[paste0("show_vars",i)]]
回答2:
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=''))
来源:https://stackoverflow.com/questions/48788042/select-different-columns-in-rendertable