Getting your own Google API credentials for R

Inspiration for this post My quest to make Google Drive play nice with R continues! If you’ve read my last post, you’ll know the process for using googledrive and/or googlesheets4 to access data from Google Drive in a non-interactive context, e.g. deploying a data product to RStudio Connect. This process works wonderfully…most of the time. When deploying processes accessing Google Drive that are automated and run regularly, or simply make large requests of Drive, you can start to run into problems with Google Drive’s API quota.

Publishing to RStudio Connect with Google credentials

Inspiration for this post As a data scientist, I frequently need to pull data from disparate sources and combine it in an automated fashion for other teams to access. Google Drive is one of the main places where data is stored. I use the googledrive and googlesheets4 packages to access this data regularly. It’s fairly simple to use these packages when working in your RStudio console locally: there is a great vignette for googledrive here and one for googlesheets4 here.

Scheduling R Scripts

Inspiration for this post Since starting my job as a data scientist, I found myself setting dozens of reminders on my calendar to run certain scripts every day / week / month. I figured that there had to be a better way to deal with this, and after some research, I started off with creating cron jobs. After a few months of these running (with varying success), I learned about Launchd as well.

Adding featured images to posts

Inspiration for this post When I updated my website to the latest version of Hugo Academic a few months ago, I realized that I could add featured images to all of my blog posts. I was very excited about this, but none of my tinkering with the YAML of the RMarkdown could produce an image. After some digging, however, I was able to find how to format my files properly so the Academic theme worked to create the images.

Changing fonts in RMarkdown PDFs

Note: this post has been edited on 2 August 2020 - there was a typo in the indenting of the YAML. Thank you to Henry Hankins for catching this! Inspiration for this post After learning how to change the fonts within a graph in R, I wanted to be able to change the fonts in the body of an RMarkdown to match. I specifically wanted to be able to knit an RMarkdown to PDF using the Source Sans Pro Google font, but these instructions should work for any Google font.

Putting Google Fonts in R graphs

Inspiration for this post I’ve been working on creating a visualization cookbook for R graphics (check out this great example from the BBC to see what I’m eventually going for). As part of this process, I wanted to be able to change the font of my plots to Source Sans Pro, which is a Google font. sysfonts package The first step here was to install and load the sysfonts package:

Accessing Mode data in R

Inspiration for this post In the past, I’ve been used to a pretty straightforward R workflow: download my data as a csv from somewhere, save it on my computer, and go to work. Now that I work in healthcare, downloading data and saving it on my computer is no longer such a good idea (since that would be illegal) so I’ve needed other ways to get the data I need into R.

Copying R libraries to a USB

Inspiration for this post A few people in my lab are headed to a workshop in Colombia next week, and due to the possibility of intermittent wi-fi, they need to load all of the workshop materials onto USB drives. They need R with certain packages on the USBs in versions compatible with both Macs and PCs, so I offered to help out by loading my Mac version of R and my packages.

Setting resolution and aspect ratios in R

Inspiration for this post I recently needed to make some figures to present and noticed that the ones I was importing from saved R plots were showing up fuzzy on the presentation. Shortly afterwards, I had interns trying to generate figures with the same aspect ratio from different computers. I did a little digging and found this method for saving figures with specified resolutions and aspect ratios. Solution In order to save your figure with a certain size and resolution, you just need to include your code for plotting between the two lines below:

Thresholding species distribution models

Inspiration for this post Conservation is often the main motivation behind studying where a species lives – having a model of a species’ range can help scientists assess whether it is at risk of extinction, designate protected regions to preserve its habitat, and study potential impacts of human activity. When we create species distribution models using common methods like Maxent, the result is a map of predicted habitat suitability or probability of species presence, such as the one below.