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
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.
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.
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.
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?
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.
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.
Move (copy) the csv-files to the folder indicated by the chunk or
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
Specify the full path of the csv-files in your code.