In this post I was trying to find a way to get a list of GRASS data layers in R. The problem is running the GRASS function ‘g.list’ from R (using system or execGRASS from the spgrass6 package) gives a vector which can not be easily handled in R (i.e., it is not a nice vector with one layer name per vector element).
Finding the solution was a nice exersize in using the R functions gsub() and strsplit(), but today I realized that there is solution that is not only easier but also better and more flexible.
Instead of using the GRASS function g.list, just use g.mlist. This GRASS function allows you to filter the layers you want to list based on regular expressions. What is important in the light of the above-described problem is that the output is a text file (or output on the console) with one layer per line. So much more sensible as it means that if you run the command in R, you’ll get a vector with one name per vector element… Great! The code should look something like:
system("g.mlist pattern=* mapset=species")
Or use the execGRASS command from the spgrass6 package (see my previous post)