Interactive reports and webpages with R & Shiny

OUHSC Statistical Computing User Group

Will Beasley, Dept of Pediatrics,

Biomedical and Behavioral Methodology Core (BBMC)

October 6, 2015

Overview of Shiny

R doesn't work well when called by a conventional website. RStudio's developers built the Shiny web framework that permits this integration.

  • Requires a UI (user interface) and server file.
  • Everything can be written in R. Shiny will create & translate to the necessary HTML5, JavaScript & CSS components. (But these can be written to create fancy reports.)
  • Can leverage almost all R capabilities.
  • Great official gallery, user examples, tutorials, and documentation, and presence in forums and StackOverflow.

Our Tipping Point

  • In 2012, our 8 data collectors need fresh reports every few hours; the report has stats, tables, & graphs, suggesting R.
  • Our previous best option was to install (a) R & knitr and (b) the report code on each laptop. Updates were required too.
  • Shiny avoids the deployment hassle. Our campus server contains the stat & reporting software. The data collectors need only a browser.
  • Many deployment scenarios have much more than 8 users, further suggesting Shiny.

Screen Shots

(Switch to the slides of screen shots.)

Official Gallery

(Switch to the RStudio's Gallery.)

Areas of Interest of Today's Group

  • What are people's interest & experience.
  • Three categories to work out.
    • Upstream data flowing
    • Server Code
    • UI Code

Code

(Switch back to these slides.)

Disclaimer: The following code is a simplication of our real code. The basics are the same, but

  • encapsulation was flattened, and
  • non-essential functions were omitted.

Retrieve Token

channel <- odbcConnect(dsn="BbmcSecurity")
token <- REDCapR::retrieve_token_mssql(
  project_name = "Gpav2", 
  channel      = channel
)
uri <- RODBC::sqlQuery(
  channel, 
  "EXEC Security.prcUriStatic @UriName 
    = 'RedcapBbmc'", 
  stringsAsFactors=FALSE
)[1, 'Value']
RODBC::odbcClose(channel)

Pull From REDCap (& Tidy a Litte)

# Pull
dsDC <- REDCapR::redcap_read(
  redcap_uri = redcapUri, 
  token      = token, 
  fields     = c("studyid", "dc")
)$data

# Tidy
dsDC <- dplyr::rename_(dsDC, 
  "study_id"       = "studyid",
  "dc_responsible" = "dc"
)
ds <- dplyr::left_join(
  x  = dsSchedule, 
  y  = dsDC, 
  by = "study_id"
)

Recurring Write from VM to Shiny

  • Every ~10 minutes, run the previous code on the VM and save a de-identified text file to Shiny. Use Task Scheduler in Windows, or Cron in Linux.
  • pathOut goes to a write-only directory on the Shiny server, exposed through Samba, so CSVs are saved on the network like a local drive.
  • R code couldn't be simpler:
pathOut <- "//shiny-public/dump/near.csv"
write.csv(ds, file=pathOut, row.names=F)

Our Current Shiny Instance is Public-Only

  • Data must be PHI-free. (Displayed tables and underlying CSVs.)
  • The instance currently run by the BBMC lacks user-authentication.
  • When PHI isn't needed, this is a huge simplification.
  • The commericial versions of Shiny have many authentication options. It was a breeze in a non-OUHSC server I helped with. (I rarely make these claims.)

Three Connectivity Approaches

  • One-way from CSV caches (our current approach).
    • Simple approach w/ few security concerns.
  • One-way from live database.
    • Real-time data requires many security measures.
    • We plan to create an instance like this.
  • Two-way to/from live database.
    • Even more flexibility and risk.

Deploying Code to Shiny Server

  1. Develop on your desktop, not on the live server.
  2. Commit & push to GitHub repo.
  3. Admin pulls updated files to server's copy of repo.

Helps insulate multiple developers on the same Shiny app.

Finally Getting to Shiny Code

  • I've spent so much time on the upstream material because it's the hardest to find online.
  • Most public Shiny articles/examples don't use PHI.
  • The PHI-free examples are really good and easy to find.

Concepts from Telephone Example

http://shiny.rstudio.com/gallery/telephones-by-region.html

  • ui vs server worlds
    • layout vs function
    • client-side vs server-side
  • connecting/referencing components across worlds

Concepts from DataTable Example

http://shiny.rstudio.com/gallery/datatables-demo.html

  • The JavaScript DataTables library is neatly wrapped & exposed through R code.
  • Let the established library do the heavy lifting for you.
  • RStudio's frequently leverages established open source components.
    • Leverages documentation.
    • Leverages innovation in the field.
    • Leverages your existing knowledge.

Concepts from Reactivity Example

http://shiny.rstudio.com/gallery/reactivity.html

  • Call chain (much more here).
  • Lazily evaluates terms ('lazy' is good here; it's efficient).
  • Watch the yellow highlights respond.

What's Available to You Through

  • shinyapps.io
  • Our public BBMC Shiny Server…
  • Our upcoming secured BBMC Shiny Server…
  • Our Shiny code & server installation scripts…

Immediate Influence & Assistance