Week 6: Introduction to Shiny and Reactive ProgrammingWeek 7: Input widgets and UI layout functionsWeek 8: Building Interactive UI Components- Week 9: Linking
ggplot2with Shiny for Dynamic Visualisation - Week 10: Final support session
ggplot2 with Shiny for Dynamic VisualisationBy the end of this session (and your own reading), you should be able to \(\dots\)
spellnameRows: 8,584 Columns: 12 $ ppt_id <dbl> 40, 40, 40, 40, 40, 40… $ resp <chr> "almond", "ambulance",… $ name_familiarised <lgl> TRUE, FALSE, FALSE, TR… $ cat <chr> "is natural", "is manm… $ ppt_vocab <dbl> 0.95, 0.95, 0.95, 0.95… $ modality <chr> "speech", "speech", "s… $ rt <dbl> 1312, 1057, 967, 1148,… $ dur <dbl> 649, 544, 800, 680, 58… $ spell_div <dbl> 0.5290947, 0.3919984, … $ name_div <dbl> 3.7269149, 0.1407271, … $ nsyl <dbl> 2, 3, 4, 3, 3, 3, 4, 2… $ aoa <dbl> 7.67, 6.16, NA, 6.28, …
Layout Type?
Plot Type?
Inputs Needed?
Multiple Plot Types?
Reactivity?
What would make a dataset interesting to explore interactively?
What’s the goal of your app?
If you have already identified a dataset:
Identify user actions:
Which of these would make sense for your dataset?
| Element | Your Idea |
|---|---|
| Dataset name | |
| Key variables | |
| Main question(s) | |
| Filters to include | |
| Visualisations | |
| Extra features |
psych40940-portfolio.Rproj.spellname dataset) inside your new R project.app.R.shinyapp.Start by creating an app that displays a single visualisation. Use your own data or the spellname dataset.
# Load data
spellname <- read_csv("spellname.csv")
# Create user interface
ui <- fluidPage(
plotOutput("myplot")
)
# Create server with a single plot
server <- function(input, output) {
output$myplot <- renderPlot({
# ...
})
}
Create a copy of your app and change fluidPage to navbarPage with a different visualisation per tab.
ui <- navbarPage("Many tabs app",
tabPanel("My plot 1",
plotOutput("myplot1")
),
tabPanel("My plot 2"
)
)
server <- function(input, output) {
output$myplot1 <- renderPlot({
# ...
})
}
Then, create a sidebar layout within both tabs but leave the sidebar panel empty for now.
sidebarLayout(
sidebarPanel(
),
mainPanel(
plotOutput("myplot1"),
)
)
In a tab of your choice, allow users to select variables from the input.
Define options the user can select from:
choices <- names(spellname)
but make sure these contain only numeric variables; or at least 2-3 numeric variables.
Add selecting option to user interface:
selectInput("var", "Title", choices = choices)
Link the selected variable to the plot:
ggplot(spellname, aes(x = .data[[input$var]]))
Allow user to filter the selected variables using a slider.
You need a placeholder in your UI for the user selection:
uiOutput("varout")
Add a reactivity that creates an input slider for the selected variable.
datavar <- reactive({
spellname[[input$var]]
})
output$varout <- renderUI({
# get the min and max values
rng <- range(datavar(), na.rm = TRUE)
# create a slider for the
sliderInput("dynamicVar",
input$var,
value = rng,
min = floor(rng[1]),
max = ceiling(rng[2]))
})
Use the values from the new slider to limit the displayed information:
ggplot(...) + ... coord_cartesian(xlim = input$dynamicVar)
Repeat this for another variable (e.g. for the y axis).
?plotOutput plotOutput( outputId, width = "100%", height = "400px", click = NULL, dblclick = NULL, hover = NULL, brush = NULL, # <- check this out! inline = FALSE, fill = !inline )
See Chapter 16 in Andrews (2021).
mainPanel(
plotOutput("scatterPlot",
brush = brushOpts(id = "selected_region")),
h4("Selected data"),
verbatimTextOutput("selected_info")
)
output$selected_info <- renderPrint({
brushedPoints(spellname, input$selected_region)
})
… allows the user to use their cursor to select data points and print them.
mainPanel(
...,
DT::DTOutput("selected_info")
)
output$selected_info <- DT::renderDT({
brushedPoints(spellname, input$selected_region)
})
DT package provides an interactive data table (hence, “DT”).renderDT that is connected to the UI via DTOutput.mainPanel(
plotOutput("scatterPlot",
brush = brushOpts(id = "selected_region")),
plotOutput("subplot")
)
xyrange <- reactiveValues(x = NULL, y = NULL)
observe({
sel_reg <- input$selected_region
xyrange$x <- c(sel_reg$xmin, sel_reg$xmax)
xyrange$y <- c(sel_reg$ymin, sel_reg$ymax)
})
output$subplot <- renderPlot({
ggplot(...) +
coord_cartesian(xlim = xyrange$x, ylim = xyrange$y)
})
Using fluidRow and column allows you to constrain the width of output elements.
mainPanel(
fluidRow(
column(5, # (must be between 1 and 12)
plotOutput("myplot1")),
column(5,
)
)
Adjust columns that both plots (zoomed in and not zoomed in) fit next to each other.
plotly# Load package library(plotly) # Check documentation for plotly output function ?plotlyOutput # Arguments for plotlyOutput plotlyOutput( outputId, width = "100%", height = "400px", inline = FALSE, reportTheme = TRUE, fill = !inline )
plotly# Load package library(plotly) # Create a ggplot2 plot and save it in `plot` plot <- ggplot(d_blomkvist, aes(x = ...)) + geom_point() # Make the visualisation interactive by using `ggplotly` ggplotly(plot)
For the plot in your tab with user selected variables turn the visualisation into an interactive plotly visualisation.
Make sure you’re using plotlyOutput (user interface) and renderPlotly (server).
plotlylibrary(plotly)
plot_ly(data = spellname,
x = ~freq,
y = ~rt,
z = ~dur,
color = ~modality,
type = "scatter3d",
mode = "markers",
marker = list(size = 3)
)
Add a new tab with sidebar layout that displays a 3D scatterplot.
Allow the user to select variables for the x, y and z axis:
selectInput to allow users to select variables.Use plotlyOutput (UI) and renderPlotly (server).
Review the assessment specification on NOW.
Some inspiration using features we discussed: Spellname-app
Recommended reading
Consider adding features that affect how to user is experiencing the app:
reactiveEvent) to update plots and slides instead of instant updates.helpText("Say something about the user's options.").You can also embed your app in an RMarkdown document: see “RMarkdown based Shiny apps” in (Andrews, 2021, Chapter 16: LINK)
Andrews, M. (2021). Doing data science in R: An introduction for Social Scientists. SAGE Publications Ltd.
Wickham, H. (2021). Mastering shiny. O’Reilly Media.