无法在Windows中使用`raster :: raster`和Rscript读取光栅文件

时间:2016-02-19 21:31:31

标签: r r-raster

我似乎只有一个仅限Windows的问题,使用Rscript读取栅格并直接调用raster包。我有最新版本的raster包(2.5-2)。在Windows 2008 R2中运行R 3.2.1。

鉴于以下内容:

library(raster)
j <- raster(matrix(rnorm(100), ncol=10, nrow=10))
writeRaster(j, 'j.tif')

如果我开始一个新的R会话(或不是),这适用于R:

library(raster)
j <- raster('j.tif')

这样做(再次,在新的会话中):

j <- raster::raster('j.tif')

如果我从命令行尝试并使用Rscript,则可以:

C:\> Rscript -e "library(raster); raster('j.tif')

但这不起作用:

C:\> Rscript -e "raster::raster('j.tif')"
Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer",  :
  Cannot create a RasterLayer object from this file.
Calls: <Anonymous> -> <Anonymous> -> .local -> .rasterObjectFromFile
Execution halted

1 个答案:

答案 0 :(得分:1)

该文件是否存在于c:/中?你能做到:

Rscript -e "file.exists('j.tif')"

我认为你对Rscript没有加载方法包是正确的。但raster取决于它并加载它:

Rscript -e "library(raster); sessionInfo()"

#Loading required package: methods
#Loading required package: sp
#R Under development (unstable) (2016-01-09 r69890)
#Platform: x86_64-w64-mingw32/x64 (64-bit)
#Running under: Windows 7 x64 (build 7601) Service Pack 1

#attached base packages:
#[1] methods   stats     graphics  grDevices utils     datasets  base