how to pass multiple dataframes to different output in shiny in r

Hi please wanted to ask for help if it is possible to pass multiple dataframes to different output in shiny in r, am fairly new to shiny in r:

tables<-paste0("table",1:10)
> tables
 [1] "table1"  "table2"  "table3"  "table4"  "table5"  "table6"  "table7"  "table8"  "table9"  "table10"

 for(i in 1:10){
   output$tables[i]<-renderTable(my.list[[i]],rownames = T)
   }

Hello,

This a a way to create a number of tables in the UI and assign them each a dataset:

library(shiny)
library(plotly)

ui <- fluidPage(
  uiOutput("allTables")
)

server <- function(input, output, session) {
  
  #Create fake data tables
  nTables = 3
  myData = lapply(1:nTables, function(x) {data.frame(x = 1:10, y = runif(10))})
  
  #Create the UI for the tables
  output$allTables = renderUI({
    
        eval(parse(text = 
            paste0("tagList(", 
              paste0("tableOutput('table", 1:nTables,"')", collapse = ", "), 
            ")")))

  })
  
  #Assign the output to the UI
  eval(parse(text = paste0("output$table", 1:nTables, "<-renderTable(myData[[",1:nTables,"]])")))

  
}

shinyApp(ui, server)

It doesn't look very readable as you have to use string evaluation to get the effect you want, but it does work...

If you would tell us a bit more of your exact goal, I think there might be more elegant ways to solve this issue... normally you know beforehand how many tables you'll have and what data goes in it, but maybe your case is special :slight_smile:

Anyway, this will get your started!
PJ

Thank you very much for the swift reply PJ. This code could help my goal but the code outputs all the datasets at the same time.

Is there a way on can assess a single dataset out of the three one at a time in the UI?

Also my goal is to create a dashboard where each tabItem displays the one of the list of dataframes. Thus instead of typing the code all over again, each tabItem outputs one of dataframe.

Hi,

So how many tabs you have and how many data frames? And where is the data coming from?

If your tabs are repetitive, you might want to create modules. I don't know much about them, but here is a link to get started:

Maybe it's best you create a Reprex (minimal reproducible example). You can find more info in this link:

PJ

The number of tabs currently is 10 and the dataframes are also 10. Thus the number of tabs should be the same as the number of dataframes. Thank you.

The dataframe is coming from a xlsx file i have cleaned to create these multiple dataframes.

Hi,

I think the best way of solving this will be to create a module that you can then load on every tab. This will create clean and short code.

To get started, I suggest you watch this excellent Shiny webinar where they exactly explain how to create a module and call it on different tabs to save time and code.
https://www.rstudio.com/resources/videos/modularizing-shiny-app-code/

Grtz,
PJ

Hi PJ,

You have been very helpful today and i really appreciate it. Thank you once more.

Cheers.

1 Like

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.