Error message file(file, "rt")

I am trying to open a dataset in R markdown but I keep getting an error. When I run the data path in just the R script, I am able to open the data but not in R Markdown using the same path. I don't know what I am doing wrong. Here is the code:

#Load the data


```{r}
turnout <- read.csv("Desktop/SOC 4500_Data Analysis/R Assignment Answers/turnout.csv")
data(turnout)
df <- turnout
names(turnout)

Hi, the RMarkdown relative paths are different. You could move the rmd file so that it is in the same directory as your script (if it isn't).

16.6 The working directory for R code chunks | R Markdown Cookbook (bookdown.org)

You could use the projects workflow or the here package.

2 Likes

The best way to work with RStudio is to use the File menu to create a new project at a convenient location on your computer. Many people like projects, Move the csv file to that folder and you will be able to use read.csv with just the file name.

2 Likes

I created a new project as you suggested and I was able to open the data this time. Thank you so much.

3 Likes

Great. If you find your project starting to get cluttered with mixed scripts and data files, consider creating a data data and installing the {here} package. Then within the project you can use

read.csv(here(data("myfile.csv"))

wherever your current working directory is located with your project.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.