Skip to content

Creating Shiny apps with R

Himal Shrestha edited this page Jun 30, 2023 · 1 revision

7. Allow users to upload and download the data

i. Allow users to upload and download data from an app

  • It's simple
  • downloadButton() in ui.R
  • downloadHandler() in server.R
  • ​paste0(Sys.Date(), "_bechdel-ratings.csv")

ii. Download data from DT tables

  • Most flexible soln to download data from table is to used DT package
  • Extensions argument
  • Dom argument - Bftip

iii. Allow users to upload data to an app

Will be useful for map generation

iv. Uploading files into Shiny App

  • Use fileInput("uploaded_file") to upload files in ui.R
  • In server.R, use input$uploaded_file$datapath to access the uploaded file
  • Uploaded files will be created in a temporary directory, and will be deleted when the app exits
    • If you want to keep the file permanently, it should be explicitly stated in the code
  • Need to create a reactive variable in the server.R to store the uploaded file
    • uploaded_file <- reactive({read.csv(input$uploaded_file$datapath)})
    • input$uploaded_file$datapath is a reactive expression, so it needs to be wrapped in reactive()

v. Using rhandsontable to collect data

  • rhansontable is a htmlwidget package
  • rhansontable library should be loaded in both ui.R and server.R

8. Problem solving in Shiny apps

====================================

i. Problem solving in Shiny apps 101

  • Misspelling the variables between ui.R and server.R

  • Deploying shiny apps on making available to colleagues and customers

  • Shiny app that works on your local machine might not necessarily work when deployed

  • Three skills you need

    1. Debugging R code
    2. Debugging web content (HTML, CSS, JavaScript)
    3. Debuggin deploying issues
  • Debugging steps R code

    • Use print() to send variables/data to the console
    • Use shinyjs::runCodeUI() and shinyjs::runcodeServer() to interactively explore R variables in your Shiny app
    • Use shiny::reactLog() to explore the issues with reactivity
  • Debugging web components

    • Use web browser's inspector to investigate the issues and change CSS/HTML attributes dynamically
    • shinyjs::showLog() to show the log of JavaScript events
  • Appropriate packages are loaded in ui.R and server.R

  • All file paths must be relative to Shiny app's root directory

  • Inlude all code in the server.R or use source("data-processing.R", local = TRUE) to source external files

  • Check issues is not due to difference in time zones in the date function

  • When using APIs, ensure tokens are sent to the server and are not locked in the local machine

====================================

ii. Printing to the R console in Shiny apps

  • We cannot access the value of input variables in outside reactive expressions
  • You can use print function to within reactive expressions like render plot, event reactive, observe event and its cousins
    • The examples of reactive expressions are renderPlot(), eventReactive(), observeEvent(), observe(), reactive(), reactiveValues()

ii. Debugging Shiny apps with shinyjs::runcodeUI

  • Load shinyjs library in both ui.R and server.R
  • Add folowing block of code to ui.R inside fluidPage()
    • useShinyjs()
    • runcodeUI(type = "textarea")
  • Add runCodeServer() to server.R inside shinyServer() function

iii. Using reactlog to debug Shiny apps

  • reactlog is a package that helps to debug reactive components of the Shiny app
  • options(shiny.reactlog = TRUE) in R console to enable reactlog
  • Ctrl + F3 to open reactlog or Cmd + F3 in Mac

====================================

9. Making Shniy apps beautiful

i. Applying custom CSS to Shiny apps

  • ==CSS: Cascading Style Sheets== - all web contents are styled using CSS
  • CSS can be applied "inline" to specific Shiny elements
  • Style and entire app using theme argument of fluidPage()/navbarPage()
  • Always save CSS and JavaScript files in www subfolder folder
  • Themes are all changed within the ui.R file
  • Problem: Pull down menu is below the zoom (+/-) button
  • Solution: Add the selectInput() within the div() function and add style = "postion:relative;z-index: 1000;" to the div() function

ii. Inserting images to Shiny apps

  • Different ways to insert images in Shiny app
  • img(src = "images/plot.png") in ui.R
  • RenderImage() in server.R and imageOutput() in ui.R
  • HTML tags in ui.R - <img> </img> tags

iii. Show loading spinners in Shiny apps

  • Good idea to use spinners when a function takes couple of seconds to load
  • Very nice function; could be used in ==mapping Shiny app==
  • withLoader() function in shinycustomloader package in ui.R file

====================================

10. Shiny app deployment

i. Where can you deploy shiny apps?

  • Value in Shiny apps is in sharing them with others
  • Two options for hosting
    • Self-hosting
    • Cloud hosting
  • Self-hosting is a bit of a misnomer as it usually means running server applications on a virtual private server (VPS) like Amazon Web Services (AWS), Azure, or Digital Ocean
  • Cloud hosting - sinyapps.io, RStudio Connect, Shiny Server Pro
  • Shiny apps if used commercially might require a self hosted architecture due to data provisioning requirements
  • shinyapps.io is not currently GDPR compliant and unlikely to be in the near future
    • GDPR - General Data Protection Regulation

ii. Deploying Shiny apps to shinyapps.io

  • Copy the secret key from the shinyapps.io account to RStudio while publishing the app
  • The more complicated your app is, the more time it takes to deploy
  • Here is your deployed test app: https://himal-shrestha.shinyapps.io/testapp/

iii. Managing Shiny apps with RStudio Connect

iv. Programmatically deploying Shiny apps

Jun 28

Made shiny app to accept inputs and then plot the map of the country

Clone this wiki locally