Skip to content

Commit

Permalink
to address #16 using solution from rstudio/learnr#529 (comment)
Browse files Browse the repository at this point in the history
  • Loading branch information
andysouth committed May 26, 2021
1 parent 6d3fdca commit e0c59cc
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions inst/tutorials/get-my-data-in/get-my-data-in.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ To map these data in R usually requires a 3 step process.

Here we will demonstrate the 3 steps using some airport data from the excellent [ourairports](https://ourairports.com/continents/AF/airports.csv) that we have extracted and saved in the [afrilearndata](https://github.com/afrimapr/afrilearndata) package.

```{r csv-airports-setup}
# to avoid issues with interactive maps https://github.com/rstudio/learnr/issues/529
# IdSeed must be set to be unique in the Rmd, even for mode('plot') to allow user to change
#tmap_mode("plot")
htmlwidgets::setWidgetIdSeed(1)
```


```{r csv-airports, exercise = TRUE, exercise.eval = TRUE}
Expand Down Expand Up @@ -120,6 +126,13 @@ In this case it tells `sf` what system to expect. In the majority of cases coord
**Question :** What happens when the `crs=4326` argument is not included in the code below ? Try adding it back in and see what the difference is.


```{r csv-crs-missing-setup}
# to avoid issues with interactive maps https://github.com/rstudio/learnr/issues/529
# IdSeed must be set to be unique in the Rmd, even for mode('plot') to allow user to change
#tmap_mode("plot")
htmlwidgets::setWidgetIdSeed(2)
```

```{r csv-crs-missing, exercise = TRUE, exercise.eval = TRUE, message = FALSE, warning = FALSE}
# 1. read into dataframe
Expand Down Expand Up @@ -172,6 +185,13 @@ An alternative is directly to create a dataframe within R containing coordinates

In the example below try changing the coordinates within the dataframe at step 1, and run to see the points change.

```{r dataframe-sf-setup}
# to avoid issues with interactive maps https://github.com/rstudio/learnr/issues/529
# IdSeed must be set to be unique in the Rmd, even for mode('plot') to allow user to change
#tmap_mode("plot")
htmlwidgets::setWidgetIdSeed(3)
```

```{r dataframe-sf, exercise = TRUE, exercise.eval = TRUE, message = FALSE, warning = FALSE}
# 1. create dataframe
Expand Down Expand Up @@ -204,6 +224,12 @@ If a colleague emails use just a single file named `*.shp` then you will not be

Shapefiles can store points, lines or polygons. The example below uses a shapefile containing polygons.

```{r shp-countries-setup}
# to avoid issues with interactive maps https://github.com/rstudio/learnr/issues/529
# IdSeed must be set to be unique in the Rmd, even for mode('plot') to allow user to change
#tmap_mode("plot")
htmlwidgets::setWidgetIdSeed(4)
```

```{r shp-countries, exercise = TRUE, exercise.eval = TRUE, message = FALSE, warning = FALSE}
Expand All @@ -226,6 +252,13 @@ For other spatial vector formats (e.g. kml, geopackage & geojson) the same appro

Here we show an example with a .kml file of the simplified African highway network.

```{r kml-highways-setup}
# to avoid issues with interactive maps https://github.com/rstudio/learnr/issues/529
# IdSeed must be set to be unique in the Rmd, even for mode('plot') to allow user to change
#tmap_mode("plot")
htmlwidgets::setWidgetIdSeed(5)
```

```{r kml-highways, exercise = TRUE, exercise.eval = FALSE, message = FALSE, warning = FALSE}
filename <- system.file("extdata","trans-african-highway.kml", package="afrilearndata", mustWork=TRUE)
Expand All @@ -244,6 +277,12 @@ To read in raster data we need to use the package `raster` instead of `sf`. The

`raster(filename)` will also work with other raster formats such as ascii grids or .jpg.

```{r rast1-setup}
# to avoid issues with interactive maps https://github.com/rstudio/learnr/issues/529
# IdSeed must be set to be unique in the Rmd, even for mode('plot') to allow user to change
#tmap_mode("plot")
htmlwidgets::setWidgetIdSeed(6)
```

```{r rast1, exercise = TRUE, exercise.eval = TRUE, message = FALSE, warning = FALSE}
Expand All @@ -268,6 +307,13 @@ In intro-to-spatial-r we fixed this in `tmap` using the `breaks=` argument to se
In these examples we have used `mapview` to give us a quick view by passing it only the spatial object and not specifying any other options. `mapview` is very flexible and by passing just a few arguments the map can be made much more informative. Try copy & pasting this line to replace the final line in the code window below and running it. It uses the columns named `type` and `name` from the datafile to colour and label the points. `cex` sets the size of the points, in this case making them smaller.
`mapview(mysf, zcol='type', label='name', cex=2)`

```{r mapview-options-airports-setup}
# to avoid issues with interactive maps https://github.com/rstudio/learnr/issues/529
# IdSeed must be set to be unique in the Rmd, even for mode('plot') to allow user to change
#tmap_mode("plot")
htmlwidgets::setWidgetIdSeed(7)
```

```{r mapview-options-airports, exercise = TRUE, exercise.eval = FALSE}
# 1. read into dataframe
Expand Down

0 comments on commit e0c59cc

Please sign in to comment.