-
Notifications
You must be signed in to change notification settings - Fork 0
Creating Shiny apps with R
- It's simple
-
downloadButton()inui.R -
downloadHandler()inserver.R -
paste0(Sys.Date(), "_bechdel-ratings.csv")
- Most flexible soln to download data from table is to used DT package
- Extensions argument
- Dom argument -
Bftip
Will be useful for map generation
- Use
fileInput("uploaded_file")to upload files inui.R - In
server.R, useinput$uploaded_file$datapathto 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.Rto store the uploaded fileuploaded_file <- reactive({read.csv(input$uploaded_file$datapath)})-
input$uploaded_file$datapathis a reactive expression, so it needs to be wrapped inreactive()
-
rhansontableis a htmlwidget package -
rhansontablelibrary should be loaded in bothui.Randserver.R
====================================
-
Misspelling the variables between
ui.Randserver.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
- Debugging R code
- Debugging web content (HTML, CSS, JavaScript)
- Debuggin deploying issues
-
Debugging steps R code
- Use
print()to send variables/data to the console - Use
shinyjs::runCodeUI()andshinyjs::runcodeServer()to interactively explore R variables in your Shiny app - Use
shiny::reactLog()to explore the issues with reactivity
- Use
-
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.Randserver.R -
All file paths must be relative to Shiny app's root directory
-
Inlude all code in the
server.Ror usesource("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
====================================
- 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()
- The examples of reactive expressions are
- Load
shinyjslibrary in bothui.Randserver.R - Add folowing block of code to
ui.RinsidefluidPage()useShinyjs()runcodeUI(type = "textarea")
- Add
runCodeServer()toserver.RinsideshinyServer()function
-
reactlogis a package that helps to debug reactive components of the Shiny app -
options(shiny.reactlog = TRUE)in R console to enable reactlog -
Ctrl + F3to open reactlog orCmd + F3in Mac
====================================
- ==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
wwwsubfolder folder - Themes are all changed within the
ui.Rfile - Problem: Pull down menu is below the zoom (+/-) button
- Solution: Add the selectInput() within the
div()function and addstyle = "postion:relative;z-index: 1000;"to thediv()function
- Different ways to insert images in Shiny app
-
img(src = "images/plot.png")inui.R -
RenderImage()inserver.RandimageOutput()inui.R - HTML tags in
ui.R-<img> </img>tags
- 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 inshinycustomloaderpackage inui.Rfile
====================================
- 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.iois not currently GDPR compliant and unlikely to be in the near future- GDPR - General Data Protection Regulation
- 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/
Made shiny app to accept inputs and then plot the map of the country