R shiny: send data to Firebase
R shiny: send data to Firebase
I would like to send some data to Firebase using shiny, this is a simplified version of my app.R
:
app.R
library(shiny)
library(shinyjs)
callFB <- "shinyjs.FB = function(){
var config = {
apiKey: 'my_apiKey',
databaseURL: 'my_databaseURL'
};
firebase.initializeApp(config);
var database = firebase.database();
var id = database.ref('test').push().key;
var myData = 'hello';
console.log(id); // OK
console.log(myData); // OK
console.log(database); // OK
database.ref('test/' + id + '/').update({myData}).then(function() {
console.log('myData sent!');
}); // ERROR
}"
ui <- fluidPage(
mainPanel(
tags$head(tags$script(src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js")),
tags$head(tags$script(src="https://www.gstatic.com/firebasejs/5.0.4/firebase.js")),
useShinyjs(),
extendShinyjs(text = callFB),
textOutput("sendFB")
)
)
server <- function(input, output, session) {
output$datauri <- renderText({
js$FB()
return("data sent")
})
}
shinyApp(ui = ui, server = server)
This however return an error:
> runApp()
Error: shinyjs: Error parsing the JavaScript code provided.
I already tested the js code in a normal html file and it works just fine. The line that seems to cause the error is the last one database.ref...
database.ref...
Any idea what may be causing this error? Is there an other way to handle this?
Edit
For some unknown reason this works:
library(shiny)
library(shinyjs)
callFB <- "shinyjs.FB = function(){
var config = {
apiKey: 'my_apiKey',
databaseURL: 'my_databaseURL'
};
firebase.initializeApp(config);
var database = firebase.database();
var id = database.ref('test').push().key;
var myData = 'hello';
database.ref('test/' + id + '/').update({myData}).then(function() {
console.log('myData sent!');
});
}"
ui <- fluidPage(
mainPanel(
tags$head(tags$script(src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js")),
tags$head(tags$script(src="https://www.gstatic.com/firebasejs/5.0.4/firebase.js")),
useShinyjs(),
textOutput("sendFB")
)
)
server <- function(input, output, session) {
output$datauri <- renderText({
runjs(callFB)
return("data sent")
})
}
shinyApp(ui = ui, server = server)
js$FB()
FB()
FB
comes from shinyjs.FB
, it's basically the way the function is called. There's an other example provided here: cran.r-project.org/web/packages/shinyjs/vignettes/…– mat
Jul 1 at 8:07
FB
shinyjs.FB
got it - thanks.
– SymbolixAU
Jul 1 at 8:10
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
in your example that doesn't work you're calling
js$FB()
- what'sFB()
?– SymbolixAU
Jun 30 at 22:16