Set location to save .csv files and graphs?

3,710 views
Skip to first unread message

Ashlinn Quinn

unread,
Jun 4, 2013, 1:08:35 PM6/4/13
to shiny-...@googlegroups.com
Hello, I am building a Shiny app that will process a user-supplied input file, outputting graphs and summaries. Some of these graphs and summaries will be viewed in the Shiny UI, but I would also like to save them to the user's hard drive - preferably, to a user-specified file directory that is NOT the location of the shiny app. 

As of now, I am able to save files in the working directory (which is the location of the shiny app) using the below "saved_tables <- observe()" statement within my reactive Data() function, but would like to specify that the output be saved elsewhere. Ideally, the user would specify where to save the files on their hard drive. I have thought of placing a setwd() command within the saved_tables code, but this would only work if the user could actually specify the directory themselves somehow. Any ideas?


###### server.R ##############

shinyServer(function(input, output) {

  Data <- reactive({

            ####### a bunch of data processing here ########

saved_tables <- observe({
    write.csv(data_summary, file = "Data_summary.csv", sep = ",")    ### files are saved in working directory (location of shiny app) ###
    write.csv(minute_averages, file = "Data_Minute_Averages.csv", sep = ",") 
    write.csv(hour_averages, file = Data_Hour_Averages.csv", sep = ",")
   })

info <- list(ID = ID, summary = data_summary, minutedata = minute_averages, hourdata = hour_averages)
    return(info)
})

### continue to process output for shiny UI  ### 

Joe Cheng

unread,
Jun 4, 2013, 3:24:33 PM6/4/13
to shiny-...@googlegroups.com
You can just give the file argument a full path--it doesn't have to be a bare filename. And you can use file.path to construct the full path.

Also, you want the saved_tables observer to be defined *outside* of the reactive, otherwise multiple observers will be created that all do the same thing.

Something like this:

Data <- reactive({
  # ...
  list(ID = ID, summary = data_summary, minutedata = minute_averages, hourdata = hour_averages)
})

observe({
  dir <- '/foo/bar'  # wherever you want the files to end up
  write.csv(Data()$data_summary, file = file.path(dir, "Data_summary.csv"), sep = ",")
  write.csv(Data()$minute_averages, file = file.path(dir, "Data_Minute_Averages.csv"), sep = ",") 
  write.csv(Data()$hour_averages, file = file.path(dir, "Data_Hour_Averages.csv"), sep = ",")
})



--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Ashlinn Quinn

unread,
Jun 5, 2013, 8:51:44 AM6/5/13
to shiny-...@googlegroups.com
Thanks! It works as you said, if I specify the filepath for the  "dir" myself in server.R. 
But now I'm trying to figure out how the user can input the filepath for the "dir" on the UI side. The below code is not working - it gives me an error:

 "Warning in file(file, ifelse(append, "a", "w")) :
  cannot open file '/_Data_Summary.csv': Permission denied
Error in file(file, ifelse(append, "a", "w")) : 
  cannot open the connection"

Ideas?



#### ui.R ###
shinyUI(pageWithSidebar(

  textInput("dir", "Paste the filepath to save the output"),
    
),
# etc.


#### Server.R ###

Data <- reactive ({
## data processing

 info <- list(ID = ID, summary = summary)
    return(info)
  })
  
  
  observe({
  
  dir <- input$dir
  if (is.null(dir))
    return(NULL)

  write.csv(Data()$summary, file = file.path(dir, paste(Data()$ID,"_Data_Summary.csv", sep = "")), row.names = F)
})

Ashlinn Quinn

unread,
Jun 6, 2013, 10:40:24 AM6/6/13
to shiny-...@googlegroups.com
Hi - update, I've come up with a solution that works with directory names entered on a Mac, but I doubt it will be platform-independent. If anyone has ideas on how to make this work for both Mac and Windows, I would be very appreciative!


#### ui.R ####

shinyUI(pageWithSidebar(
  sidebarPanel(
        textInput("dir", "Paste the filepath to the location where you would like to save the output"),   # user enters the filepath to desired folder here
        ),
         # etc.
 
#### server.R ####

shinyServer(function(input, output) {
  
  Data <- reactive({
    
    dir <- input$dir

     # data processing, etc.

     info <- list(dir = dir, ID = ID, summary = summary, minutedata = minute.average, hourdata = hour.average)
    return(info)   
)}

 observe({
      dir <- if (is.null(Data()$dir)) { dir <- getwd() } else dir <- Data()$dir             # this line seems to keep the code from stopping due to initial lack of "dir" name

      write.csv(Data()$summary, file = file.path(dir, paste(Data()$ID,"_Data_Summary.csv", sep = "")), row.names = F)    # files are saved to folder specified in input$dir
      write.csv(Data()$minutedata, file = file.path(dir, paste(Data()$ID, "_Data_Minute_Averages.csv", sep = "")), row.names = F) 
      write.csv(Data()$hourdata, file = file.path(dir, paste(Data()$ID,"_Data_Hour_Averages.csv", sep = "")), row.names=F)
    })
  
})

Joe Cheng

unread,
Jun 6, 2013, 11:23:16 AM6/6/13
to shiny-...@googlegroups.com
It seems extremely sketchy to let the user just plop files anywhere on the server that they feel like. Can you talk a little more about your scenario? What will they want with these files, how will they access them later?

Ashlinn Quinn

unread,
Jun 6, 2013, 12:10:39 PM6/6/13
to shiny-...@googlegroups.com
Hi - the scenario is that the shiny app that I'm writing is for a collaborative project between researchers in New York and Ghana. The data is being collected in Ghana and the app is to enable the Ghana field staff to easily clean and process the raw data files, saving cleaned output files and plots to a location (currently in a shared Dropbox folder) that is accessible to the entire research team. There will be many, many files generated, so I would like the staff to be able to organize them within folders (most likely one folder for the output from each raw data file). Saving them to the working directory (location of the shiny app files) is not a good solution as I'd like to keep that directory pristine. At the least, I'd like to be able to specify a particular location in the Dropbox folder where the output files will be saved, but have the user able to point to this location from his/her own specific computer.


--
You received this message because you are subscribed to a topic in the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/shiny-discuss/MaN4-ia6wfk/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to shiny-discus...@googlegroups.com.

Keith Doyle

unread,
Nov 1, 2014, 11:51:11 AM11/1/14
to shiny-...@googlegroups.com
 
Maybe this is way over simplified, why not use setwd("dir")
 
Reply all
Reply to author
Forward
0 new messages