File path for calling data in Rmd file for blogdown

Hello everyone,

I'm fairly new to Rmarkdown and blogdown, so forgive me if this is a simple question. I have successfully used blogdown to post an Rmd file to my blog, which worked great! Now I would like to post an Rmd file which calls data, and I'm not sure how to do this.

I have the data saved on my computer, and I want to do something like:
train <- read.csv("~/data/train.csv")

where the file path is the link to the path on my computer. This didn't work, so I tried variations of putting the data in the static folder in the blogdown files, putting it in the public folder, putting it on github and linking to the blob/master/train.csv file, etc.

There must be an easy way to do this, but for the life of me I can't figure it out. Please help!

Thank you!

So people seem to deal with this differently. It can definitely be confusing, since a file path, such as the one you have above, essentially evaluates differently when you run it locally in the IDE, as opposed to when you build the post with serve_site(). (I recommend taking a look at the Paths of figures and other dependencies section of the blogdown book– getting a good grasp on that idea took me a beat, but definitely saved me future trouble).

Depending on your preference, you can use different approaches: run it locally and cache the data for the post or specific chunk (consistent with knitr cache features), you can do the remote file option (this should work fine, you just have to remember to use the raw url from GitHub), you can use the here package (see Jenny's tribute to here for a nice explanation, which builds the path to the file when you execute it.

Personally, I like to stash the data in a repo, or a gist, since it makes the analysis reproducible, and also means that I can easily update the post with new data, if I so choose.

Hopefully this helps.



In my /content folder (-> /post folder for posts, but your mileage may vary), each new post gets a new folder and an R Project, and not just an .Rmd file.
In this folder, I store the .Rmd file, the data, the .R code (if exists separately), as well as any pictures that aren't generated by my code.

Aside for the pictures (the path will need to be changed for blogdown), the rest should be resilient and knit well for blogdown as well as locally.

Thanks to @mara and @taras for your advice. It worked! I ended up storing the data locally for now because that seemed like a quick fix, but I will check out mara's other suggestions when I get the chance.

1 Like

You can also offload your data from a CSV file to a database server, and have your script or Rmd file call it from there.

It takes a bit longer to set up and get used to, but having your data live in a database has major benefits - not only cleaner file paths.

For discoverability, I'm linking to a blog post on the blogdown demo site, partially inspired by this question and @mara's answer!