rstudio / learnr

Interactive Tutorials with R Markdown
https://pkgs.rstudio.com/learnr
Apache License 2.0
705 stars 236 forks source link

Data files on shiny server #99

Closed nlhuong closed 2 years ago

nlhuong commented 7 years ago

When running tutorials locally one can set the path to whatever directory in the local machine and then read files from that directory.

However, when one publishes the tutorial on a server, it is not clear how to access the data files (external sources) that have been uploaded together with the Rmd tutorial file. The default current directory when calling getwd() in an exercise chunk is not the directory of the tutorial Rmd.

Please provide a solution so that either the working directory is by default the directory containing the Rmd tutorial file or another solution to call the data files from inside the chunks without having to input the whole absolute path to the data.

jjallaire commented 7 years ago

Exercise code does indeed run in an isolated directory (that's a feature!) so that it can't inspect the tutorial and it's related files. So I'd say having code read data from the tutorial source directory just won't work.

nlhuong commented 7 years ago

So, is there any solution for writing tutorials uploaded on a server where exercises can ask students to read the data from file, e.g. a csv file?

jjallaire commented 7 years ago

I'd have them read the file from a URL

dtkaplan commented 7 years ago

I've found it convenient for large files just to create a package containing the data. Learnr happily works with this.

On Fri, Jun 16, 2017 at 2:39 PM, Lan Huong Nguyen notifications@github.com wrote:

So, is there any solution for writing tutorials uploaded on a server where exercises can ask students to read the data from file, e.g. a csv file?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/rstudio/learnr/issues/99#issuecomment-309116619, or mute the thread https://github.com/notifications/unsubscribe-auth/AAggrQmEL0el4Yp_tzQu3WerlFM23ofSks5sEtnmgaJpZM4N8z8w .

--

...

DeWitt Wallace Professor of Mathematics, Statistics, and Computer Science Macalester College

samuelhuerga commented 6 years ago

It would be nice to have a directory where files could be readable.

I'm trying to create a tutorial on reading different data formats, but no file is accesible. I would want to achieve something like: https://campus.datacamp.com/courses/importing-data-in-r-part-1/importing-data-from-flat-files-with-utils?ex=2

Is there any directory where I can place my archives that could be read?

rblissett commented 6 years ago

Side note that importing data, whether it's from a package or URL doesn't seem to work if you set the exercise lines to be less than three. Unsure why - very interesting.

pickardpredictives commented 5 years ago

It would be nice to have a directory where files could be readable.

I'm trying to create a tutorial on reading different data formats, but no file is accesible. I would want to achieve something like: https://campus.datacamp.com/courses/importing-data-in-r-part-1/importing-data-from-flat-files-with-utils?ex=2

Is there any directory where I can place my archives that could be read?

I just wanted to second this request. I'm trying to teach my students (accounting majors - not at all R savvy) to load data and it would be nice to have my tutorial on the web.

reuning commented 5 years ago

I'd like to third this. Right now I have a workaround but it is ugly and needs to be run for each exercise.

{r mean-and-median-setup}
tmp.df <- read.csv("https://www.kevinreuning.com/assets/state_party.csv")

write.csv(tmp.df, "state_party.csv", row.names=F)
rm(list=ls())
ttimbers commented 4 years ago

I would like to fourth this issue...

tataphani commented 4 years ago

Any one found a solution to this problem yet ? I am creating tutorials for my friends so I do not have a webspace available , how can I do it , Can I create dataset itself in the tutorials and then use it later ?

reuning commented 4 years ago

You could use a dropbox, have the data there, and then grab it as I described here https://github.com/rstudio/learnr/issues/99#issuecomment-500462786

JosiahParry commented 3 years ago

Perhaps not directly related to Shiny Server, but the issue is similar.

There seems (from what I can surmise from documentation) to be no ability to use local files inside of a learnr tutorial. For example if I have the structure

tutorials/
    - reading_data.Rmd
    data/
        - example.csv

I would expect to be able to incorporate a line such as readr::read_csv("data/example.csv") in the code itself. Given that the code is ran in an isolated directory, would it be feasible to be able to copy the files being read to the temp directory? I believe that is what @reuning is doing in essence with their setup chunk.

One of the things that I am trying to teach with this example is reading from relative paths. I think this is also something that others are trying to address as well.

TL;DR adding my vote for the ability to directly read files from a learnr tutorial.

allisonhorst commented 3 years ago

Adding my vote here, this would be very helpful!

gadenbuie commented 2 years ago

Thank you everyone for your input in this issue. We just added a new feature that makes it possible to include files when stored as described by @JosiahParry above.

There are now three ways authors can include files for use in exercises:

  1. Store the files in a data/ directory in the same directory as the tutorial.

  2. Use the setup chunk to download or write the files to a data/ directory.

  3. Use the tutorial.data_dir global option or the TUTORIAL_DATA_DIR environment variable to specify a path to a data directory.

More information can be found online in the learnr documentation in Exercises - Using Files in Exercises.

derekpowell commented 2 years ago

I am trying to use this feature by storing my data in a data/ folder but finding it does not work. Trying to access a file in the path data/stroop-2014.csv, I get the error:

'data/stroop-2014.csv' does not exist in current working directory ('/private/var/folders/_g/mndhmt091j90xgl5ppfk0hbr0000gn/T/RtmpOaM0D8/learnr-tutorial-exercise160b049630662').

The autocomplete suggestions can see the data folder and the file in it, but I can't get it to actually execute correctly in R. A reprex is a bit trick b/c of files, but can try to provide something if needed

NaturallyAsh commented 1 year ago

I get the same error as @derekpowell . Any resolution to this? I'm using an M1 MacPro. I also wanted to note that my tutorial is bundled in a package. So, I have a data folder in my root. Not sure if that information is useful or not.

gadenbuie commented 1 year ago

So, I have a data folder in my root.

The data/ folder needs to be in the same folder as the tutorial source file. If that isn't the case, please try moving the data folder into the same folder as the tutorial source.

It's possible there's an edge case related to tutorials in R packages that we've missed, so please let me know if the above suggestion works. If you can link to the package you're working on, that could also help.

NaturallyAsh commented 1 year ago

I did create the data folder in the same directory as my tutorial file. These are my paths:

"tutorials/module_2/data/dataset.csv" 
"tutorials/module_2/lesson.Rmd" 

My entire course is bundled in a package, and that package root has a data folder. Below are the paths of both data folders:

"learnrIO/data/dataset.rds" 
"learnrIO/inst/tutorials/module_2/data/dataset.csv" 

I'm not sure if having a root data folder and tutorial level data folder causes so sort of interference?

gadenbuie commented 1 year ago

@NaturallyAsh is it possible for you to share the source of your package? If it's on GitHub, you could just share the link to the repo. If not, I'll try to come up with a reprex. Also, I recommend we open a new issue to track this rather than continuing in this old thread. Would you like to open the new issue?