Praveen Kishor
Praveen Kishor

Reputation: 2523

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

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.

Upvotes: 27

Views: 67041

Answers (7)

guyabel
guyabel

Reputation: 8366

I just hit this problem when working with project in a Dropbox file directory. I paused the Dropbox sync and no longer having a problem.

Upvotes: 0

Valentin_Ștefan
Valentin_Ștefan

Reputation: 6426

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.

Upvotes: 4

Julius Diel
Julius Diel

Reputation: 177

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)

Upvotes: 15

Roman Luštrik
Roman Luštrik

Reputation: 70643

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.

Upvotes: 31

dmt
dmt

Reputation: 2183

For me, it was simply a case of not saving my .Rmd file yet... As pointed out above, the code is trying to find the file in the same directory where .Rmd is located, and the .Rmd doesn't exist, you might get this error.

Upvotes: 0

Hugo Zaragoza
Hugo Zaragoza

Reputation: 601

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

But this is not enough for me. For some reason I find that i need to load all my data on the very first block of R commands in the .Rmd file, otherwise I get the same error 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")
````

Upvotes: 0

Bangyou
Bangyou

Reputation: 9816

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'))

Upvotes: 6

Related Questions