Below are my r-shiny codes. the problem seems come from the server part. if I use the dataset name directly, as the line quoted out "#dList <- mtcars_6[mtcars_6$cyl==6,]", then the app works.
But if I want to make it more dynamic, like use input$index to choose to use different datasets, like "dNam <- paste0(...) and dList <- dNam[dNam$cyl==6,], then it won't work.
Could be an obvious fix, but I just start learning R by project-based, had struggled 2 days and couldn't fix it. Really appreciate if anyone could point out what I missed or was wrong.
Regards,
#2 data involved, you can consider the mtcars as a summary data, and the mtcars_6 is the 6th
#listing data with more details. it's like when you see the summary and want to see the expanded
#data listings.
data(mtcars)
mtcars_6 <- mtcars
#here are the UI & Server part for a r-shiny app
ui <- fluidPage(
titlePanel("Example"),
fluidRow(
column(4,
selectInput("index",
"Index",
c("All",
unique(as.character(mtcars$cyl))))
)
),
DT::dataTableOutput("table")
)
server <- function(input, output) {
output$table <- DT::renderDataTable(DT::datatable({
if (input$index != "All") {
# here are the part I failed, if I use dLsit<- mtcars_6 directly, then gives what I want,
# but if use paste0() to involve the input$index to generate a dataset name, then it
# vvv--- failed.---vvv
#dList <- mtcars_6[mtcars_6$cyl==6,]
dNam <- paste0("mtcars_",input$index)
dList <- dNam[dNam$cyl==6,]
}
dList
}
))
}
shinyApp(ui = ui, server = server)
I tried:
dList <- sprintf("mtcars_",input$index),
dList <- get(paste0("mtcars_",input$index),inherits=F),
assign(dList,paste0("mtcars_",input$index))...
Expecting to let the program use the dataset whose name tailed with input$index.