library(shiny)# User Interfaceui <-fluidPage()# Serverserver <-function(input, output, session) {}shinyApp(ui, server)
Structure of a shiny app
library(shiny)# User Interfaceui <-fluidPage(# Display inputs and outputs)# Serverserver <-function(input, output, session) {# Calculate chances of survival from the user inputs}shinyApp(ui, server)
Scaling the app via Firebase
Authentication
Creating, Reading, Writing, Updating, and Deleting user data (Firestore)
tagList(# sexradioGroupButtons(inputId ="sex",label ="Are you a girl or a boy?",choices =list("Girl"= 1L, "Boy"= 0L),status ="primary",selected =character(0) ),# agesliderInput(inputId ="age",label ="What is your age?",min =12,max =18,value =15,step =1 ),# careerradioGroupButtons(inputId ="career",label ="I am a career tribute.",choices =list("Yes"= 1L, "No"= 0L),status ="primary",selected =character(0) ),# ratingsliderInput(inputId ="rating",label ="How would you rate your survival skills?",min =3,max =11,value =5,step =1 ))
Getting inputs from user
UI:
tagList(# sexradioGroupButtons(inputId ="sex",label ="Are you a girl or a boy?",choices =list("Girl"= 1L, "Boy"= 0L),status ="primary",selected =character(0) ),# agesliderInput(inputId ="age",label ="What is your age?",min =12,max =18,value =15,step =1 ),# careerradioGroupButtons(inputId ="career",label ="I am a career tribute.",choices =list("Yes"= 1L, "No"= 0L),status ="primary",selected =character(0) ),# ratingsliderInput(inputId ="rating",label ="How would you rate your survival skills?",min =3,max =11,value =5,step =1 ))
Server:
# Form inputs in one listform_inputs <-reactive({list(sex = input$sex,age = input$age,career = input$career,rating = input$rating )})# Send data to firestore when 'Submit' is clickedobserveEvent( input$form_submit, {## Convert data to json structure form_data_list <-toJSON(list(fields =list(uid =list("stringValue"=email()),Sex =list("integerValue"=form_inputs()[["sex"]]),Age =list("integerValue"=form_inputs()[["age"]]),Career =list("integerValue"=form_inputs()[["career"]]),Rating =list("integerValue"=form_inputs()[["rating"]]) ) ), auto_unbox =TRUE)## writing data endpoint_quiz <-"projects/gdg-demo-b8928/databases/(default)/documents/Quiz" write.db <-function(db_endpoint, data, auth_token) { r <- httr::POST(sprintf("https://firestore.googleapis.com/v1beta1/%s", db_endpoint), httr::add_headers("Content-Type"="application/json","Authorization"=paste("Bearer", auth_token) ),body = data )return(r)} write_request_quiz <-write.db(db_endpoint =paste0(endpoint_quiz, "?documentId=", email()),data = form_data_list,auth_token =accessToken() ) })