Recently I had the need to interact with some data stored in firebase’s firestore database via some R scripts. This was the first time that I interacted with firestore not via a package/library (e.g., node.js, python) that commonly make the full experience painless. Gladly, the REST API from firebase is intuitive enough, and with the help of the httr package in R, implementing the functions I needed was simple enough.
Anyway, I thought of writing this short tutorial, in case someone else finds it useful.
- Setting up the firebase project and authentication
- Creating an example database in firestore
- Interacting with the firestore database through R
Setting up the firebase project and authentication
For this example, the first thing to do is to create a firebase project. This can be quickly achieved by going to the firebase console, clicking on “Add project” and following the instructions.
Once the project is created, we go to “Build -> Authenitication” on the left panel:
Once there, we click on “Get started” and enable authentication via email, e.g.,:
And that’s it! Now we should be able to authenticate users for our project.
Creating an example database in firestore
Similarly to enabling authentication, now we select “Build> Firestore Database”, click on “create database” and follow the steps (for this tutorial, the default settings are fine).
Now, let’s create rules for who can access the data, by going to “Rules”:
The rules for this tutorial are as follows:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /mydata/{userIDFromWildcard}/data/{fileId} {
// Users can only edit documents in the database if the documentID is
// equal to their userID
allow read, write, update, delete: if request.auth.uid == userIDFromWildcard;
}
match /r-test/{data} {
allow read, write, update, delete: if request.auth.uid != null;
}
}
}
Here, I am indicating that there will be a couple of collections. The“mydata/{userIDFromWildcard}/data” will be each user’s private collection. In order for a user to write and read to that collection, its authentication user ID should match {userIDFromWildcard} so no other user can see or write the content of this collection. The “r-test/” collection is a semi-public collection where any authenticated user can read and write.
Now let’s see how to make use of this firestore database using R.
Interacting with the firestore database through R
In order to interact with our firebase project via the REST API, we need the Web API Key. So go to “Project settings” and copy the Web API Key.
Now, we will write a couple of functions in R to Sign Up and Sign In to our firebase project following the specifications of firebase REST API for authentication and user management.
Where “api_key” is the Web API key we copied previously. If everything has gone ok so far, we should be able to now sign up and sign in. For example:
> my_auth_info <- sign.up("gab@gab.com", "examplepass", api_key)
> nchar(my_auth_info$localId)
[1] 28
> nchar(my_auth_info$idToken)
[1] 916
Now let’s create the functions to write to and read from the database as detailed in the firestore REST API documentation.
Now, we should be able to write to the database collections (e.g., projects/r-firestore-example/databases/(default)/documents/r-test ). For example:
my_auth_info <- sign.in("gab1@gab.com", "examplepass", api_key)
data <- toJSON(list(fields = list(name=list("stringValue" = "Gabriel"),
favoriteNumber=list("integerValue" = "32343"))),
auto_unbox=TRUE)
write.db("projects/r-firestore-example/databases/(default)/documents/r-test", data, my_auth_info$idToken)
Here, we wrote the data object to our database. For documentation about the data format, visit this link.
That should produce:
Or writing to the “private” collection (`projects/r-firestore-example/databases/(default)/documents/mydata/”,my_auth_info$localId,”/data`)
Finally, we can read the data to we have written into the database:
d <- read.db("projects/r-firestore-example/databases/(default)/documents/r-test", my_auth_info$idToken)
fromJSON(content(d,"text"))
And that’s it. Hope you found this useful!