5

I am a new user of blogdown using Hugo. I would like to create a new post that includes R code to read a data file.

The data file is in my static folder, local path C:\mydir\myblogdown\static\data\savedrecs.txt. Since I was successful in referring to an image using a relative path like this, ![](/images\myimage.jpg), I tried reading the data using something similar for the data file, read.csv("/data\savedrecs.txt"), but that didn't work.

I started playing around with the list.files() function to see if I could find a relative path that worked in my local version of the post, list.files("../../static/data") worked, showing me ## [1] "savedrecs.txt".

I tried searching around other folks' blogdown repos on Github to see how they might have referred to a data file, but the only example I found referred to a data file using a URL.

Jean V. Adams
  • 4,634
  • 2
  • 29
  • 46
  • 1
    I've managed to add a data (CSV) file to a blogdown post, by putting it in the same directory as the Rmd file and reading it via a Rmd chunk. Perhaps it is the location of your text file? – p0bs May 27 '17 at 10:36
  • 1
    You can certainly `read.csv("../../static/data/savedrecs.txt")` in Rmd. – Yihui Xie May 28 '17 at 09:22
  • 1
    Yes, @Yihui. I was avoiding doing that because I thought I wasn't supposed to refer specifically to "static" in the blogdown / Hugo world ... I thought things in "static" were supposed to end up in "public" ... or something like that. – Jean V. Adams May 30 '17 at 15:37

1 Answers1

6

I suspect that this may be due to the location of your data file. In my working example, the Rmd form of my blog post is in the directory, called p0bs/content/post. I also add my data file (a CSV in my case) to this directory.

I then treat the rest of the post as I would in a standard Rmarkdown website, with Rmd chunks (that are named without spaces). In your case, that code would include:

read.csv("savedrecs.txt")

I hope that helps you.

p0bs
  • 1,004
  • 2
  • 15
  • 22