Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase database data to R

I have a database in Google Firebase that has streaming sensor data. I have a Shiny app that needs to read this data and map the sensors and their values.

I am trying to pull the data from Firebase into R, but couldn't find any package that does this. The app is currently running on local downloaded data.

I found the FireData package, but have no idea how it works.

I do know that you can pull data from Firebase with Python, but I don't know enough Python to do so, but I would be willing to code it in R with rPython if necessary.

I have: - The Firebase project link - The username - The password

Has anyone tried Firebase and R / Shiny in the past?

I hope my question is clear enough.

like image 294
Daniel Vargas Avatar asked Jul 21 '17 03:07

Daniel Vargas


1 Answers

The basics to get started with the R package fireData are as follows. First you need to make sure that you have set up a firebase account on GCP (Google Cloud Platform). Once there set up a new project and give it a name

Set up new firebase project

Now that you have a project select the option on the overview page that says "Add Firebase to your web app". It will give you all the credential information you need.

[]Get credentials for a web app[3]

One way of dealing with this kind of information in R is to add it to an .Renviron file so that you do not need to share it with your code (for example if it goes to github). There is a good description about how to manage .Renviron files in the Efficient R Programming Book.

API_KEY=AIzaSyBxxxxxxxxxxxxxxxxxxxLwX1sCBsFA
AUTH_DOMAIN=stackoverflow-1c4d6.firebaseapp.com
DATABASE_URL=https://stackoverflow-1c4d6.firebaseio.com
PROJECT_ID=stackoverflow-1c4d6

This will be available to your R session after you restart R (if you have made any changes).

So now you can try it out. But first, change the rules of your firebase Database to allow anyone to make changes and to read (for these examples to work)

Change firebase database permissions

Now you can run the following examples

library(fireData)
api_key <- Sys.getenv("API_KEY")
db_url <- Sys.getenv("DATABASE_URL")
project_id <- Sys.getenv("PROJECT_ID")
project_domain <- Sys.getenv("AUTH_DOMAIN")

upload(x = mtcars, projectURL = db_url, directory = "new")

The upload function will return the name of the document it saved, that you can then use to download it.

> upload(x = mtcars, projectURL = db_url, directory = "main")
[1] "main/-L3ObwzQltt8IKjBVgpm"   

The dataframe (or vector of value) you uploaded will be available in your Firebase Database Console immediately under that name, so you can verify that everything went as expected.

Now, for instance, if the name that was returned read main/-L3ObwzQltt8IKjBVgpm then you can download it as follows.

download(projectURL = db_url, fileName = "main/-L3ObwzQltt8IKjBVgpm")

You can require authentication, once you have created users. For example, you can create users like so (the users appear in your firebase console).

createUser(projectAPI = api_key, email = "[email protected]", password = "test123")

You can then get their user information and token.

registered_user <- auth(api_key, email = "[email protected]", password = "test123")

And then use the tokenID that is returned to access the files.

download(projectURL = db_url, fileName = "main/-L3ObwzQltt8IKjBVgpm", 
           secretKey = api_key, 
           token = registered_user$idToken)
like image 142
FvD Avatar answered Oct 17 '22 02:10

FvD