0

I am quite new to using R - and i am currently trying to create an elevation diagram of a study site using the below code:

library(rnaturalearth)
library(rnaturalearthhires)
library(mapview)
library(mapedit)
library(elevatr)
library(tidyverse)
library(raster)

countries<-ne_countries(scale = 110, type = "countries", continent = NULL,
                        country = NULL, geounit = NULL, sovereignty = NULL,
                        returnclass = c("sf"))

admin<-ne_states(country = NULL, geounit = NULL, iso_a2 = NULL, spdf = NULL,
                 returnclass = c("sf"))

admin %>% filter(name=="Áncash") -> Áncash
countries %>% filter(name=="Peru") -> PER

mapview(Áncash) %>% editMap() -> d  

poly<-d$drawn
elev1 <- get_elev_raster(poly, z = 14,clip="bbox")

When I try to run this I run into the following error message:

Listening on http://my IP:8888 # For reasons I am not releasing my IP
createTcpServer: address not available
Error in .subset2(public_bind_env, "initialize")(...) : 
  Failed to create server
> 
> poly<-d$drawn
Error: object 'd' not found
> elev1 <- get_elev_raster(poly, z = 14,clip="bbox")
Error in if (attributes(class(locations)) %in% c("raster", "sp")) { : 
  argument is of length zero

There is probably something really simple I am missing, but after a couple of hours trying to do this on my own, I am at the point now of needing help!

Thanks in advance!

suvayu
  • 4,271
  • 2
  • 29
  • 35
t33ling
  • 5
  • 5
  • which packages are you using? The error seems to happen when creating a tcp server. Which function creates a tcpserver? – zelite Feb 17 '20 at 10:31
  • Hello t33ling, welcome to SO. Can you help us help you by providing a minimal reproducible example ? You will find how to do so [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – cbo Feb 17 '20 at 10:31
  • Hello cbo, thank you for sending over that link! I'll make sure to follow it in the future. I was typing this up in a state of caffeine fuelled panic and have obviously missed out some vital info. – t33ling Feb 17 '20 at 13:47

1 Answers1

0

Your code actually worked for me (eventually), once I had downloaded all the R packages which were required to run it:

library(rnaturalearth)
library(rnaturalearthhires)
library(mapview)
library(mapedit)
library(elevatr)
library(tidyverse)
library(raster)

You seem to be getting an error when running editMap, which launches a shiny application that allows you to draw lines and polygons on a map. You have witheld your IP address in your question, but this IP address should be your home address, i.e. http://127.0.0.1.

I think you might just need to change the port that shiny is running on, so try this:

options(shiny.port = 7775)

Then when you do

mapview(Áncash) %>% editMap() -> d

It should say

#> Listening on http://127.0.0.1:7775

And your viewer should show this:

enter image description here

You should then be able to draw a polygon on your map. I would advise that you only draw a tiny polygon, otherwise the elevation data will literally take hours to download.

When you are happy with the polygon, stop the application and d will contain your polygon. So now when you do

poly<-d$drawn
elev1 <- get_elev_raster(poly, z = 14, clip="bbox")

The data will download to the raster elev1 which you can now plot:

plot(elev1)

enter image description here

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • Ah that's worked! I think it might have been because I hadn't set the right port. Thank you so much! – t33ling Feb 17 '20 at 14:16