Error "cannot open the connection" in executing "knit HTML" in RStudio

50,510

Solution 1

When you run "Knit HTML", the code is trying to find the file you're reading in the same directory where .Rmd is located because knitr sets the working directory to that path. As far as I see you have two options.

  • Try specifying the absolute path to the file (not very robust, but handy in some cases).
  • Figure out the relative path to the file. If you have your .Rmd file in / and data in /data, relative path should be, e.g., read.table("./data/myfile.csv"...). . means "here" (wherever the getwd() is), two dots climb the directory structure up while specifying directories climbs the structure down.

Solution 2

I don't know since when this is part of the global options, but as I just stumbled over it and its not written here: under global options -> Markdown, there is a setting: "evaluate chunks in directory" and when you use "current" or "Project" this worked at least for me (apparently it is set to "document" by default)

Solution 3

Sometimes it is annoying for the executing path of Rmd file, especially when rmd file is not store in the root folder of a project. I normal store rmd in the Report folder to avoid all temp files in the project root (e.g. Report/myreport.Rmd).

For example, there is a file myfile.csv in the Resources folder. In the rmd file, I need to use two dots to specify the file path:

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

But the file path is not correct if I want to test my code in the console of a Rstudio project as the normal working directory is root folder of the project. So I need to remove two dots from file path:

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

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

library(rproject)
read.csv(project_filepath('Resources/myfile.csv'))

Solution 4

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

enter image description here

And in the folder scripts you have some *.Rmd (*.rmd) file or some *.R (*.r) script that you would like to knit/compile as HTML report (CTRL + SHIFT + K from RStudio), then you have the options:

1) For the *.Rmd file only, there is the option to define the path to your working directory at the top of the file (see the "Note" section in the help page of 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 the absolute path; though not encouraged if you will 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 fail on yours if you move my_project)

```{r setup, include=FALSE}
knitr::opts_knit$set(root.dir = 'absolute/path/to/my_project/')
```

2) For both the *.R script and the *.Rmd file (though not encouraged in the "Note" section of the help page of knitr::knit) - you can put at the top of your *.R script or in the code chunk of 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 compiling (say when testing the script), it will not alter the usual getwd() path since it cannot find the /scripts pattern. When compiling to HTML it will edit the working directory path by deleting the /scripts part from path/to/my_project/scripts

Both these options will allow you to keep on using relative paths like:

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 scritps before compiling them to HTML reports.

Share:
50,510
Praveen Kishor
Author by

Praveen Kishor

Keep Cool and CODE!!

Updated on July 09, 2022

Comments

  • Praveen Kishor
    Praveen Kishor almost 2 years

    I am getting the below error 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.