28

I want to read a txt in Rmd

---
title: "Untitled"
output: html_document
---
```{r}
country <- read.table("country.txt")
country
```

It show error:

processing file: Preview-2878539db5c7.Rmd

Quitting from lines 6-8 (Preview-2878539db5c7.Rmd) 
Error in file(file, "rt") : cannot open the connection
Calls: <Anonymous> ... withCallingHandlers -> withVisible -> eval -> eval -> read.table -  > file
Execution halted

But I can run code in R console successfully

> country <- read.table("country.txt")

> country
     production1 education1     fir1 inflation1 lq1 nonstatein1 patent1     tax1   trade1
2001    52920.47   132649.4 2.339263   0.700000  NA    19562.16  109313 23783.07 23783.07
2002    65876.57   144090.3 2.500826  -0.800000  NA    24727.30  131972 27479.61 27479.61
2003    89227.20   156280.4 2.691411   1.168900  NA    34044.45  164611 31537.50 31537.50
2004    92656.06   167683.7 2.615238   3.878600  NA    45613.10  177364 36179.87 36179.87
2005   167115.37   171379.7 2.617289   1.810000  NA    77525.52  231570 42008.37 42008.37
2006   218827.79   181084.6 2.578939   1.467800  NA    77441.52  282315 54866.43 54866.43
2007   286691.88   192677.9 2.439093   4.772700  NA    99032.26  333059 66453.31 66453.31
2008   364955.86   202542.8 2.440807   5.864570  NA   124621.23  418524 74104.80 74104.80
2009   398476.09   213539.2 3.783803  -0.693900  NA   153670.18  537957 65501.69 65501.69
2010   511364.93   254805.1 3.806066   3.322200  NA   194286.94  700304 81966.57 81966.57
2011   624657.55   279690.7 2.862413   5.393433  NA   229513.81  997132 91118.75 91118.75

The file is existing in wd

> file.exists("country.txt")
[1] TRUE

I also try to use read.csv() but it show similar error:

processing file: Preview-28786aad2e0.Rmd

Quitting from lines 6-8 (Preview-28786aad2e0.Rmd) 
Error in file(file, "rt") : cannot open the connection
Calls: <Anonymous> ... withVisible -> eval -> eval -> read.csv -> read.table -> file
Execution halted
Alexey Grigorev
  • 2,415
  • 28
  • 47
Liu
  • 403
  • 1
  • 4
  • 7

6 Answers6

32

The short answer is:

KNOW THY getwd()

Do not confuse the working directory of your current R console with the working directory of your R Markdown document (which is the directory of this R Markdown document). When in doubt, print out getwd() where you want to know your working directory (e.g. in *.Rmd). Note R Markdown documents are compiled in separate R sessions to enhance reproducibility, so you current R console has nothing to do with the compilation of the R Markdown documents.

Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
  • @StevenLee No, you are most likely to be doing the wrong thing if you find you have to use `setwd()` or using absolute paths. As I said, go ahead and figure out what your working directory `getwd()` really is, and always think about _relative paths_. For example, if `country.txt` is under the same directory as your Rmd file, you do not need to do anything and you are all set. – Yihui Xie Sep 16 '14 at 06:37
  • 2
    But there are occasions when data files may be far from the code( like I keep code in sync with a net disk, while really large data file elsewhere), is there an option to set the knitr document working directory? – xgdgsc Nov 02 '14 at 09:00
  • The opts_knit$set(root.dir = 'path') doesn' t work outside the document. For accessing from other people/OS, I need the working directory set from outside the document. – xgdgsc Nov 02 '14 at 09:20
  • The global options default working directory setting in Rstudio doesn' t work for .Rmd files. Maybe you need to make it clear the setting is for .R files only, and adding working directory setting in the Rmd Document Options for file. – xgdgsc Nov 02 '14 at 09:34
  • 1
    @xgdgsc You can always use absolute paths (e.g. `read.csv('/full/path/to/your/data')`) if you do not need other people to reproduce your document on different computers. – Yihui Xie Nov 02 '14 at 18:07
  • But I do need me or other people on other computer with different OS to reproduce. Maybe separating code to a .R file is a better workflow here. – xgdgsc Nov 03 '14 at 01:36
  • setwd() seems to work but in every code chunk it resets itself. As long as you keep that in mind it'll be fine. – OganM Dec 05 '14 at 00:00
  • I agree with @xgdgsc, being able to setwd() at the beginning of the document would be optimal. Is there not a way of preventing every code chunk from resetting it? – A.Mstt Feb 25 '15 at 00:53
3

Your current RMD file is not in your current working directory

1

Just try and place all your data file where your Markdown (.Rmd) file resides.

1

Mine is a silly case, but may happen to someone else... the Rmd had been saved to my default working directory not the current working directory where I thought I was. I had a long list of files and didn't realize the script was not there. Worth to check!

Picarus
  • 760
  • 1
  • 10
  • 25
0

After spending literally 2 hours on this, this is the final solution-

Using getwd() will not solve your problem, you have to paste the complete path no matter what your current directory is in the R markdown

For example in my case - System - MacOS, Latest version of R and RStudio as of June 2022

Location of my file was -

/Users/bhavykhatter/Desktop/data_project/data to use/202205-divvy-tripdata.csv

On using getwd() in my markdown file I get this-

enter image description here

So ideally this should work

enter image description here

But it doesn't and we get the same error as stated above

enter image description here

But on entering the complete path, everything works normally

enter image description here

0

I also made the same error. Finally, I found out that I chose "knit on save" by accident. The problem is solved by not selecting "knit on save".