2

I am doing some geocoding of street addresses (n=18,000) using the ggmap package in R and the Google Maps API, which I understand has a limit of 2,500 geocoding requests per day for addresses.

The geocoding script I'm using is very simple and works on the small test dfs I've tried (like the sample below), but I'm wondering about the most simple/elegant way to stitch together the final geocoded df of all 18,000 locations over the next ~7 days for each 2500-row chunk.

I'd thought about just numbering them by day and then binding them all together at the end, using the following line of code each time on a df that looks like the sample below:

library(ggmap)
library(tidyverse)

register_google(key = "MY API KEY", write = TRUE)

pharmacies <- data.frame(pharm_id = c("00001", "00002", "00003"), address = c("250 S. Colonial Drive, Alabaster, AL 35007", "6181 U.S. Highway 431, Albertville, AL 35950", "113 Third Avenue S.E., Aliceville, AL 35442")

pharmacies_geocoded_1 <- mutate_geocode(pharmacies, address, output = "latlon")
pharm_id address
00001 250 S. Colonial Drive, Alabaster, AL 35007
00002 6181 U.S. Highway 431, Albertville, AL 35950
00003 113 Third Avenue S.E., Aliceville, AL 35442

But it seems like manually doing this day by day will get a bit messy (or that there may be some more elegant loop strategy that I can set up once and walk away from). Is there a better way?

arachne591
  • 21
  • 4
  • Hi, @arachne591, I am also thinking to bind them day by day. I am interested to see the answer if it can be automatically done for the next seven days. Thanks – ail Mar 26 '22 at 01:29

1 Answers1

1

EDIT

As @arachne591 says its also available a R interface to cron with cronR package. Also in Windows taskscheduleR makes the same job.


You can wrap you code on a scrip and run it daily with a cron job:

If you are on UNIX (Linux/MAc):

crontab -e

and then introduce a new line with:

0 0 * * 0-6 Rscript "/route/to/script.R"

This runs your script “At 00:00 on every day-of-week from Sunday through Saturday.”

You can build your own schedule with contrabguru

Additional resources:

VYago
  • 325
  • 2
  • 9
  • Thanks - I didn't know about this option! Now I am looking into using cronR or taskscheduleR ( https://anderfernandez.com/en/blog/how-to-automate-r-scripts-on-windows-and-mac/ ). I have a Mac M1 running on Big Sur in case that is relevant – arachne591 Mar 26 '22 at 01:48
  • Good point! cron works in Mac too, I have added your suggestions to my answer, sure it works. Please upvote if was helpful. – VYago Mar 26 '22 at 11:23