GIS4DEV / GIS4DEV.github.io

Open Source GIScience & GIS for Development
1 stars 12 forks source link

Code issue in the miscellaneous map features section #40

Closed vinfalardeau closed 3 years ago

vinfalardeau commented 3 years ago

I'm not sure what's going on, but I was able to make a map in R last week, and now I'm running into an error in the section that makes a layer for national parks, reserves, and lakes (in the "r misc. map features" code chunk). What I'm trying to run is this:

ea = lhz %>% st_transform(3395) %>% summarize %>% st_geometry %>% st_intersection(st_geometry(st_transform(ta, 3395))) %>% st_transform(4326) %>% st_sf %>% mutate(EA = case_when( grepl("Reserve", ta[["NAME_2"]]) | grepl("Park", ta[["NAME_2"]]) ~ "National Parks and Reserves", T ~ "Missing Data") ) %>% rbind(lakes) %>% st_make_valid()

The error seems to be that there are 256 Traditional Authorities, but only 255 features in the ea data frame prior to the mutate step:

Error: Problem with mutate() input EA. x Input EA can't be recycled to size 255. ℹ Input EA is case_when(...). ℹ Input EA must be size 255 or 1, not 256.

Maybe one of the TA features is entirely outside of the livelihood zones, so it gets clipped out and cannot be pushed into the case_when statement? I've been stuck on this line of code for a while, trying to recreate the map I had earlier, so I might just resort to doing it in QGIS

vinfalardeau commented 3 years ago

On second thought, probably not important – looks like I saved the old version of the map, and this step isn't needed for the figure 4 comparison.

josephholler commented 3 years ago

I'm wondering if one of the case_when results is NULL in your data? It also may not be necessary to run this code chunk again, since it's just creating features for cartography that are the same across all of the maps? If it has already been run, then you can use the existing EA data frame. Sometimes re-running older code chunks when the data objects already exist can cause trouble, because the chunks are based on assumptions that the data is in an older state...

josephholler commented 3 years ago

BTW if you need to scrap a data object and recreate it, in the console the command rm(EA) deletes/removes the EA data.

vinfalardeau commented 3 years ago

Thanks, using rm() for all the inputs and then reloading them without some intermediate steps did the trick!

The trouble is that when I've closed and reopened my R Project, the global environment comes up empty, which is odd because in my experience an .Rproj file usually stores everything in the environment. So anything that I hadn't written to a file (such as the EA data frame), I needed to restore by re-running code. I think I must have either added a step in the code that threw off one of the inputs, or else I forgot to run a necessary snippet somewhere, but all's well now.