Stage 3 Talent

A Pirate’s Favorite Programming Language – R

International Talk Like a Pirate Day by Stage 3 Talent

Ahoy, me hearties! September 19 is International Talk Like a Pirate Day, and here at Stage 3 Talent, our seadog Sadukie wants to show ye scallywags the coffers of a pirate’s favorite data programming language – R!

For the lads and lassies out there who have heard of R, they know it has a place in data for data gathering and analysis. One of the sources for gathering data is an API.

We can’t be the only pirates making sure our data is in shipshape. So we want to show you how you can learn pirate using R and an API.

First, we need our libraries to help us call out to the API:

library(httr)
library(jsonlite)

Next, we want to call out to the API. We are using the FunTranslations.com Pirate Speak translator to help us out. Specifically, we are looking at their Pirate translator API.

We’ll start with the phrase “Hello my friend” To make it easier to write, we’re storing our message in a variable called textToTranslate. We will print out the response.

Our R code looks like this:

url ="https://api.funtranslations.com/translate/pirate.json"
textToTranslate = "Hello my friend"
res = GET(url,query = list(text = URLencode(textToTranslate)))
res

The JSON response looks like this:

Response [https://api.funtranslations.com/translate/pirate.json?text=Hello%2520my%2520friend]
  Date: 2020-09-18 12:52
  Status: 200
  Content-Type: application/json; charset=utf-8
  Size: 185 B
{
    "success": {
        "total": 1
    },
    "contents": {
        "translated": "Ahoy%20my%20friend",
        "text": "Hello%20my%20friend",
        "translation": "pirate"
    }

We stored our response in the res variable. To access the content we can use res$content. rawToChar will allow us to convert it from raw data to a string.

'{\n    "success": {\n        "total": 1\n    },\n    "contents": {\n        "translated": "Ahoy%20my%20friend",\n        "text": "Hello%20my%20friend",\n        "translation": "pirate"\n    }\n}'

R has a handy function called fromJSON in the jsonlite library. We are going to store that in a variable named data. We will use the names function to get the properties of the data object.

data = fromJSON(rawToChar(res$content))
names(data)

The properties come back as:

'success''contents'

We want to get our translation so that we can learn pirate with this translator’s help. To get our translation, we will access data$contents.

data$contents

The output comes back with encoded values:

$translated
'Ahoy%20my%20friend'
$text
'Hello%20my%20friend'
$translation
'pirate'

The translation is in $translated. Since this is URL encoded, we can use the URLdecode function to clear up our response.

Our code looks like this:

URLdecode(data$contents$translated)

The translation came back from the API as:

'Ahoy my friend'

While we are a little surprised that friend didn’t get translated to heartie or matey, we did show ye how to call an API to learn how to speak Pirate on this International Talk Like a Pirate Day!

While this is a fun example, this tool can be used to call serious APIs to import data from a data source and then analyze.

Here at Stage 3 Talent, we have a data curriculum with a little something for everybody – including our Data Literacy for those business folks who get data presented and need to understand those presentations in order to make an informed decision, Data Analysis for those who want to process their own data without waiting for someone else to do it, and Data Engineering for those who have developers who can help them with gathering, processing, and analyzing data using Python.

For more information about our data courses, contact us today!