Shiny

From Dave's wiki
Jump to navigation Jump to search

Server functions

  1. Save objects to display to output$xx
  2. Build objects to display with render*()
  3. Use input values with input$xx

Tables

Note that with the DT package, the convention is to not load the DT package, and instead use the DT:: prefix when calling the datatable functions.

Reactivity

Reactivity automatically occurs when an input value is used to render an output object. Furthermore, reactive expressions only get executed when their input changes.

Reactive source -> reactive conductor -> reactive endpoint

Reactive conductors allow you to decompose large and complex steps into smaller pieces of code

Cached expression; by using a reactive expression for the subsetted data frame, we can subset once and use the result twice

reactive({})
  • observeEvent() is used to perform an action in response to an event
  • eventReactive() is used to create a calculated value that only updates in response to an event

Use isolate() to remove reactive dependency

# x will not change when input$var changes
x <- reactive({
   y() * isolate({ input$var }) * input$var2
})

Use action buttons to trigger.

actionButton(inputId = "action_button", label = "Go")

x <- reactive({
   input$action_button
   isolate({
      y() * input$var * input$var2
   })
})

Reactlog

The reactlog is a graphical representation of the dependency structure of your app; it provides you with detailed information about what's happening under the hood.

options(shiny.reactlog = TRUE)

Panels

Use panels to group multiple elements into a single element that has its own properties.

wellPanel()
sidebarPanel()
mainPanel()
titlePanel()

Use conditionalPale() to show/hide UI elements based on an input value

conditionalPanel(
   condition = "input.something == 'Something'",
   plotOutput("plot")
)

Fetch from database

https://support.rstudio.com/hc/en-us/articles/217592507-How-do-I-give-my-application-on-shinyapps-io-access-to-my-remote-database-

54.204.34.9
54.204.36.75
54.204.37.78

Upload

File inputs for uploading a text file

fileInput(inputId, label, ...)
?fileInput()

Accessing input$inputId is a data frame with one row per file. Each file has four variables: name, size, type, and datapath.

# read selected file
input$inputId$datapath

Download

Downloading files is achieved using the pair of functions downloadButton() and downloadHandler(). These two functions pair together similarly to how output and render functions are paired: downloadButton() determines where in the UI it will show up, while downloadHandler() needs to be saved into the output list and has the actual R code to create the downloaded file.

shinydashboard

Getting started

library(shinydashboard)

# Create an empty header
header <- dashboardHeader()

# Create an empty sidebar
sidebar <- dashboardSidebar()

# Create an empty body
body <- dashboardBody()

ui <- dashboardPage(header, sidebar, body)

server <- function(input, output) {}

shinyApp(ui, server)

Header

  • Messages
  • Notifications
  • Tasks
header <- dashboardHeader(
  dropdownMenu(type = "messages")
)

Sidebar and Body

CSS

It's possible to include CSS in your app by writing it in a separate file and importing it with includeCSS().There is also the simpler approach of placing the CSS code inside tags$style() in the UI.

Links