RMarkdown errors

Hello there, I am trying to get my RMarkdown to work. I have run the code in R and all of it works. I've executed it before hitting the knit option and then it says the following:
Error in file(file, "rt" : cannot open the connection
Calls: ... eval_with_user_handlers -> eval -> exal -> read.csv -> Execution halted

I have ```{r setup, include=FALSE}
knitr::opts_chunk$set(eval = TRUE, echo = TRUE, results = "hide")

The screenshot is not legible, so I can't tell where the problem is. Cutting and pasting the code is a better idea.

means that either the read.csv file does not exist or it's not in the working directory.

A good way to cut down on this is

  1. Use RStudio projects
  2. Create a data directory under the project folder
  3. Install the {here} library
  4. Have library(here) in the startup chunk
  5. Then you can use `read.csv(here("data/my_file")) with confidence that it will be found

Hey there @technocrat,

Sure thing. I'll post the code I have in order up until the error message. I do thing that it might be read.csv not in the working directory but which directory do I chose for the project? Go off of existing directory? Version control?

Here's the in between code so I don't have to type everything out.

knitr::opts_chunk$set(eval = TRUE, echo = TRUE, results = "hide")
library(tidyverse)
library(readr)
library(janitor)
library(dplyr)
library(lubridate)
Trips_2021_09 <- read.csv("202109-divvy-tripdata.csv")

Thank you in advance!

In the console, type dir() to see if your csv file is in your current working directory (it probably isn't).

From the RStudio menu, use File | New Project to create a project folder and put the csv and your Rmd file there. The from the menu, open the project and if necessary the Rmd. Use dir() again to confirm that the csv file is actually there. If it is, the read.csv will work as you've written it.

@technocrat

I tried creating a new file and then went back to use dir() and it had the csv files in there, but it still gave me the same error message when I tried knitting the file. I'm very new to R, sorry if I misunderstand what you're suggesting.

And now my file is all messed up. When I click the file on my computer, it has none of the saved progress but just the files. No script or anything. I have to load it differently in r. That makes no sense why read.csv wouldn't work in RMarkdown, that's silly on R's part.

1 Like

Try creating a new Rmd document and make the first chunk look similar to this
{r setup, include=TRUE} knitr::opts_chunk$set(echo = TRUE) my_data <- read.csv("chunk_1.csv") my_data[1:5,1:5]

In the screenshot in the lower right pane you can see the second file is the same one read in. Can you do that?

1 Like

Hey there @technocrat, I can do that. Do I need to do that same code for every document I'm transferring? I have twelve so should I just do like:

Trips_2021_09 <- read.csv("202109-divvy-tripdata.csv")
Trips_2021_09[1:5,1:5]
Trips_2021_10 <- read.csv("202110-divvy-tripdata.csv")
Trips_2021_10[1:5, 1:5] # And continue to go till I have all 12 files loaded?
...

And what exactly does the [1:5,1:5] mean? I can try this and see if it works. And just not have to output for the original code I had in my markdown.

just takes the first five rows and columns of the my_data for the purposes of illustration; don't use that for your data unless you want just a portion defined by position.

For doing this 12 times, it's a toss-up between repeating and writing a little script to automate if this is a one-off. If you are going to be doing it frequently comeback and I'll show you how to automate.

Hey @technocrat,

I tried this and received the same error message.

knitr::opts_chunk$set(echo = TRUE)
Trips_2021_09 <- read.csv("202109-divvy-tripdata.csv")
Trips_2021_10 <- read.csv("202110-divvy-tripdata.csv")
Trips_2021_11 <- read.csv("202111-divvy-tripdata.csv")
Trips_2021_12 <- read.csv("202112-divvy-tripdata.csv")
Trips_2022_01 <- read.csv("202201-divvy-tripdata.csv")
Trips_2022_02 <- read.csv("202202-divvy-tripdata.csv")
Trips_2022_03 <- read.csv("202203-divvy-tripdata.csv")
Trips_2022_04 <- read.csv("202204-divvy-tripdata.csv")
Trips_2022_05 <- read.csv("202205-divvy-tripdata.csv")
Trips_2022_06 <- read.csv("202206-divvy-tripdata.csv")
Trips_2022_07 <- read.csv("202207-divvy-tripdata.csv")
Trips_2022_08 <- read.csv("202208-divvy-tripdata.csv")

What do I do now?

In the RStudio lower right hand frame (the default, as shown on my screenshot) on the file tab, are all the files there?

I also had occasional issues with file locations. Suggestion by @technocrat for using here indeed helps.

Also helping in my case was looking at the output of getwd in the knitr environment.
Just add the following chunk to your .Rmd file:

```{r one-off}
# local folder for this knit action
getwd()
```

It shows what the knitting process considers the local map.
With this knowledge you can change the path of the csv files.

2 Likes

Sorry for the late reply @technocrat,

No, all the files aren't there on the under the home tab. Not even the folder shows up. I don't understand why R wouldn't just use the read.csv function in RMarkdown and make everything difficult.

Hi @HanOostdijk,

I tried this code, and it did nothing. Here's the message I received after running that chunk:

[1] "C:/Users/devan/OneDrive/Capstone Project/Cleaned Data"

I don't want it using the Cleaned Data file and I tried finding the folder that contains the files I'm using but couldn't.

You can do:

  1. Move (copy) the csv-files to the folder indicated by the chunk or

  2. Find out where the csv-files are in relation to this folder and adjust in your code the path of the files using relative addressing (where ../is the parent directory) or

  3. Specify the full path of the csv-files in your code.

This topic was automatically closed 42 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.