Regarding ggmap trouble

Hi, I installed the ggmap from CRAN and loaded too. But when I tried to do something like this https://www.r-bloggers.com/r-beginners-plotting-locations-on-to-a-world-map/ it says me to generate an API key. So, I did that and tried to save my API key. But it was telling me that it's an invalid API. I don't know what was wrong. So, I uninstall the ggmap and installed it again from Github. But I faced problem here as well. It was showing me this error -- Error: Failed to install 'ggmap' from GitHub: (converted from warning) cannot remove prior installation of package ‘digest’.
Could you please help in this regard?

Try to keep one problem per topic, so if you want us to help you with the installation issue first, please post the whole error message you get when you try to install the package. If you already solved that and you want help with your plot, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

Thank you. I am new here. Apology for the inconveniences. I tried to troubleshoot by installing digest. But mow instead of showing invalid PI it is showing something like below.

Here is the screenshot:

I loaded ggmap and then try to load the API key. But again it is saying the provided API key is invalid.

Are you dead set on using {ggmap}? Its geocoding function is built against Google API, which has gotten much bad rep recently.

There are other backends available though, among others Open Street Map accessible via {tmaptools} package.

If you are after something like the linked example from 2013 you may wish to consider something along these lines, built on more current toolset but giving a comparable result:

library(tmap)
library(tmaptools)

data("World") # for the basemap

cities <- tmaptools::geocode_OSM(c("New York", "Prague", "Lagos"), # or what not :)
                                 as.sf = T) 

tm_shape(World) + tm_polygons() +
  tm_shape(cities) + tm_bubbles(col = "red")

1 Like

Thank you so much for you response. Much appreciated. Yea, I am trying to locate Central and Northern China, South Korea, and Japan for the origin of wild soybean. The example you gave is aligned with my interest. I followed the instructions. But I got following error:

data("World")
Warning message:
In data("World") : data set ‘World’ not found

Any idea what's going on?

The dataset World (note the capital W) lives in {tmap} package. Do you have it installed and loaded? It may require updating.

Yes, I loaded it -- tmap is showing in my global environment.

I might be wrong but I believe that Google API keys are not enclosed among [ ], I can register my API key without any issue, check that you are setting your credentials correctly.

library(ggmap)

register_google(key = Sys.getenv("google_key"))
qmap("Peru", zoom = 5)

That is strange... what is your version of tmap? Or rather, what does your sessionInfo say?


> sessionInfo()
R version 3.6.3 (2020-02-29)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.4 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/atlas/libblas.so.3.10.3
LAPACK: /usr/lib/x86_64-linux-gnu/atlas/liblapack.so.3.10.3

locale:
 [1] LC_CTYPE=en_US.UTF-8        LC_NUMERIC=C                LC_TIME=custom.UTF-8        LC_COLLATE=en_US.UTF-8     
 [5] LC_MONETARY=custom.UTF-8    LC_MESSAGES=en_US.UTF-8     LC_PAPER=custom.UTF-8       LC_NAME=C                  
 [9] LC_ADDRESS=C                LC_TELEPHONE=C              LC_MEASUREMENT=custom.UTF-8 LC_IDENTIFICATION=C        

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] tmaptools_3.0 tmap_3.0     

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.4.6       raster_3.1-5       magrittr_1.5       units_0.6-6        munsell_0.5.0     
 [6] colorspace_1.4-1   viridisLite_0.3.0  lattice_0.20-41    R6_2.4.1           rlang_0.4.5       
[11] tools_3.6.3        dichromat_2.0-0    parallel_3.6.3     grid_3.6.3         packrat_0.5.0     
[16] leafem_0.1.1       png_0.1-7          KernSmooth_2.23-16 e1071_1.7-3        DBI_1.1.0         
[21] htmltools_0.4.0    crosstalk_1.1.0.1  leafsync_0.1.0     class_7.3-16       abind_1.4-5       
[26] leaflet_2.0.3      digest_0.6.25      lifecycle_0.2.0    lwgeom_0.2-3       sf_0.9-2          
[31] RColorBrewer_1.1-2 base64enc_0.1-3    codetools_0.2-16   stars_0.4-1        htmlwidgets_1.5.1 
[36] sp_1.4-1           compiler_3.6.3     scales_1.1.0       XML_3.99-0.3       classInt_0.4-3

My session info is given in a screenshot herewith (showing R version 3.6.2). But whenever I tried to install and load any package, it says was built in under version 3.6.3. Do u think this might be an issue?

Hi, I tried the way you advised. This time it's not saying invalid API key, instead it's saying -- Error: Google now requires an API key.
See ?register_google for details.

Any suggestions?

Can you show what have you tried? Just to be sure, you haven't used this code right? Sys.getenv("google_key")
You have to replace this with your API key, my advice was that you double check that you are copying it correctly.

Here is an attached image for your convenience. Thank you!!

As I said, you are not supposed to use Sys.getenv("google_key") I use that to avoid showing my key, you have to replace that with your own API key.

Yes, in the parentheses inside the quotation mark I used my own API key. You can see I hid them using red rectangular box for security issue.

But you are still using Sys.getenv() you are not supposed to use that. That function is for retrieving the value of environmental variables, I use it because I have my API key stored on my Renviron.site file. You have to simply use your API key directly, just make sure you have paste it correctly, notice that the key shouldn't be surrounded by []

Thank you so much for your time. Yes, I tried like this:
register_google(key = ("My API Key")) and register_google(key = "My API Key")
But after the hit it's not loaded in the environment. I don't know what's wrong! :frowning:

Also, if you have noticed in my earlier comment regarding the session info screenshot (showing R version 3.6.2). But whenever I tried to install and load any package, it says was built in under version 3.6.3. Do u think this might be an issue? Do I need to install the latest version and start all over again?

It is a good practice to keep your R version current (which for the next couple days means 3.6.3; version 4.0.0 is scheduled to be released on Friday), especially when on Windows and relying on pre-built binaries.

From your screenshot it seems as if the {tmap} package was not loaded (only {tmaptools} is), which would explain why the World dataset was not available. This may relate to your R version and may not.

I would strongly suggest to update your R version, unless you have a compelling reason not to (things like admin access on work computer and so on). It is not difficult - installr::updateR() is a neat shortcut - and it will get rid of one possible sticking point.

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