Skip to content

Instantly share code, notes, and snippets.

@slopp
Created January 30, 2017 18:35
Show Gist options
  • Save slopp/8f9bed338a8f1d89bf0a0d23ecfaad9c to your computer and use it in GitHub Desktop.
Save slopp/8f9bed338a8f1d89bf0a0d23ecfaad9c to your computer and use it in GitHub Desktop.
---
title: "Untitled"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
library(shiny)
library(ggplot2)
library(htmltools)
```
Inputs {.sidebar}
----------------------
```{r}
selectInput("dataset", "Pick Dataset", choices = c("mtcars", "iris"), selected = "iris")
# This will hold column dropdowns and "Add plot" button
uiOutput("column_ui")
```
Column
------------
```{r}
# pick the dataset
dataset <- reactive({
eval(parse(text = input$dataset))
})
# Let user choose columns, and add plot.
output$column_ui <- renderUI({
choices <- c("Choose one" = "", names(dataset()))
tagList(
selectInput("xvar", "X variable", choices),
selectInput("yvar", "Y variable", choices),
conditionalPanel("input.xvar && input.yvar",
actionButton("addplot", "Add plot")
)
)
})
# One of the very few times you'll see me create a non-reactive
# session-level variable, and mutate it from within an observer
plot_count <- 0
# Add a plot when addplot is clicked
observeEvent(input$addplot, {
plot_count <<- plot_count + 1
id <- paste0("plot", plot_count)
# Take a static snapshot of xvar/yvar; the renderPlot we're
# creating here cares only what their values are now, not in
# the future.
xvar <- input$xvar
yvar <- input$yvar
output[[id]] <- renderPlot({
df <- brushedPoints(dataset(), input$brush, allRows = TRUE)
ggplot(df, aes_string(xvar, yvar, color = "selected_")) +
geom_point(alpha = 0.6) +
scale_color_manual(values = c("black", "green")) +
guides(color = FALSE) +
xlab(xvar) + ylab(yvar)
})
insertUI("#section-column", where = "afterEnd",
ui = div(class = 'chart-title', id,
div(class = 'section level3 chart-wrapper chart-wrapper-flex', style = 'flex: 478 478 0px;',
plotOutput(id, brush = "brush", width = 275, height = 275)
))
)
})
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment