# Run examples if in an active GRASS session in the nc_basic_spm_grass7
Sys.setenv("_SP_EVOLUTION_STATUS_" = "2")
run <- FALSE
GISRC <- Sys.getenv("GISRC")
if (nchar(GISRC) > 0) {
location_name <- read.dcf(GISRC)[1, "LOCATION_NAME"]
if (location_name == "nc_basic_spm_grass7") {
run <- TRUE
}
}
# Save and set echo command option
echoCmdOption <- get.echoCmdOption()
set.echoCmdOption(TRUE)
if (run) {
# Read and print GRASS interface description for 'r.slope.aspect'
print(parseGRASS("r.slope.aspect"))
}
if (run) {
# Assemble the 'r.slope.aspect' command with specified parameters as a string
doGRASS(
"r.slope.aspect",
flags = c("overwrite"),
elevation = "elevation.dem",
slope = "slope",
aspect = "aspect"
)
}
if (run) {
# Alternatively, specify parameters as a list
params <- list(elevation = "elevation",
slope = "slope",
aspect = "aspect")
doGRASS("r.slope.aspect",
flags = c("overwrite"),
parameters = params)
}
if (run) {
# Read and print GRASS interface description for 'r.buffer'
print(parseGRASS("r.buffer"))
}
if (run) {
# Assemble the 'r.buffer' with specified parameters as as string
doGRASS(
"r.buffer",
flags = c("overwrite"),
input = "schools",
output = "bmap",
distances = seq(1000, 15000, 1000)
)
}
if (run) {
# Alternatively, specify parameters as a list
params <- list(
input = "schools",
output = "bmap",
distances = seq(1000, 15000, 1000)
)
doGRASS("r.buffer", flags = c("overwrite"), parameters = params)
}
if (run) {
# Restore original echo command option
set.echoCmdOption(echoCmdOption)
# Try executing 'r.stats' command which will fail because "fire_blocksgg"
# does not exist in the mapset
try(res <- execGRASS("r.stats", input = "fire_blocksgg", flags = c("C", "n")),
silent = FALSE)
}
if (run) {
# Execute 'r.stats' with legacyExec and print the result
res <- execGRASS(
"r.stats",
input = "fire_blocksgg",
flags = c("C", "n"),
legacyExec = TRUE
)
print(res)
}
if (run) {
# If the command failed, retrieve error message
if (res != 0) {
resERR <- execGRASS(
"r.stats",
input = "fire_blocksgg",
flags = c("C", "n"),
redirect = TRUE,
legacyExec = TRUE
)
print(resERR)
}
}
if (run) {
# Use 'stringexecGRASS' to run a command and print the result
res <- stringexecGRASS("r.stats -p -l input=geology", intern = TRUE)
print(res)
stringexecGRASS(
"r.random.cells --overwrite --quiet output=samples distance=1000 ncells=100 seed=1"
)
}
if (run) {
# Alternatively, run the same command using 'execGRASS'
execGRASS(
"r.random.cells",
flags = c("overwrite", "quiet"),
output = "samples",
distance = 1000,
ncells = 100L,
seed = 1L
)
}
Run the code above in your browser using DataLab