Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading data files in blogdown

Tags:

r

csv

blogdown

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.

like image 984
Jean V. Adams Avatar asked May 27 '17 10:05

Jean V. Adams


People also ask

How do I load a dataset in R markdown?

You can click in the upper left menu File > Import Dataset > From Excel and select the file to import it. Then you can copy the code that appears in the R console with the code required for import the data in xlsx and then copy it in a R Markdown code chunk.

How do I read a csv file in R markdown?

Go to the Files tab in RStudio (lower right). Click Upload and browse to select the file you created. Then, use the read. csv() function to read in the file.


1 Answers

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.

like image 161
p0bs Avatar answered Sep 24 '22 22:09

p0bs