Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Shiny request user to choose directory

Tags:

r

shiny

I am new to R and R Shiny.

For the code i have at the moment i need to manually input the file name, i would like to generalize the case and let the user to pick working directory and corresponding file name.

1, user choose working directory then shiny able to store all the file names under the selected working directory. similar to list.files()

2, then the box list files will list all file names under the selected wd and user able to check which dataset should be shown

3, in the mainpanel top 10 instances of the dataset with the header will be shown

What i have tried is

server.R

library(shiny)
setwd("C:/Users/HKGGAIT001/Google Drive/GA Project/Cargo/Cargo.Statistics/data/Hactl")
data1 <- read.csv(list.files()[1])
data2 <- read.csv(list.files()[2])

# Define server logic required to summarize and view the selected
# dataset
shinyServer(function(input, output) {

  # Return the requested dataset
  datasetInput <- reactive({
    switch(input$dataset,
           "data1" = data1,
           "data2" = data2)
  })

  # Generate a summary of the dataset
  output$summary <- renderPrint({
    dataset <- datasetInput()
    summary(dataset)
  })

  # Show the first "n" observations
  output$view <- renderTable({
    head(datasetInput(), n = input$obs)
  })
})

ui.R

library(shiny)

# Define UI for dataset viewer application
shinyUI(fluidPage(

  # Application title
  titlePanel("Shiny Text"),

  # Sidebar with controls to select a dataset and specify the
  # number of observations to view
  sidebarLayout(
    sidebarPanel(
      selectInput("dataset", "Choose a dataset:", 
                  choices = c("data1", "data2")),

      numericInput("obs", "Number of observations to view:", 10)
    ),

    # Show a summary of the dataset and an HTML table with the 
     # requested number of observations
    mainPanel(
      verbatimTextOutput("summary"),

      tableOutput("view")
    )
  )
))

enter image description here

The situation is similar to This website while my case is request user to pick local working directory.

Thanks for your gentle help

like image 867
useR Avatar asked Aug 25 '14 08:08

useR


1 Answers

First, create the .csv files to reproducibility:

write.csv(x = data.frame(V1 = 1:13, V2 = letters[1:13]),
          file = "teste1.csv", row.names = FALSE)

write.csv(x = data.frame(V1 = 14:26, V2 = letters[14:26]),
          file = "teste2.csv", row.names = FALSE)

write.csv(x = data.frame(V1 = rnorm(15), V2 = runif(15)),
          file = "teste3.csv", row.names = FALSE)

Add a global.R script in your app might be useful. In this script you would be able to:

i. let the user select the working directory,

ii. read the .csv files in that folder,

iii. create a list of files that could be used by ui.R and server.R

# global.R
library(shiny)

wd <<- choose.dir()
setwd(wd)

csv <<- list.files(pattern = ".csv")

files <<- vector("list", length(csv))
for (i in seq_along(files)) {
  files[[i]] <- read.csv(csv[i], stringsAsFactors = FALSE)
}

list_of_datasets <<- seq_along(files)
names(list_of_datasets) <- gsub(pattern = ".csv", replacement = "", x = csv)

Then you just have to make a few changes in the original scripts you provided us. In ui.R I would redefine the selectInput function so that displays the name of the files to the users. Also, you can't be sure that the selected folder would have 2 files.

selectInput("dataset", "Choose a dataset:", 
            choices = list_of_datasets)

In server.R you should i) remove the 2nd, 3rd and 4th lines (already handled by global.R) and ii) change datasetInput function:

datasetInput <- reactive({
  files[[as.numeric(input$dataset)]]
})
like image 175
Tomás Barcellos Avatar answered Sep 17 '22 10:09

Tomás Barcellos