Make the display reactive

I am trying to make the output reactive. I am not able to execute it. Is it possible to achieve like this,
For example, the moment I enter no of datasets, in the textbox, I need to get so many "Choose the csv file" boxes. For example, if the input is 3, the output should be 3 boxes with "Choose the csv file" . Below is the code I tried for it

library(shinydashboard)
library(readxl)
out <- data.frame(baseFns = ls('package:base'))
ui <- dashboardPage(
  dashboardHeader(title = "Loading data"),
  dashboardSidebar(sidebarMenu(
    menuItem("Load Data", tabName = "Load_Data", icon = icon("balance-scale")),
    menuItem("Analysis", tabName = "Analysis", icon = icon("chart-bar"))
  )),
  dashboardBody(
    tabItems(tabItem(tabName = "Load_Data",textInput("T", "No of data sets", value = 0,width = 150),
                     fluidRow(box(fileInput("datafile","Choose the csv file",multiple = TRUE, 
                                            accept = c("text/csv","text/comma-separated-values,text/plain",".csv")),width = 2)
                              ),
                     fluidRow(box(title = "Dataset",uiOutput("filter_70"),width = 5000)))
    ))
)

server <- function(input,output){
  output$contents <- renderTable({
    file_to_read <- input$datafile
    if(is.null(file_to_read))
      return(NULL)
    a <- read.csv(file_to_read$datapath)
    head(a,n=15)
  })

}
shinyApp(ui, server)

Hi @vinayprakash808. You may try to use insertUI to generate the number of fileInput as follow.

library(shinydashboard)
library(readxl)
library(shiny)
out <- data.frame(baseFns = ls('package:base'))
ui <- dashboardPage(
  dashboardHeader(title = "Loading data"),
  dashboardSidebar(sidebarMenu(
    menuItem("Load Data", tabName = "Load_Data", icon = icon("balance-scale")),
    menuItem("Analysis", tabName = "Analysis", icon = icon("chart-bar"))
  )),
  dashboardBody(
    tabItems(tabItem(tabName = "Load_Data", numericInput("T", "No of data sets", value = 1, min = 1, width = 150),
                     # textInput("T", "No of data sets", value = 0,width = 150),
                     fluidRow(
                       tags$div(id = "container")
                       # box(fileInput("datafile","Choose the csv file",multiple = TRUE, 
                       #                      accept = c("text/csv","text/comma-separated-values,text/plain",".csv")),width = 2)
                     ),
                     fluidRow(box(title = "Dataset",uiOutput("filter_70"),width = 5000)))
    ))
)

server <- function(input,output){
  observe({
    req(input$T)
    removeUI("#fileInputContainer")
    insertUI("#container", "afterBegin", tags$div(id = "fileInputContainer"))
    for(i in 1:input$T) {
      insertUI("#fileInputContainer", "beforeEnd",
               box(fileInput(paste0("dataFile", i), "Choose the csv file",
                             accept = c("text/csv","text/comma-separated-values,text/plain",".csv")),width = 2))
    }
  })
  
  # output$contents <- renderTable({
  #   file_to_read <- input$datafile
  #   if(is.null(file_to_read))
  #     return(NULL)
  #   a <- read.csv(file_to_read$datapath)
  #   head(a,n=15)
  # })
  
}
shinyApp(ui, server)

Very perfect and thanks a lot. Million thanks. :slight_smile:

Hi, it is working. But if you do not mind. can you please explain below codes. Its better I understand and then put this :slight_smile:

 removeUI("#fileInputContainer")
    insertUI("#container", "afterBegin", tags$div(id = "fileInputContainer"))
    for(i in 1:input$T) {
      insertUI("#fileInputContainer", "beforeEnd",
               box(fileInput(paste0("dataFile", i), "Choose the csv file",
                             accept = c("text/csv","text/comma-separated-values,text/plain",".csv")),width = 2))

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