Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 442
Praveen Kishor Avatar asked Nov 18 '14 12:11

Praveen Kishor


People also ask

Why is Knit not working in R?

If a chunk works in R but not when you knit, it is almost always because you've changed a variable in your global working environment not using code in a chunk. Try restarting your R session and running each chunk sequentially to make sure all your variable values are up to date.

How do I Knit RStudio to HTML?

To transform your markdown file into an HTML, PDF, or Word document, click the “Knit” icon that appears above your file in the scripts editor. A drop down menu will let you select the type of output that you want. When you click the button, rmarkdown will duplicate your text in the new file format.

Why can't I Knit my R Markdown?

No Knit HTML button This means that RStudio doesn't understand your document is supposed to be an RMarkdown document, often because your file extension is . txt . To fix this, go to the Files tab (lower right corner, same pane as Plots and Help) and select the checkbox next to your document's name.

How do I get knitting to work in R?

Rendering Output If you are using RStudio, then the “Knit” button (Ctrl+Shift+K) will render the document and display a preview of it.


1 Answers

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.
like image 95
Roman Luštrik Avatar answered Dec 29 '22 00:12

Roman Luštrik