https://slides.com/alexpawlowski/building-with-shiny/live/
29 April 2016
Knoxville R Users Group
@alexpawlowski
Alex Pawlowski
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin urna odio, aliquam vulputate faucibus id, elementum lobortis felis. Mauris urna dolor, placerat ac sagittis quis.
# ui.R
## Libraries ----
library(shiny)
library(plotly)
library(leaflet)
library(maps)
shinyUI(fluidPage(theme = "bootstrap.css",
# Google Analytics
tags$head(includeScript("www/google-analytics.js")),
# Application title
HTML('<img src="logo.svg" style = "max-height:130px" width = "100%"/>'),
fluidRow(column(12, align ="center",
h3("A Clean Power Plan Evaluation Tool")
)),
# ...
fluidRow(
selectizeInput("stateInput", #inputID
label = "State", #label
choices = NULL,
selected = "Alabama",
multiple = FALSE,
options = list(placeholder = 'select a state name'))
),
# ...
))
# server.R
## Libraries ----
library(shiny)
library(plotly)
library(leaflet)
library(maps)
## Non-Reactive ---
# ...
### Plant Location Data
geodata <- read.csv("data/plantgeodata.csv")
# ...
## Reactive ---
shinyServer(function(input, output, session) {
updateSelectizeInput(session,
'stateInput',
choices = statenames, #must be a character vector!!! http://shiny.rstudio.com/articles/selectize.html
selected = "Alabama",
server = TRUE)
output$NGCCSlider <- renderUI({
sliderInput("NGCCCap",
"NGCC Average Capacity Factor (%)",
min = 0,
max = 90,
value = generationDataCleaned[input$stateInput, "NGCCcapFactor"] * 100,
step = 1,
width = '1000px')
})
# ...
output$ratePlotly <- renderPlotly({
r <- plot_ly(result(),
type = "bar", # all "bar" attributes: https://plot.ly/r/reference/#bar
orientation = "h",
x = Rate, # more about bar's "x": /r/reference/#bar-x
y = Name, # more about bar's "y": /r/reference/#bar-y
name = "lbsCO2/MWh",
opacity = 0.7, # more about bar's "name": /r/reference/#bar-name
marker = list( # marker is a named list, valid keys: /r/reference/#bar-marker
color=c("517C96","FF8200") # more about marker's "color" attribute: /r/reference/#bar-marker-color
))
r <- add_trace(x = c(result()[1,5],result()[1,5]), type = "line",
marker = list(
color="red",
size = 0
),
line = list( # marker is a named list, valid keys: /r/reference/#bar-marker
color="red",
width = 6
),
name = "Goal"
)
r <- layout(r, # all of layout's properties: /r/reference/#layout
xaxis = list( # layout's xaxis is a named list. List of valid keys: /r/reference/#layout-xaxis
title = "" # xaxis's title: /r/reference/#layout-xaxis-title
),
yaxis = list( # layout's yaxis is a named list. List of valid keys: /r/reference/#layout-yaxis
title = "lbsCO2/MWh" # yaxis's title: /r/reference/#layout-yaxis-title
),
margin = list(
l = 120
)
)
})
}) /end reactive
States' Views on Clean Power Plan
Alex Pawlowski
Justin Knowles
Michelle Halsted
Jessica Velez
Sarah Eichler-Wood
Emilio Ramirez
library(shiny)
source("linked_scatter.R")
ui <- fixedPage(
h2("Module example"),
linkedScatterUI("scatters"),
textOutput("summary")
)
server <- function(input, output, session) {
df <- callModule(linkedScatter, "scatters", reactive(mpg),
left = reactive(c("cty", "hwy")),
right = reactive(c("drv", "hwy"))
)
output$summary <- renderText({
sprintf("%d observation(s) selected", nrow(dplyr::filter(df(), selected_)))
})
}
shinyApp(ui, server)
example from RStudio
library(shiny)
library(ggplot2)
linkedScatterUI <- function(id) {
ns <- NS(id)
fluidRow(
column(6, plotOutput(ns("plot1"), brush = ns("brush"))),
column(6, plotOutput(ns("plot2"), brush = ns("brush")))
)
}
linkedScatter <- function(input, output, session, data, left, right) {
# Yields the data frame with an additional column "selected_"
# that indicates whether that observation is brushed
dataWithSelection <- reactive({
brushedPoints(data(), input$brush, allRows = TRUE)
})
output$plot1 <- renderPlot({
scatterPlot(dataWithSelection(), left())
})
output$plot2 <- renderPlot({
scatterPlot(dataWithSelection(), right())
})
return(dataWithSelection)
}
scatterPlot <- function(data, cols) {
ggplot(data, aes_string(x = cols[1], y = cols[2])) +
geom_point(aes(color = selected_)) +
scale_color_manual(values = c("black", "#66D65C"), guide = FALSE)
}
on
28 April, 2nd Quarterly Meetup!
mycpp.gitbooks.io/mycpp/content/
bccpp.shinyapps.io/mycpp/
mycpp