Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

r shiny - upload all files from shinyDirChoose folder to server

Tags:

r

shiny

I saved the path of a user-defined folder with shinyDirChoose. Now I want to upload files from that user's folder, but I cannot figure out how to do it. 1) All on the server end? 2) Feeding the file paths to fileInput somehow?

This is how I construct the file paths for the three files that should be uploaded.

### ui end, to browse to desired folder
ui = fluidPage(shinyDirButton('directory', 'Folder select', 'Please select a folder'))

### extracting the folder path
server = function(input, output, session) {
    volumes <- getVolumes()
    shinyDirChoose(input, 'directory', roots=volumes, session=session)
    path1 <- reactive({
       return(print(parseDirPath(volumes, input$directory)))
    })

### constructing the 3 file paths
datpat <- renderText({
    req(nchar(path1())>0)
    datpat <- paste0(path1(),"/data.csv")
  })
vispat <- renderText({
    req(nchar(path1())>0)
    vispat <- paste0(path1(),"/visit.csv")
  })
statpat <- renderText({
   req(nchar(path1())>0)
   statpat <- paste0(path1(),"/statvisit.csv")
})

So now I have these paths, but how can I use them to upload the related content to the server? A simple read.csv unfortunately does not do the trick.

EDIT - but not there yet...

Working further with the great help @SBista provided, I think I'm approaching my goal, but see below code...

volumes <- getVolumes()
shinyDirChoose(input, 'directory', roots=volumes, session=session)
path1 <- reactive({
  return(print(parseDirPath(volumes, input$directory)))
})

observe({
  if(!is.null(path1)){
    ### vis1
    vis1 <- reactive({
      datpat <- paste0(path1(),"/visit.csv")
      vis <- read.csv(datpat, header = input$header, sep = input$sep, quote = input$quote,
                      stringsAsFactors = FALSE)
      vis
    })
    ### dataruw1
    dataruw1 <- reactive({
      datpat <- paste0(path1(),"/data.csv")
      dataruw <- read.csv(datpat, header = input$header, sep = input$sep, quote = input$quote,
                          stringsAsFactors = FALSE)
      dataruw
    })
  }
})

Unfortunately, dataruw1 and vis1 do not seem to be generated, as I get a 'could not find function' error when trying to use the actual data with dataruw1() and vis1(). What am I missing? Any idea? Thanks a lot in advance!

like image 519
Jeroen Speybroeck Avatar asked Jun 08 '17 13:06

Jeroen Speybroeck


2 Answers

I have modified your app to demonstrate that you can use read.csv to upload the file. For demonstration I am reading only one file from the folder and displaying the read data frame in a datatable.

  library(shiny)
  library(shinyFiles)

  ### ui end, to browse to desired folder
  ui = fluidPage(shinyDirButton('directory', 'Folder select', 'Please select a folder'),
                 tableOutput(outputId = "datpat")
                 )

  ### extracting the folder path
  server = function(input, output, session) {
    volumes <- getVolumes()
    shinyDirChoose(input, 'directory', roots=volumes, session=session)
    path1 <- reactive({
      return(print(parseDirPath(volumes, input$directory)))
    })

    ### constructing the 3 file paths
    observe({
      if(!is.null(path1)){
        output$datpat <- renderTable({
          req(nchar(path1())>0)
          datpat <- paste0(path1(),"/data.csv")
          dat <- read.csv(datpat)
          dat
        })

      }
    })

  }

  shinyApp(ui = ui, server = server)

Hope it helps!

like image 134
SBista Avatar answered Sep 22 '22 21:09

SBista


Happy to have found a solution! Big thanks to SBista once more, even though I did it a bit differently. The below code is not just a chunk (as in my original post) but fully functional, provided you browse to a folder that contains a data.csv file.

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='"')),
           mainPanel(
             fluidRow(
               column(6, tags$b(textOutput("text")))),
             tags$hr(),
             fluidRow(
               column(6, dataTableOutput("table"))
             )
           )
  )
)

server = function(input, output, session) {

  volumes <- getVolumes()
  shinyDirChoose(input, 'directory', roots=volumes, session=session)
  path1 <- reactive({
    return(print(parseDirPath(volumes, input$directory)))
  })

  dataruw1 <- eventReactive(input$directory, {
    datpat <- paste0(path1(),"/data.csv")
    dataruw <- read.csv(datpat, header = input$header, sep = input$sep, quote = input$quote,
                        stringsAsFactors = FALSE)
    dataruw
  })

  output$text <- renderText({
    path1()
  })

  output$table <- renderDataTable({
    dataruw1()
  })

}

shinyApp(ui = ui, server = server, options = list(launch.browser=TRUE))
like image 24
Jeroen Speybroeck Avatar answered Sep 22 '22 21:09

Jeroen Speybroeck