Reputation: 131
I want to upload multiple tables and display them separately.
For example: I would like to upload N tables (I don't know N in advance) and want to display them in the main panel as:
Table1:
Table2:
Table3:
.....
My code is shown below but it did not work. How to change it?
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
fileInput(
inputId = "calfile",
label = "Choose CSV File",
multiple = TRUE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv")
)
),
mainPanel(
tableOutput("contents")
)
)
)
server <- function(input, output) {
data<-reactive({
if (is.null(input$calfile))
return()
else
{
nfile<-nrow(input$calfile)
csv=list()
for(i in 1: nfile)
{
csv[[i]]=read.csv(input$calfile$datapath[i])
}
}
})
output$contents<- renderTable(data())
}
shinyApp(ui, server)
Many thanks.
Upvotes: 2
Views: 414
Reputation: 30539
Here is an example that may be helpful:
https://stackoverflow.com/a/35943224/3460670
Edit: Try this for your server for creating N tables. You can read in your N data files in a list, and dynamically create outputs for the N tables in an observe
expression.
server <- function(input, output) {
observe({
if (!is.null(input$calfile)) {
N_tables = length(input$calfile[, 1])
upload <- list()
for (i in 1:N_tables) {
upload[[i]] <- read.csv(input$calfile$datapath[i])
}
output$contents <- renderUI({
table_output_list <- lapply(1:N_tables, function(i) {
tableOutput(paste0("table_name", i))
})
do.call(tagList, table_output_list)
})
for (i in 1:N_tables) {
local({
my_i <- i
output[[paste0("table_name", my_i)]] <- renderTable({
upload[[my_i]]
})
})
}
}
})
}
Upvotes: 1