[R-sig-Geo] Error in writeRaster for signature RasterPCA

Melanie Bacou mel at mbacou.com
Wed Mar 8 14:23:08 CET 2017


Vijay,

`rasterPCA` returns an object of class `rasterPCA` (a list). The actual 
raster layers (components) can be accessed via its `map` element. See 
example below.

--Mel.

library(raster)
library(RStoolbox)

bio <- getData("worldclim", var="bio", res=10, path="~/WorldClim")

# You can use `filename=` arg to save the resulting rasters
pre_pca <- rasterPCA(bio, nComp=2, filename="~/PC.tif")

class(pre_pca)
## [1] "rasterPCA" "RStoolbox"

summary(pre_pca)
##       Length  Class       Mode
## call        4 -none-      call
## model       7 princomp    list
## map   3888000 RasterBrick S4

dim(pre_pca$map)
## [1]  900 2160    2

# Plot the 1st comp.
ggR(pre_pca$map[[1]])


On 03/07/2017 01:06 PM, Vijay Ramesh via R-sig-Geo wrote:
> I am currently trying to obtain two rasters that are the Principal
> component axes 1 and 2 for a list of bioclimatic variables that I am using.
> I am using the rasterPCA() function that exists in the RStoolbox in R, but
> I am unable to write it to a raster. Below is the code and the error that I
> am getting:
>
> library(raster)
> library(RStoolbox)
> ##Loading the variables and then creating a raster stack, followed by a PCA
> bio3 <- raster("C:\\Users\\rameshv\\bio3")
> bio4 <- raster("C:\\Users\\rameshv\\bio4")
> bio5 <- raster("C:\\Users\\rameshv\\bio5")
> bio6 <- raster("C:\\Users\\rameshv\\bio6")
> bio14 <- raster("C:\\Users\\rameshv\\bio_14")
>
> pres_stack <- stack(bio3, bio4,bio5,bio6,bio14)
>
> pre_pca <-  rasterPCA(pres_stack, nComp = 2)
>
> writeRaster(pre_pca, "C:\\Users\\rameshv\\PC.tif")
>
> Error in (function (classes, fdef, mtable) : unable to find an inherited
> method for function ‘writeRaster’ for signature ‘"rasterPCA", "character"’
>
> I even tried a modification of the above code
>
> writeRaster(pre_pca,"C:\\Users\\rameshv\\4_PCAforR\\PC.asc",
> format="ascii", bylayer=T)
>
> Getting the same error as above: Error in (function (classes, fdef, mtable)
> : unable to find an inherited method for function ‘writeRaster’ for
> signature ‘"rasterPCA", "character"’
>
> Any suggestions?
> Vijay


	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list