Reading data from google sheets into R
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Reading data from google sheets into R is something you imagine should be really simple, but often is anything but. However, package googlesheets goes a long way to solving this problem.
Let’s crack on with an example.
First, install the software:
install.packages("googlesheets")
We then need an example sheet to work with, and I’m going to use one from Britain Elects:
For the latest council by election results and polls, be they Westminster or #IndyRef, see: https://t.co/dPyiwIqJ3O pic.twitter.com/mvyoCuIJgd
— Britain Elects (@britainelects) August 19, 2016
So go here and add this to your own collection (yes you’ll need a google account!) using the little “Add to my drive” icon to the right of the title, just next to the star.
Right, let’s play around a bit!
# load package library(googlesheets) # which google sheets do you have access to? # may ask you to authenticate in a browser! gs_ls() # get the Britain Elects google sheet be
This should show you all of the sheets you have access to, and also select the one we want.
Now we can see which worksheets exist within the sheet:
# list worksheets gs_ws_ls(be)
We can “download” one of the sheets using gs_read()
# get Westminster voting west
And hey presto, we have the data. Simples!
Now let’s plot it:
# reverse so that data are forward-sorted by time wdf
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.