Creating Shiny Apps In R For Sharing Automated Statistical Products

Transcription

U.S. ARMY EVALUATION CENTERCreating Shiny Apps in R for SharingAutomated Statistical ProductsRandy Griffiths

Goal1. Understand basic structure of Shiny app code2. Produce simple apps3. Feel confident that you can create morecomplicated apps2

AgendaAssumptions & DisclaimersWhat is a Shiny app?How to make a Shiny appYour turn3

AssumptionsR and R Studio are installed on your computerWorking knowledge of R languageNever made a Shiny application4

Miscellaneousinstall.packages(“shiny”) if you have notalready done soGet to know your neighbor―You may need a friend as you work problems5

DisclaimersI am not a computer scientist―I don’t write elegant or efficient codeI am not an expert with web-page design―My knowledge of HTML and using CSS themes isembarrassingI have not worked with Shiny for very long―I made my first app in December 2016 https://aec-doe.shinyapps.io/Reliability Quick Calculations/ https://aec-doe.shinyapps.io/Likert plots/6My first Shiny apps are still being used in AECIf I can do it, so can you

What is a Shiny app?“Shiny is an R package that makes it easy to buildinteractive web apps straight from R. You can hoststandalone apps on a webpage or embed them in RMarkdown documents or build dashboards. You canalso extend your Shiny apps with CSS themes,htmlwidgets, and JavaScript actions.”(https://shiny.rstudio.com/)7

When is a Shiny app useful?You want to share a specific type of analysis withmany users with different data or other user input.You can do it in R but don’t trust them to use yourcode.8

How to make a Shiny appWhere to beginShiny website has a lot of free training material and alot of code.―https://shiny.rstudio.com/―Many examples with executable code―Widget Gallery (under Widgets) is extremely usefulUnderstand―User interface (UI) and server functions9

What is a Shiny app?Host Serverrunning RReceives input from UI forcomputing (math, plots,etc.)Creates objects and sendsspecified output to the UI10Client Webbrowser (UI)Collects input from UIand sends to server forcomputingReceives output fromserver for display

How to make a Shiny appUI Page LayoutshinyUI() wraps the UI contentfluidPage() is the most flexible page layout―You can split your “page”, or UI, into rows which canhave columns―I’ve never needed more rows than I was allowed―Column widths are defined on a 12 point scale within arow―Actual width will be scaled dynamically to fill thebrowser’s widthHost ServerClient Webrunning R11browser (UI)

How to make a Shiny appUI Page LayoutHost Serverrunning R12Client Webbrowser (UI)

How to make a Shiny appUI Page LayoutUI sends objects to the server via a list called “input”Shiny has many functions that create objectsautomatically and adds them to the input list whichcan be called on by the server as input nameThe widgets page has many useful examples―sliderInput(“name”, )―radioButtons(“name”, )―fileInput(“name”, )―numericInput(“name”, )Host Serverrunning R13Client Webbrowser (UI)

How to make a Shiny appServer receives UI inputshinyServer() includes the content sent to the UIand accepts a function as the argument. It is typicallyused with a function which takes two parameters: listsnamed input and outputshinyServer(function(input, output){code})Defining new output elements to be sent to UI is done by― output name - stuffHost Serverrunning R14Client Webbrowser (UI)

How to make a Shiny appServer using input from UIFunctions we will use to output results to UI include― ouput plotName - renderPlot({})― output tableName - renderTable({})Host Serverrunning R15Client Webbrowser (UI)

How to make a Shiny appGetting startedUsing R-Studio― Open new shiny Web App file to see an appstructure― Easier to develop apps using separate ui andserver files (choose that option)― ui.R structures the html file based on layoutfunctions and receives objects from server.R― server.R does the “R stuff” and receives objectsfrom ui.R (user inputs)― Passing objects between server and ui createsan interactive web appHost Serverrunning R16Client Webbrowser (UI)

How to make a Shiny appGetting startedAdd user options to the first column―What are the histogram inputs that you typicallymanipulate?―Look at the Widget code on the shiny app site for ideas tml―Recommendation: check box for auto-saving your filesbefore each buildHost Serverrunning R17Client Webbrowser (UI)

How to make a Shiny appMake a plot from user inputPracticeProvide a plot of random data from a distributionchosen by the user.Useful code:if(input dist 'norm'){x - rnorm(100)} else if(input dist 'exp'){fluidRow(column(2, ),x - rexp(100)column(10, )) defines a row} else {with 2 columns of relative widths 2x - rlnorm(100)and 10}br() is a vertical breakfluidPage() divide your pageinto two sections: input from userand the plot18

Adding ComplexityBefore long you notice your app is getting verycluttered. What can you do?―Add tabs along the top of page with navbarPage()―Add tabs within a page with tabsetPanel()―Look into shinydashboard for more complicatedstructurenavbarPagetabsetPanel19

Adding ComplexityUsing your existing app, add 3 tabs1.2.Plot (already coded)Data table output table - renderTable() withtableOutput(“table”)3.Table of summary statistics renderPrint(summary(data)) b 1”, ),tabPanel(“Tab 2”, ),tabPanel(“Tab 3”, ))20Extra Creditreactive({})

AdviceDevelop R code for a specific product first―You don’t want to be troubleshooting shiny code andcomplex code for output at the same time―Better to ensure code works well for specific outputoutside of Shiny, then wrap that code in a Shiny app“Run App” as often as possible―Don’t write a lot of code then try to run the app. It willlikely be painful to debug21

Using R-Studio ―Open new shiny Web App file to see an app structure ―Easier to develop apps using separate ui and server files (choose that option) ―ui.R structures the html file based on layout functions and receives objects from server.R ―server.R does the "R stuff" and receives objects from ui.R (user inputs)