Error in slot when knit in markdown

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

Execution halted
screenshot_1598604180|690x133

BUT. I can run it on my mac, It's just doesn't run in my window pc :~
Thanks all!

Hi!

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.

Hope it helps

dear bro, hear is my markdown code:

---
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

library(raster)
#> Loading required package: sp

vn <- getData('GADM',country='Vietnam',level=3)
vl <- vn[vn$NAME_1=='Vinh Long',]
head(vl)
#>  [1] GID_0     NAME_0    GID_1     NAME_1    NL_NAME_1 GID_2     NAME_2   
#>  [8] NL_NAME_2 GID_3     NAME_3    VARNAME_3 NL_NAME_3 TYPE_3    ENGTYPE_3
#> [15] CC_3      HASC_3   
#> <0 rows> (or 0-length row.names)

Created on 2020-08-30 by the reprex package (v0.3.0)

1 Like

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)

but that is only a short term solution.

Hi @hpkbinh

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):

vn <- getData('GADM',country='Vietnam',level=3)
vl <- vn[vn$GID_1=='VNM.61_1',]
head(vl)

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.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.