Widget in Shiny to take a file path

Asked

Viewed 223 times

3

In an application here from work I use the widget below for the user to load a database and it is 'criticized' by another script.

 fileInput("file1", "Escolha o arquivo",
                multiple = FALSE,
                accept = c("text/csv",
                           "text/comma-separated-values,text/plain",
                           ".csv",".xlsx"))

In another application I need the user to provide the path where the files are to be analyzed. I know I can change the parameter multiple = TRUE and will work similar to what I want, but would like widget that would ask the path of files!

1 answer

4


The in fact does not have a function for it. To circumvent this "restriction" of shiny the package was created shinyFiles.

This package has two functions to assist in this mission:

  1. shinyDirButton: goes to the UI and creates the button;
  2. shinyDirChoose: goes to the server and "picks up" the input passed by the user.

An application that uses the module looks like this:

library(shiny)
library(shinyFiles)

dir.create("teste")

ui <- fluidPage(
  br(),
  p("Escolha uma pasta"),
  shinyDirButton("pasta", "Escolher", "Escolha uma pasta"),
  hr(),
  verbatimTextOutput("dir")
)

server <- function(input, output, session) {
  # chama o módulo
  shinyDirChoose(input, 'pasta', roots=c(wd='.'))

  # torna resposta reativa
  pasta <- reactive(input$pasta)

  # joga resposta no output
  output$dir <- renderPrint( pasta() )
}

shinyApp(ui, server)

Home screen:

inserir a descrição da imagem aqui

Modal after pressing the button:

inserir a descrição da imagem aqui

Result after choosing the folder "test": inserir a descrição da imagem aqui

Note that to use the value passed by the user you will need to manipulate pasta()$path. Usually its first element is negligible ("") and therefore the past value in fact is pasta()$path[-1].

  • 1

    That’s what I was looking for, I will adapt to my application, thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.