R rgdal :: readOGR问题,文件路径引用linux主目录

时间:2016-12-22 17:01:06

标签: r rgdal

这不是一件大事,但有人知道原因:

readOGR('~/documents/zipcodes', 'zipcodes')

返回:" ogrInfo中的错误(dsn = dsn,layer = layer,encoding = encoding,use_iconv = use_iconv,:   无法打开文件"

当这个:

setwd('~/documents/zipcodes')
readOGR('.', 'zipcodes')

完美运作?

1 个答案:

答案 0 :(得分:1)

您可以像这样使用tools::file_path_as_absolute()

library(tools)
readOGR(file_path_as_absolute('~/documents/zipcodes'), 'zipcodes')

path.expand()正如hrbrmstr在他的评论中提到的那样:

readOGR(path.expand('~/documents/zipcodes'), 'zipcodes')

另一种更不方便的方法是将base::dirname()paste0()结合使用:

readOGR(paste0(dirname('~/documents/zipcodes'), 'zipcodes'), 'zipcodes')