knitr::opts_chunk$set(echo = TRUE)
RStudio works with projects.
In the top right of the windown you will see a blue sign with R. Click on that.
Build
in the top menuConfigure Build Tools
. Choose website. Ok... we are good to go.
We need to load some libraries. You can find each one in Packages
and tick them or you can write the following in the Console
.
library(opsrdev) # the opsr package library(dplyr) library(readr)
OPS will work for very limited searches without authenticating.
When you signed up you were asked to obtain a key
and a secret
. You can save them in R to use later by typing the following in the console.
Note that the sign is <-
and the key and secret must be inside quotes "".
key <- "BiaLwkHwuus8WjXQPdvVajGGrLMkZnf9" secret <- "wkvTOQeuLwUGYxcw"
Next we can use this information to authenticate
library(opsrdev) ops_auth(key, secret)
library(opsrdev) ops_status()
We can only retrieve upto 2000 results at a time.
library(opsrdev) ops_count("drones")
Ok that's fine. If we had larger results we would want to break them down by year. Let's try pizza.
library(opsrdev) ops_count("pizza")
library(opsrdev) ops_count("pizza", "ta", start = 1990, end = 2015)
Still too many. Let's try a narrower set.
library(opsrdev) ops_count("pizza", "ta", start = 2010, end = 2015)
So, by experimenting we can arrive at a date range under the 2000 results that we could then retrieve.
APIs by definition work with URls.
The basic url for the EPO OPS service is
http://ops.epo.org/3.1/rest-services
We won't use this but just to show you how this works programatically. The ops_urls function will create the urls for you.
urls <- ops_urls(query = "pizza", type = "ti", start = 1990, end = 2000) urls
We want to fetch back some bibliographic data. We can use a function called ops_fetch_biblio()
. As the name suggests it fetches bibliographic data. It uses a set of controls where type = "ta" is titles and abstracts, while type = "ti"
library(opsrdev) drones <- ops_fetch_biblio(query = "drones", type = "ta", service = "biblio", start = 1990, end = 2015, timer = 10)
Let's see that as a table in RStudio.
View(drones)
We can also write this data to a .csv file. It will write the file to your project folder.
write_csv(drones, "drones.csv")
Don't forget quotes or .csv at the end.
Congratulations, you have now accessed data from an API using R and downloaded the results.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.