JohnCoene / firebase

Google FIrebase for shiny
https://firebase.john-coene.com
GNU Affero General Public License v3.0
170 stars 26 forks source link

How should firebase be integrated with a shinydashboard app? #41

Closed paladinic closed 2 years ago

paladinic commented 2 years ago

Hi,

I am trying to use the firebase lib in a shiny app built using shinydashboard. I tried adjusting the example provided in the documentation but I don't think this is the correct way of doing this. Is there a suggested way to integrate the two libs?


library(shiny)
library(shinydashboard)
library(firebase)

ui <- dashboardPage(
  header = dashboardHeader(title = 'title'),
  sidebar = dashboardSidebar(),
  body = dashboardBody(
    useFirebase(),
    firebaseUIContainer(),
    reqSignin(actionButton("signout", "Sign out")),
    uiOutput("msg"),
    plotOutput("plot")
  )
)

server <- function(input, output){
  f <- FirebaseUI$
    new("session")$
    set_providers(
      email = TRUE,
      google = TRUE
    )$
    launch()

  output$plot <- renderPlot({
    f$req_sign_in() # require sign in
    plot(cars)
  })

  output$msg <- renderUI({
    f$req_sign_in() # require sign in

    user <- f$get_signed_in() # get logged in user info
    print(user)

    h4("Welcome,", user$response$displayName)
  })

  observeEvent(input$signout, {
    f$sign_out()
  })

}

shinyApp(ui, server)
JohnCoene commented 2 years ago

Worked for me aft CTRL + SHIFT + R

paladinic commented 2 years ago

I was wondering if there is a right way to do this, especially when it comes to dealing with multiple tabs. The example below expands a bit on that showing how I though this could be implemented.

library(shiny)
library(shinydashboard)
library(firebase)

ui <- dashboardPage(
  header = dashboardHeader(title = 'title'),
  sidebar = dashboardSidebar(sidebarMenu(
    menuItem(
      "login",
      tabName = "login",
      icon = icon(lib = 'glyphicon', "th")
    ),
    menuItem(
      "start",
      tabName = "start",
      icon = icon(lib = 'glyphicon', "th")
    )
  )),
  body = dashboardBody(
    useFirebase(),
    firebaseUIContainer(),
    tabItems(
      tabItem(tabName = 'login',reqSignin(actionButton("signout", "Sign out"))),
      tabItem(
        tabName = 'start',
        plotOutput("plot")
      )
    )
  )
)

server <- function(input, output) {
  f <- FirebaseUI$new("session")$set_providers(email = TRUE,
                                               google = TRUE)$launch()

  output$plot <- renderPlot({
    f$req_sign_in() 
    plot(cars)
  })

  observeEvent(input$signout, {
    f$sign_out()
  })

}

shinyApp(ui, server)
JohnCoene commented 2 years ago

See https://github.com/JohnCoene/firebase/issues/5 there isn't a nice elegant solution currently