简体   繁体   English

read.table() 和 read.csv 在 Rmd 中都有错误

[英]read.table() and read.csv both Error in Rmd

I want to read a txt in Rmd我想在 Rmd 中阅读一个 txt

---
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但我可以在 R 控制台中成功运行代码

> 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该文件存在于 wd

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

I also try to use read.csv() but it show similar error:我也尝试使用 read.csv() 但它显示类似的错误:

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

The short answer is:简短的回答是:

KNOW THY getwd()了解你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).不要将当前 R 控制台的工作目录与 R Markdown 文档的工作目录(即此 R Markdown 文档的目录)混淆。 When in doubt, print out getwd() where you want to know your working directory (eg in *.Rmd).如有疑问,请在您想知道工作目录的地方打印出getwd() (例如在 *.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.注意 R Markdown 文档在单独的 R 会话中编译以增强可重复性,因此您当前的 R 控制台与 R Markdown 文档的编译无关。

您当前的 RMD 文件不在您当前的工作目录中

只需尝试将所有数据文件放在 Markdown (.Rmd) 文件所在的位置。

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.我的情况很愚蠢,但可能会发生在其他人身上...... Rmd 已保存到我的默认工作目录,而不是我认为的当前工作目录。 I had a long list of files and didn't realize the script was not there.我有一长串文件,但没有意识到脚本不存在。 Worth to check!值得检查!

After spending literally 2 hours on this, this is the final solution-在花费了 2 个小时之后,这是最终的解决方案-

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使用 getwd() 不会解决您的问题,无论您的当前目录在 R 降价中,您都必须粘贴完整路径

For example in my case - System - MacOS, Latest version of R and RStudio as of June 2022例如在我的情况下 - 系统 - MacOS,截至 2022 年 6 月的最新版本的 R 和 RStudio

Location of my file was -我的文件的位置是 -

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

On using getwd() in my markdown file I get this-在我的降价文件中使用 getwd() 时,我得到了这个-

在此处输入图像描述

So ideally this should work所以理想情况下这应该有效

在此处输入图像描述

But it doesn't and we get the same error as stated above但它没有,我们得到与上述相同的错误

在此处输入图像描述

But on entering the complete path, everything works normally但是进入完整路径后,一切正常

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM