Firebase Database Data for R
I have a database on google Firebase
that has streaming sensor data. I have an application Shiny
that needs to read this data and display sensors and their values.
I have been trying to fetch data from Firebase into R
but could not find a package that does this. The app is currently working with locally loaded data.
I found the package FireData
but have no idea how it works.
I know you can pull data from Firebase using Python
, but I don't know enough Python to do this, but I would be willing to code it in R using rPython
if needed.
I have: - Firebase project link - Username - Password
Has anyone tried Firebase and R / Shiny in the past?
I hope my question is clear enough.
source to share
The basics for getting started with R fireData are as follows. First you need to make sure you have set up your firebase account to GCP (Google Virtual Platform). By creating a new project and give it a name
Now that you have a project, select the option on the overview page that says "Add Firebase to your web application". It will provide you with all the necessary credentials.
[
One way to deal with this kind of information in R is to add it to a file .Renviron
so you don't have to share it with your code (for example, if it goes to github). There is a good description of 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 made any changes).
So now you can try. But first change your firebase database rules so everyone can make changes and read (for these examples work)
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 load function will return the name of the saved document, which you can then use to load it.
> upload(x = mtcars, projectURL = db_url, directory = "main")
[1] "main/-L3ObwzQltt8IKjBVgpm"
The data file (or value vector) you uploaded will be available in your Firebase Database Console under this name, so you can verify that everything went as expected.
Now, for example, if the name that was returned is read main/-L3ObwzQltt8IKjBVgpm
, then you can load it like this.
download(projectURL = db_url, fileName = "main/-L3ObwzQltt8IKjBVgpm")
You can require authentication after creating users. For example, you can create such users (users appear in firebase console).
createUser(projectAPI = api_key, email = "test@email.com", password = "test123")
Then you can get user information and token.
registered_user <- auth(api_key, email = "test@email.com", 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)
source to share