"Cannot open connection" error while executing "knit HTML" in RStudio

I get the error below when trying to "knit HTML" in RStudio.

|................................ | 50% ordinary text without R code |.................................................................| 100% processing file: Preview-b0c112a265.Rmd label: unnamed-chunk-1 Quitting from lines 16-26 (Preview-b0c112a265.Rmd) Error in file(file, "rt") : cannot open the connection Calls: <Anonymous> ... withVisible -> eval -> eval -> read.csv -> read.table -> file Execution halted 

I am using RStudio on a 64 bit win8 machine.

+16
r rstudio knitr
source share
6 answers

When you run Knit HTML, the code tries to find the file you are reading in the same directory where .Rmd is located, because knitr sets the working directory to this path. As far as I can see, you have two options.

  • Try to specify the absolute path to the file (not very reliable, but convenient in some cases).
  • Highlight the relative path to the file. If you have a .Rmd file in / and data in /data , the relative path should be, for example, read.table("./data/myfile.csv"...) . . means "here" (wherever getwd() is located), two points go up in the directory structure, and directory pointers go down.
+19
source share

Sometimes this annoys the execution path of the Rmd file, especially when the rmd file is not stored in the project root folder. I usually save rmd in the Report folder to avoid all the temporary files in the root of the project (e.g. Report / myreport.Rmd).

For example, in the resources folder there is a file myfile.csv. In the rmd file, I need to use two points to indicate the path to the file:

 read.csv('../Resources/myfile.csv') 

But the file path is incorrect if I want to test my code in the Rstudio project console, since the usual working directory is the project root folder. Therefore, I need to remove two points from the file path:

 read.csv('Resources/myfile.csv') 

I wrote a simple function to solve this problem for myself ( https://github.com/byzheng/rproject ). The project_filepath function will generate a new path relative to the project root folder. Thus, the working directory can be any subfolder in the project. The code below will work for the Rmd file and console.

 library(rproject) read.csv(project_filepath('Resources/myfile.csv')) 
+5
source share

The following works for me - if you have your project (say a directory called my_project ), organized something like this:

enter image description here

And in the scripts folder you have some *.Rmd ( *.rmd ) or *.R ( *.r ) script that you want to link / compile as an HTML report ( CTRL + SHIFT + K from RStudio), then you have there are options:

  • 1) Only for the *.Rmd file, it is possible to determine the path to your working directory at the top of the file (see the note in the help knitr::knit ):
 '''{r setup, include=FALSE} knitr::opts_knit$set(root.dir = '../') # Or use multiple '../' if needed; # One '../' means go one level up towards the root, # here, moving from 'scripts' folder to the root 'my_project' ''' 

or use an absolute path; although this is not recommended if you share your directory / repository with colleagues (on Windows, something like C: /my/absolute/path/to/my_project will not work on any other computer and will also not work on your if you move my_project )

 '''{r setup, include=FALSE} knitr::opts_knit$set(root.dir = 'absolute/path/to/my_project/') ''' 
  • 2) As for the *.R script and the *.Rmd file (although this is not recommended in the knitr::knit help knitr::knit ) - you can put it at the beginning of your *.R script or in the code fragment your *.Rmd file ( where you read some data) something like:
 setwd(gsub(pattern = '/scripts', replacement = '', x = getwd())) 

If you run / execute the *.R script without compilation (say, when testing the script), it will not change the usual getwd() because it cannot find the /scripts template. When compiling in HTML, it will edit the path to the working directory, removing the /scripts part from path/to/my_project/scripts

Both of these options allow you to use relative paths, such as:

 read.csv('data/my_data.csv') 

avoiding something like:

 read.csv('../data/my_data.csv') 

which can be tricky if you want to test your scripts before compiling them into HTML reports.

+1
source share

You need to set the absolute paths or relative to your project folder, as other authors mention. You can also setwd (path).

But that is not enough for me. For some reason, I found that I need to load all my data into the very first block of R commands in the .Rmd file, otherwise I get the same as you.

In other words:

 ```{r} setwd("/tmp/report") # This load works data1 <- read.csv("your_file.csv", sep = "\t") ```` some markdown text here ... ```{r} # This load does not work, even if I do a setwd just before: data1 <- read.csv("your_file.csv", sep = "\t") ```` 
0
source share

For me, it was just a case when I did not save my .Rmd file yet ... As mentioned above, the code tries to find the file in the same directory where .Rmd is located, and .Rmd doesnโ€™t exist, you may get this error .

0
source share

Since then, I do not know when this is part of the global options, but as I just stumbled upon it, and it is not written here: in global options โ†’ Markdown, there is a setting: "evaluate portions in the directory" and when you use " current "or" Project "this worked at least for me (apparently," document "is set by default)

0
source share

All Articles