0

I am trying to create a number of tabs based on some list and create similar fields in those tabs with their own identity. Can we create and use reactives like

eval(paste0("Updated_files_list_",Some_List[k], "()"))

I have used following code and one location but that will be different for different tabs it seems that reactives are created for the lists but I am unable to either assign values to it or call it -

Some_List <- list("AGG", "CMP1", "CMP2")
Some_Long_List <- list("Aggregated Things", "Component 1", "Component 2")

sidebar <- dashboardSidebar(
do.call(sidebarMenu, c(lapply(1:length(Some_List), function(i) {
menuItem(Some_Long_List[i], tabName = paste0(Some_List[i],"_tab"))
}))))

uibody <- dashboardBody(
do.call(tabItems, c(lapply(1:length(Some_List), function(i) {
tabItem(tabName = paste0(Some_List[i],"_tab"), h2(Some_Long_List[i]),
fluidPage(fluidRow(column(3,
dateInput(paste0("ME_DATE_output_",Some_List[i]),label=h2("Select a Date"), value="2020-05-29"))
,column(9,column(verbatimTextOutput(paste0("file_path_",Some_List[i])),width=12),hr(),
    selectInput(paste0('select_file_',Some_List[i]),'Select a File to display', choices=NULL),hr(),
    column(dataTableOutput(paste0('selected_table_',Some_List[i])),width=12)))))
}))))

ui = dashboardPage(dashboardHeader(title = "Results"), sidebar, uibody)

server = function(input, output, session) { 
# Location for Source Inputs
Some_loc <- "K:/Outputs"
lapply (1:length(Some_List), function(k){
ME_DATE_GUI <- reactive({input[[paste0("ME_DATE_output_",Some_List[k])]]})

# Files Path
output[[paste0("file_path_",Some_List[k])]] <- renderPrint({ req(Some_loc) })

# Updated Files list
assign(paste0("Updated_files_list_",Some_List[k]), reactive({ 
lf_some <- list.files(Some_loc,full.names = TRUE) 
names(lf_some) <- basename(lf_some)
lf_some
}))

# Fetch selection of file
observeEvent(eval(paste0("Updated_files_list_",Some_List[k], "()")), {  
updateSelectInput(session, paste0("select_file_",Some_List[k]), choices=eval(paste0("Updated_files_list_", Some_List[k],"()")))
})

# Display Selected file
output[[paste0("selected_table_",Some_List[k])]] <- renderDataTable({
req(input[[paste0("select_file_", Some_List[k])]])
read.csv(input[[paste0("select_file_", Some_List[k])]], header=T)
})

})
}

shinyApp(ui, server)

In the selectInput field, I can see Update_files_list_AGG(), ("AGG", "CMP1" and "CMP2" in respective tabs) but not the list of files in the assigned location. The error I get is Error: cannot open the connection Error

Pushkar
  • 45
  • 6
  • Why are you creating reactive with `eval()` and `paste()`? That generally seems like a less than ideal solution. It's generally better to use lists rather than manipulate your executing environment. Have you looked into `reactiveValues()` list? It's not really clear what you are trying to do. It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick May 11 '21 at 17:13
  • @MrFlick, I have updated the question with the whole code now. Can you please let me know how I can use ``reactiveValues()`` here. I could only think of loops but that didn't work so resorted to lapply, but now stuck in this. – Pushkar May 11 '21 at 18:04

0 Answers0