The shiny app for the deworming OPA contains three files: ui.R
, server.R
, and all_analysis.R
.
01_final_opa
file contains the full walkthrough for the analysis, which is then represented in a more code-friendly way inall_analysis.R
- The data is
all_analysis.R
is passed to...ui.R
, which is the user interfaceserver.R
, which manages the server components of the shiny app
The purpose of the shiny app is to allow the user to experiment with the data to see the changes that those differences make, in order to better understand the analysis. It turns policy analysis, which is often complex and hard to understand, into an easily readable and understandable format.
-
To run the shiny app (SA) you will need to install R and RStudio. The relevant file can be found by running
ui.R
. -
Click 'Run App' located at the top of RStudio. This will open the SA.
-
The SA has three tabs to explore
- Main Policy Estimate, which describes the results. There is no interactive capabilities on this tab.
- Key Assumptions, where you can adjust 'policy estimate', 'unit costs in new country', and 'prevelance in the new region'.
- Main Assumptions, where you can adjust 'number of simulations' and 'policy estimate', as well as choose between 'Data' and 'Research'
- Data
- Gov Bonds (and SD)
- Inflation (and SD)
- Exchange rate (and SD)
- Costs of treatment (and SD)
- Years of treatment in original study (and SD)
- Years of treatment in new setting (and SD)
- Costs adjustments (and SD)
- Research
- Take-up (and SD)
- Take-up with no subsidy (and SD)
- Lambda 1_1_new (and SD)
- Lambda 1_2_new (and SD)
- Lambda 1_3_new (and SD)
- Prevalence in original study (and SD)
- Data
- The code begins with a list of all relevant libraries
- Then, it states the unit costs and the prevalences of 4 countries: India, Kenya, Nigeria, and Vietnam
- At the top of the SA, it says "Open Policy Analysis for Deworming Interventions: Open Output Component"
- This is created using
navbarPage
- This is created using
- All three tabs are created using
tabPanel
.- Then, use
sidebarPanel
, which creates the panel on the left side of the screen. For tab titled "Main Policy Estimate", this only consists of text and links.fluidrow
creates rows, such as "Unit costs in new country" in "Key Assumptions".
mainPanel
creates the graph seen on the right
- Then, use
shinyUI
-> creates a user interfacefluidPage
-> creates a pagenavbarPage
-> creates a page with a navigation bar at the toptabPanel
-> creates a tab panel, takes in a title (i.e. "Main Policy Estimate") and a valuesidebarPanel
-> creates a sidebar that has input controlstags$a
-> creates a linkmainPanel
-> creates a main panel of output elementsplotOutput
-> creates a plotcheckboxInput
-> creates a checkbox used to specify logical valuesnumericInput
-> creates an input control for numeric valueswithMathJax
-> loads in MathJax, a JavaScript libraryuseShinyjs
-> loads in Shinyjs, an r packagehelpText
-> creates text to help explain some element of the SAselectInput
-> creates a list of choicessliderInput
-> creates a slider widget containing numbers in a rangetabsetPanel
-> creates a tabset containing elements from tabPaneluiOutput
-> tells Shiny where the controls should be rendered
sim.data
-> returns a random sample from some distributionsobserveEvent
-> chooses which slider inputs to show and which sliders not to showggplot
-> data visualization packagegeom_density
-> computes and draws density estimategeom_vline
-> annotates the plot with vertical linesxlim
-> sets the min and max boundaries of the plot in the x-directionguides
-> the guide is set scale-by-scale for each scaleannotate
-> adds small annotations, i.e. text labelstheme
-> sets the theme of the plot by costumizing all non-data components
reactive.data1
is an R expression that uses a widget input and outputs a value, and in this case the input issim_data
.sim_data
sets variable names by turning the parameters used inui.R
to numeric factors- The following section of
server.R
isif
statements that are used to show which parameters to hide for different inputs of 'policy estimate'. - Then, the code adds helps text to the SA depending on the input for 'policy estimate'.
- The next section defines a function that generates policy estimate plots
If
statements specify what texts to display and whatselectInput
to call for each of the three different plots (Main, ka, all)- Then, the code constructs the plot for all three tabs
- All have the same axes
- All are density plots
- All share the same plot structure
-
In order to add a slider, you will want to use the
sliderInput
function inui. R
.sliderInput
is a widget that creates a slider across a range. It has five inputs: inputId, label, min, max, and value. Below is an example:sliderInput("param3", label = "Inflation (\\( \\pi \\) ) = ", min = 0.001, max = 0.2, value = inflation_so)
-
Here,
param3
is the inputID,Inflation (π) =
is the label, min is 0.001, max is 0.2, and the value isinflation_so
-
To add a new slider, you will first need to figure out what the inputID should be.
- First, you will need to add the inputID to the server file. Define a variable in the function
sim.data1
inserver.R
. For the example above this would beinflation_var2 = as.numeric(input$param3)
. simdata1
is contained in thereactive.data1
.
- First, you will need to add the inputID to the server file. Define a variable in the function
-
Then, if you don't want the slider to appear for certain inputs of 'policy estimate', insert it in
else if(input$policy_est == "What policy estimate you don't want the slider to appear in")
. -
It goes in the
list_hide
function within the else if. Once that has been added, that variable, in this caseinflation_var2
, should be added tosimdata1
inall_analysis.R
. This deals directly with the original analysis. -
EXPLAIN:
knitr::purl("code/01_final_opa.Rmd", "code/shiny_app/all_analysis.R")
-
If the slider you want to add is already in
all_analysis.R
, make sure to use the name defined inall_analysis.R
, then add it toserver.R
, and thenui.R
. -
To change the value, which is
inflation.so
in the above example, modify it in the "Data" section ofall_analysis.R
.
The line, knitr::purl("code/01_final_opa.Rmd", "code/shiny_app/all_analysis.R")
converts the R markdown in 01_final_opa.Rmd
to R script in all_analysis.R
.
This means that when data is updated or added in 01_final_opa.Rmd
, it will update the all_analysis.R
file.
One effective way to publicly share and show your shiny app (SA) for the deworming OPA is using shinyapps.io, an online hosting service RStudio provides. You can deploy and publish your SA by following the steps below.
- Create a shinyapps.io account and connect it with your RStudio.
- Make sure your codes can work in the shinyapps.io cloud.
*Please skip this step if you already have a shinyapps.io account.
-
In
ui.R
, click the icon to the right to "Run App" and select "Manage Accounts". -
In "Publishing" option, click "Connect..." to the right to "Publishing Accounts".
-
Select "ShinyApps.io" and click the "Get started here" link.
-
After signing up with shinyapps.io, following the instruction of RStudio, get, copy and paste the token. Finally, click "Connect Account".
-
Put the folders containing the related files in your local environment.
-
In Line 9 in
all_analysis.R
, turn "TRUE" into "FALSE" in theif
function. -
In Lines 55 and 56 in
all_analysis.R
, inactivate functions by putting "#" at the beginning. -
Then, in
ui.R
, click the icon to the right to "Run App" and select "Other Destination...". After making sure that all the boxes of the related files are checked, click "Publish".
If you have an error when deploying, please check "Logs" of the application through your shinyapps.io Dashboard to figure out the cause of the error.