Hello all, I'm trying to knit an RMarkdown to an HTML file.
But I got an error like this
Quitting from lines 28-34 (learning_map2.Rmd)
Error in slot(p, "Polygons") :
cannot get a slot ("Polygons") from an object of type "NULL"
Calls: ... createSPComment -> lapply -> FUN -> sapply -> lapply -> slot
To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:
From this error message, it seems p is NULL. I don't think this is related to an OS issue. Check that your Rmd file is running in a clean session on both your environment.
As @andresrcs reminded, it would help us help you if you provide a reproducible example, (or at least part of the code your Rmd). With only the error, we can't say much more thant what is already written.
---
title: "learning_map2"
author: "huynhbinh"
date: "26/8/2020"
output: html_document
---
# This lesson give a way to build a map with points and labels
## Get necessary packages
``` {r}
library(tidyverse)
library(ggplot2)
library(raster)
```
## Get data from GADM for Vietnam
```{r}
vn<-getData('GADM',country='Vietnam',level=3)
vl<-vn[vn$NAME_1=='Vĩnh Long',] #pick row for "Vinh Long" province
head(vn)
head(vl)
```
## Change data to plot data by function "fortify" in "tidyverse" package
``` {r}
dat_plot<-fortify(vl,region = 'NAME_2') #choose data to district level#
lab<-data.frame(long=105.9622895,lat=10.2365819,pos='MTU') #set point coordinate and its label#
khuvuc<-aggregate(cbind(long,lat)~id,data=dat_plot,FUN=mean) #create label for each district#
head(dat_plot)
lab
khuvuc
```
the error appear in the line
dat_plot<-fortify(vl,region = 'NAME_2') #choose data to district level#
but everything was ok when I ran it on my Mac, haha. I published it on Rpub, can may I paste the Rpub link at here, so you can see all my code easily?
Dear Mr. @andresrcs , thank for your advice! I'm reading it.
I can reproduce your issue on a Windows machine, it has to do with the enconding of the SaptialPolygonsDataFrame, I'm not experienced with geocomputation so I'm going to ask one of our sustainers to take a look into your issue.
This would be the reprex
I was writing my answer at the same time as @andresrcs !
I don't think this is related to Rmarkdown. You should get the error even if you ran your script in the console from an R script.
As a workaround, you could to this
vn <- raster::getData('GADM',country='Vietnam',level=3)
# getting Vĩnh Long from the data
# Could change if source changes
vin_long <- sort(unique(vn$NAME_1))[61]
vl<-vn[vn$NAME_1 == vin_long,] #pick row for "Vinh Long" province
head(vn)
head(vl)
I believe your error is in fact due to encoding. There is an unusual accent on letter I in the province name, and these are known to cause bewildering platform related issues.
If I am right this should do the trick (looking up the province by key value, not name):
The error in fortify was due to the fact that the vl object was empty (due to filter on name not working as expected) and therefore contained no polygons.
A somewhat unrelated observation: if the ultimate aim is to produce a plot you might be better off with a workflow based on {sf} package, and a ggplot2::geom_sf(); it is more concise and produces IMHO prettier plots than the older fortify() based workflow.