Where to put a local data directory?

79 views
Skip to first unread message

Ken Takagi

unread,
Oct 22, 2014, 3:41:25 PM10/22/14
to shinyap...@googlegroups.com
I'm attempted to deploy my first app on shinyapps.io.  In the app, the user selects a file from a drop down menu, and the data file is plotted.   I am confused where to put the directory with the data so that the app can access it; I keep getting the "ERROR: Cannot change working directory" message.

My directory is like so:  server.R, ui.R and data directory (called MyData) are located in /MyShinyApp.  When I deploy the app using deployApp() (which works fine), a directory called shinyapps is created in /MyShinyApp, with a subdirectory /MyUsername which contains a MyShinyApp.dcf file.  I am using the follow relative path to tell the app where my data is: setwd("./MyData").  Any suggestions on where to move the MyData directory so that my app can access it?

Thanks in advance.




   

Ken Takagi

unread,
Oct 22, 2014, 7:52:07 PM10/22/14
to shinyap...@googlegroups.com
After searching the showLogs() for my app, I was able to figure out what was going on.  My data directory was in the right location, and it was being uploaded to the server, but for some reason using relative file paths to change the working directory was not work for me.  

Using the absolute file path setwd("/srv/shiny-server/MyApp/MyData") worked, but relative filepaths setwd("./MyData") did not work.  Its a little strange because when I run deployApp(), the function warnings me that I'm using absolute paths, and I should use relative file paths instead.  Not sure if this is a bug or not.

john...@operamail.com

unread,
Oct 23, 2014, 8:18:51 AM10/23/14
to shinyap...@googlegroups.com
I use relative paths on sShinyApps without a problem, but I don't reset the working directory. Instead I refer to the data as "./MyData/MyFile.txt"

Fereshteh Karimeddini

unread,
Oct 23, 2014, 11:03:57 AM10/23/14
to shinyap...@googlegroups.com
Ken,

setwd() should work, depending on how you use it.
Do you mind sharing your code with us?

Thanks,
Fereshteh

Ken Takagi

unread,
Oct 23, 2014, 12:42:17 PM10/23/14
to shinyap...@googlegroups.com
Hi Fereshteh,
I've attached a .zip that has the ui.R, server.R, a directory with functions ("functions"), and the directory with the data ("IsoData"), which actually contains a subfolder with the actual data file.  I've only included 4 data files, but typically I have 100s.  Hopefully my scripts aren't too confusing.   Let me know if you need more explanation.  I was able to get this to run locally, so it should work.

I've commented the lines where I use setwd() or sources() (for calling the functions).  Right now they are set to the absolute filepaths I mentioned in my earlier post (ie. /srv/...). 
IsoApp_Debug.zip

Ken Takagi

unread,
Oct 23, 2014, 12:46:58 PM10/23/14
to shinyap...@googlegroups.com
I have hundreds of data files that I need summarize/plot, I keep them in a data folder that I access with setwd(), then access each one individually using read.table(). 

setwd() doesn't seem to be the problem, it seems to be the use of relative vs. absolute file paths.

Ken Takagi

unread,
Oct 23, 2014, 12:50:41 PM10/23/14
to shinyap...@googlegroups.com
Sorry, there was a slight error in the filepaths for setwd() and source().  I've corrected it and re-attached the zip.  This shouldn't change the behavior I was experiencing though.
IsoApp_Debug.zip

Fereshteh Karimeddini

unread,
Oct 23, 2014, 1:45:33 PM10/23/14
to shinyap...@googlegroups.com
Ken,

Thanks for sending the files.

I did a quick check, but didn't have time to go through it thoroughly. 

When I run your application locally, after changing the absolute paths to relative paths, I see the error on changing directory. So this is not a problem only on shinyapps. Do you not see any errors running locally?
This points to the fact that your current directory changes during the application run. I added a little output of getwd() to the first page, and verified that your current directory in fact changes. 
So it seems that the function(s) that set the directory to a directory within your application gets called again. The first time the call is successful, but any subsequent time, this will cause error as your current directory has already changed. So setwd with absolute path will always work, but with relative path will only work once, unless you set it back to the top level. I didn't get deep into your code, and can't tell where/how to set it back. You would know better. Or alternatively, do not use setwd(), but access the data and function files using a relative path.

Hope this helps!

Best,
Fereshteh

Ken Takagi

unread,
Oct 23, 2014, 5:13:24 PM10/23/14
to shinyap...@googlegroups.com
Thanks for looking it over.  Now it makes more sense.  I may just access the data using relative paths in the read.table() function rather than changing the working directory.  It seems to cause more trouble than it's worth.

Thanks again!
Reply all
Reply to author
Forward
0 new messages