Pay to Play: Google API Keys and Mapping in R

In January 2019, Google updated their terms of service and has essentially removed the free access to Google Maps in R. This means that you’ll need to purchase the relevant APIs (compu-speak for Application Programming Interface) from Google in your google account to access these features in R.

So do you need it?

If you’re interested in mapping in R, you basically need it. There are some mapping packages that you can use to get around using any Google products (Leaflet is a great example). But for all the glorious customization and overwhelming ubiquity of the ggmap, this API key is essential for reproducible science in ecology related fields. When I first encountered the problem, troubleshooting was a nightmare–everyone used ggmap, and even those who didn’t still used Google Maps as a source for their base maps. Not. Fun.

Luckily, it’s relatively cheap at $2/month for the first 100,000 static maps in each month (dynamic maps, street maps, embed advance, and dynamic street maps cost more, but we aren’t likely using these tools in our work). Even luckier, there’s a $200 credit/month for the first year of use!

But how?

It’s a bit confusing to navigate the Google Cloud Console if you’re trying to figure it out solo (and scary considering you’re paying for something), but the actual steps are easy and quick. There are two main steps to the process: 1)Get an API key and 2) Show R your API key. There’s just a few ministeps in between.

Get an API key:

  1. Go to this website.
  2. Pick your product (Maps).
  3. Select a Project. If you don’t have one, create one. It won’t matter later.
  4. Enter your billing information.
  5. Copy your API key. Consider pasting it into a .txt file on your machine for safe keeping.

Show R your API key:

  1. In your R console, enter this code:
    • register_google(key = “YOUR_API_KEY”)
  2. Run this code for every new session you need to map in, and you’re ready to go!

a daRk tuRn

Open science is good for everyone!

R is popular among scientists (especially ecology/conservation scientists) because of its power. But it’s basically essential for scientists because it’s free. In a field where funding is scarce and costs are high, R has been a blessing for open science and has seriously moved the discipline forward. But the same reason R is powerful is because it’s not entirely autonomous; it (in large part) relies on monolithic companies like Google to up the ante. It may not be a very expensive fee, but it is yet another barrier for researchers and open science. Hopefully someday we can return to a free, open access Google Maps. After all, open science benefits scientists, the general public, and corporations–even Google.

Cat at a laptop: Scientific writing in R Markdown

How I felt when first trying to work in R Markdown.

Writing can be scary. Writing can be scary for everyone, not just us scientists. But whether or not we enjoy it, or think we’re good at it, it’s probably the best tool for communicating our findings. So removing as much pain from the process is key.

That’s why I’ve started using R Markdown for writing.

If you’re like me, the worst part about writing scientific papers is formatting. I hate it. I hate getting bogged down in font size, citation style, line numbers–all that stuff. Not only does it take me forever to get just right, but it gives me so much room to mess up stuff that isn’t based in content. If I’m spending time fighting with format, that’s time away from thinking about stuff that really matters. And the idea of switching between different journals’ format style makes me want to cry. R Markdown made worrying about that a thing of the past.

But perhaps even better than the formatting convenience R Markdown provides, it makes collaboration so much easier. This is especially true when you pair R Studio with your Github account. All changes and additional files referenced are all neatly connected, and any code printout included in your paper is already sitting in your paper.

So, I’ve switched to writing in R Markdown. I’ve always worked in either Word or Google Docs, and I still will if I’m writing something that isn’t going to require a lot of coordinating; but for big projects, I’m moving on up. I’m ready to get productive.

When I first tried this new step in my workflow, I felt less than skilled. I have experience in R Studio and Markdown, but when learning anything new I feel like a cat trying to type. So here’s some important tips I’ve collected from my first time through the process to hopefully make it easier.

  1. Define and fill the space R will reference when filling in format details. Three dashes (—) start and end the referential space, so write any parameters you want to fill followed by a colon and the content you want associated with it (title: Scientific Writing in R Markdown). When you create a new .rmd file, this is already started for you. Some parameters require a little extra characters, like abstracts or authors.  You’ll also need to include which output you want (a specific journal, word doc, html, pdf, etc.). If you want to format in a specific journal style, you can look up different csl (citation style and language) codes to reference journals here. You’ll also need to install and run rticles package. The rticles package allows you to reference different journal format styles so your .rmd can knit to that format style. After you finish the referential section, begin writing your paper outside the ending three dashes.
  2. Know and use your syntax. Writing in R Markdown means you’re writing in plain text as opposed to rich text. Rich text is when you’re writing but you have all these different formatting options–italics, font, colors–all the formatting options you can see in the the GUI interface. This is what you’re working with when you’re in Word. Plain text, which is just the text characters, is what you’ll use whenever you’re working in R. In order to get things like italics, or numbered lists, or bold, you need to use certain syntax. The rich text formatting will appear after you knit. Once you get used to this, it’s snap (here’s a handy guide to syntax). Plus, it’s one less thing to distract you when you’re trying to focus on content and ideas.
  3. Understand citations. Probably my single favorite thing about R Markdown is the ease with which I can include citations. It took me a minute to figure out the steps, but once I did, I never want to type out a citation or use a Word plugin again. All you have to do is export whichever papers you could possibly want to cite from your reference manager (I use Mendeley) into a .bib file. Notice what your citation key is. For Mendeley, it automatically formats your key to be author and year (@Lemon2018). After you create this, make sure your bibliography reference in your .rmd is your new .bib file. If you know your citation key, all you need to add a parenthetical citation is include [@author]. For example, you might type: “A cat like to be scratched behind its ears [@Lemon2018]”. This will automatically populate the entire citation at the end of the document. If you want to include multiple citations in one parenthetical, simply separate the keys with a semi-colon [@Lemon2017;@Lemon2014].
  4. Code! Don’t forget you’re writing in R Studio, so being able to directly code is a huge advantage of working in R Markdown. You can include any figures or tables you would in R Studio, just insert a new chunk. For tables, I recommend the kable function in the knitr package which creates an attractive table from a dataframe you already have. Just be sure to include “include=FALSE” at the beginning of your chunk so you only see the outputs of your code. Here’s a video that shows side-by-side screens of coding/writing in Markdown and how the code will look after knitting.

For me, it was a steep learning curve to make the transition from rich text programs to R markdown. In this post, I included some introductory tips for switching to R Markdown. There are lots of more advanced options with R Markdown, but for this post I wanted to focus on the challenges that I  struggled with while writing my first paper in an .rmd file. This doesn’t include steps that I found intuitive, or questions that are associated with learning to code in R, or tricks that are so advanced that I didn’t run into them. But I found the answers to most of my questions by scouring the web, so even if I didn’t answer something here, the answer is probably out there. Hopefully, the tips I devised can help an intermediate R coder get the most out of their work with R Markdown.