Pushkar
Pushkar

Reputation: 45

Converting Outputs to Inputs in R Shiny

I was trying to get the list of files in a location based on user selected date and supply that list as options (maybe with checkbox) to select a file and show that as output in R Shiny. I could get to the point where I get the list, but unable to supply this to user as for option to make a selection and shpw the contents. Any help is appreciated.

ui <- fluidPage(
        titlePanel("Select the execution date"),
        dateInput("ME_DATE",label=h3("Execution Date Input"), value="2020-05-29"),
        hr(),
        fluidRow(column(3,verbatimTextOutput("Output_path"))),
        h4("Files in Above Location"),
        hr(),
        verbatimTextOutput("Output_files")

)

server <- function(input, output) {

        # Main Directory for Source Files
        Copied_Source_Files_DIR <- "U:/Source Files/"
        
        Copied_Source_Files_loc <- reactive({
                                        
        year_N_ME_DATE <- format(input$ME_DATE,"%Y")
        month_N_ME_DATE <- format(input$ME_DATE,"%m")
        month_T_ME_DATE <- months(input$ME_DATE)

        file.path(paste(Copied_Source_Files_DIR,year_N_ME_DATE,"/",month_N_ME_DATE,". ",month_T_ME_DATE,"/",sep=""))
        })
        
        output$Output_path <- renderPrint({ Copied_Source_Files_loc() })
        
        # files
        output$Output_files <- renderPrint(list.files(Copied_Source_Files_loc()))
        
}

shinyApp(ui, server)

Upvotes: 1

Views: 725

Answers (2)

Jan
Jan

Reputation: 5274

updateCheckboxInput' would be a way to deal with it (as r2evans suggested). This solution uses a uiOutput` that allows you to generate the UI elements on the server side and send that to the UI. It does not require a reactive expression as step between input and output.

ui <- fluidPage(
  titlePanel("Select the execution date"),
  dateInput("ME_DATE",label=h3("Execution Date Input"), value="2020-05-29"),
  hr(),
  fluidRow(column(3,verbatimTextOutput("Output_path"))),
  h4("Files in Above Location"),
  hr(),
  uiOutput("Output_files")
)

server <- function(input, output) {
  
  # Main Directory for Source Files
  Copied_Source_Files_DIR <- "./"
  
  Copied_Source_Files_loc <- reactive({
    
    year_N_ME_DATE  <- format(input$ME_DATE,"%Y")
    month_N_ME_DATE <- format(input$ME_DATE,"%m")
    month_T_ME_DATE <- months(input$ME_DATE)
    
    file.path(paste(Copied_Source_Files_DIR,year_N_ME_DATE,"/",month_N_ME_DATE,". ",month_T_ME_DATE,"/",sep=""))
  })
  
  output$Output_path <- renderPrint({ Copied_Source_Files_loc() })
  
  # files
  output$Output_files <- renderUI({
    checkboxGroupInput("File_Selector", 
                       "Select a file", 
                       choices = list.files(Copied_Source_Files_loc()))
  })
  
}

shinyApp(ui, server)

Upvotes: 1

r2evans
r2evans

Reputation: 160952

Don't think of an output$ as a possible intermediate step, it is always a terminator in a sense. It is best that you keep data separate as reactive(.) data, and use it in multiple places.

  filelist <- reactive({ list.files(Copied_Source_Files_loc()) })
  output$Output_files <- renderPrint(filelist())

Upvotes: 1

Related Questions