R语言:“cannot open the connection”的解决办法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yiifaa/article/details/82317632

在R语言的实践中,读入数据文件几乎是必须的操作,但没有设置好工作空间的位置,将会提示如下错误:

Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
  cannot open file 'Salary.csv': No such file or directory

对于读取文件的操作来说,如果采用相对路径的话,原因主要在于当前工作空间的指向不准确,最直接的办法是调用“setwd”命令,如下:

#   设置为工作目录
setwd("D:/Workspace/R-Works/R-Stat")

另外,在以脚本方式运行R脚本时,也尽可能显式地指定当前工作空间,如下:

#! /usr/bin/env RScript
# 显式地指定工作目录
setwd("D:/Workspace/R-Works/R-Stat")
# LIST
items <- read.csv("Salary.csv", TRUE, encoding = "UTF-8")
# 
typeof(items)

如果是采用“RStudio”IDE工具,则可以点击右下角文件面板“More->Set As Working Dictionary”,如下图:
RStudio

其他问题

1. 最后一行未输入换行符(回车)

Warning message:
In read.table("Score.txt", header = TRUE, sep = ",", quote = "\"",  :
  incomplete final line found by readTableHeader on 'Score.txt'

2. 中文字符解析失败(或者其他格式问题,一般都是某列数据格式问题)

<0 行> (或0-长度的row.names)

猜你喜欢

转载自blog.csdn.net/yiifaa/article/details/82317632
今日推荐