'Why does shiny app not load local data after selecting directory and file in folder?

I am trying to built a shiny app, which allows the user to

  1. select a local directory
  2. select a csv from a list of available *.csv files in the folder
  3. display the table / work with it.

The app loads a csv, but only for files with a static filename (see datpat static example). I assume the path to the file is the problem. The path to the directory seems to be ok. Also, the app should be accessed online (most likely own server), if this makes a difference.

This is a minimal code, which hopefully helps finding the error. You just have to navigate to a folder, where .csv files are. This example works if you choose a folder with a csv file named "data.csv". Thanks a lot for your help!

library(shiny)
library(shinyFiles)
library(htmltools)


##############################################################################

ui = navbarPage(
  HTML("Title"),
  tabPanel(HTML("<font size=3>Start</font>"),
           sidebarPanel(width = 2,
                        shinyDirButton('directory', 'Folder select', 'Please select a folder'),
                        checkboxInput('header', 'Header', TRUE),
                        radioButtons('sep', 'Separator', c(Comma=',',Semicolon=';',Tab='\t'), selected=';'),
                        radioButtons('quote', 'Quote', c(None='','Double Quote'='"','Single Quote'="'"), selected='"'),
                        uiOutput("csv_select"), #button to select files
                        ),
           mainPanel(
             fluidRow(
               column(6, tags$b(textOutput("text")))),
             tags$hr(),
             fluidRow(
               column(6, dataTableOutput("table"))
             )
           )
  )
)

server = function(input, output, session) {
  
  volumes <- c(Home = fs::path_home(), "R Installation" = R.home(), getVolumes()())
  shinyDirChoose(input, 'directory', roots=volumes, session=session)
  path1 <- reactive({
    return(print(parseDirPath(volumes, input$directory)))
  })
  
  ### Select mp4 files within directory
  output$csv_select <- renderUI({
    csvs <- list.files(parseDirPath(volumes, input$directory),pattern=c(".csv"),recursive = F,full.names=T)
    
    selectInput("csv_select","csv-ography",choices=basename(csvs))
  })
  
  
  file1 <-  reactive({
    return(print(input$csv_select))
  })
  
  
  dataruw1 <- eventReactive(input$directory, {
    
    
    #datpat <- paste0(path1(),"/",file1()) # dynamic case, which selects file from folder --> does not work
    datpat <- paste0(path1(),"/","data.csv") # static case, which works after directory selection
    
    
    
    dataruw <- read.csv(datpat, header = input$header, sep = input$sep, quote = input$quote,
                        stringsAsFactors = FALSE)
    dataruw
  })
  
  output$text <- renderText({
   paste0(path1(),"/",file1())
  
    })
  
  output$table <- renderDataTable({
    dataruw1()
  })
  
}

shinyApp(ui = ui, server = server, options = list(launch.browser=TRUE))


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source