Firebase database data to R

2024/10/4 3:23:15

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.

Answer

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)
https://en.xdnf.cn/q/70659.html

Related Q&A

Django 1.8 Migrations - NoneType object has no attribute _meta

Attempting to migrate a project from Django 1.7 to 1.8. After wrestling with code errors, Im able to get migrations to run. However, when I try to migrate, Im given the error "NoneType object has …

Manage dependencies of git submodules with poetry

We have a repository app-lib that is used as sub-module in 4 other repos and in each I have to add all dependencies for the sub-module. So if I add/remove a dependency in app-lib I have to adjust all o…

Create Boxplot Grouped By Column

I have a Pandas DataFrame, df, that has a price column and a year column. I want to create a boxplot after grouping the rows based on their year. Heres an example: import pandas as pd temp = pd.DataF…

How can I configure gunicorn to use a consistent error log format?

I am using Gunicorn in front of a Python Flask app. I am able to configure the access log format using the --access-log-format command line parameter when I run gunicorn. But I cant figure out how to c…

Implementing seq2seq with beam search

Im now implementing seq2seq model based on the example code that tensorflow provides. And I want to get a top-5 decoder outputs to do a reinforcement learning.However, they implemented translation mode…

Pandas Random Weighted Choice

I would like to randomly select a value in consideration of weightings using Pandas.df:0 1 2 3 4 5 0 40 5 20 10 35 25 1 24 3 12 6 21 15 2 72 9 36 18 63 45 3 8 1 4 2 7 5 4 16 2 8 4…

Matplotlib TypeError: NoneType object is not callable

Ive run this code many times but now its failing. Matplotlib wont work for any example, even the most trivial. This is the error Im getting, but Im not sure what to make of it. I know this is vague and…

Resize image faster in OpenCV Python

I have a lot of image files in a folder (5M+). These images are of different sizes. I want to resize these images to 128x128. I used the following function in a loop to resize in Python using OpenCVdef…

How to install Yandex CatBoost on Anaconda x64?

Iv successfully installed CatBoost via pip install catboostBut Iv got errors, when I tried sample python script in Jupiter Notebookimport numpy as np from catboost import CatBoostClassifierImportError:…

pyspark returns a no module named error for a custom module

I would like to import a .py file that contains some modules. I have saved the files init.py and util_func.py under this folder:/usr/local/lib/python3.4/site-packages/myutilThe util_func.py contains al…