Skip to content

Commit beb7ee9

Browse files
committed
Materials for Shiny Gadgets webinar
1 parent 88dc7df commit beb7ee9

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed

29-Shiny-Gadgets/brush-gadget.R

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
library(shiny)
2+
library(miniUI)
3+
library(ggplot2)
4+
5+
pick_points <- function(data, x, y) {
6+
ui <- miniPage(
7+
gadgetTitleBar(paste("Select points")),
8+
miniContentPanel(padding = 0,
9+
plotOutput("plot1", height = "100%", brush = "brush")
10+
),
11+
miniButtonBlock(
12+
actionButton("add", "", icon = icon("thumbs-up")),
13+
actionButton("sub", "", icon = icon("thumbs-down")),
14+
actionButton("none", "" , icon = icon("ban")),
15+
actionButton("all", "", icon = icon("refresh"))
16+
)
17+
)
18+
19+
server <- function(input, output) {
20+
# For storing selected points
21+
vals <- reactiveValues(keep = rep(TRUE, nrow(data)))
22+
23+
output$plot1 <- renderPlot({
24+
# Plot the kept and excluded points as two separate data sets
25+
keep <- data[ vals$keep, , drop = FALSE]
26+
exclude <- data[!vals$keep, , drop = FALSE]
27+
28+
ggplot(keep, aes_(x, y)) +
29+
geom_point(data = exclude, color = "grey80") +
30+
geom_point()
31+
})
32+
33+
# Update selected points
34+
selected <- reactive({
35+
brushedPoints(data, input$brush, allRows = TRUE)$selected_
36+
})
37+
observeEvent(input$add, vals$keep <- vals$keep | selected())
38+
observeEvent(input$sub, vals$keep <- vals$keep & !selected())
39+
observeEvent(input$all, vals$keep <- rep(TRUE, nrow(data)))
40+
observeEvent(input$none, vals$keep <- rep(FALSE, nrow(data)))
41+
42+
observeEvent(input$done, {
43+
stopApp(vals$keep)
44+
})
45+
observeEvent(input$cancel, {
46+
stopApp(NULL)
47+
})
48+
49+
}
50+
51+
runGadget(ui, server)
52+
}
53+
# pick_points(mtcars, ~wt, ~mpg)
54+
# pick_points(ggplot2::mpg, aes(displ, hwy))

29-Shiny-Gadgets/get_password.R

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
library(miniUI)
2+
library(shiny)
3+
4+
get_password <- function() {
5+
ui <- miniPage(
6+
gadgetTitleBar("Please enter your password"),
7+
miniContentPanel(
8+
passwordInput("password", "")
9+
)
10+
)
11+
12+
server <- function(input, output) {
13+
observeEvent(input$done, {
14+
stopApp(input$password)
15+
})
16+
observeEvent(input$cancel, {
17+
stopApp(stop("No password.", call. = FALSE))
18+
})
19+
}
20+
21+
runGadget(ui, server, viewer = dialogViewer("Password", height = 200))
22+
}
3.17 MB
Binary file not shown.

29-Shiny-Gadgets/tabs.R

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
library(shiny)
2+
library(miniUI)
3+
library(leaflet)
4+
library(ggplot2)
5+
6+
tab_app <- function() {
7+
ui <- miniPage(
8+
gadgetTitleBar("Shiny gadget example"),
9+
miniTabstripPanel(
10+
miniTabPanel("Parameters", icon = icon("sliders"),
11+
miniContentPanel(sliderInput("year", "Year", 1978, 2010, 2000))
12+
),
13+
miniTabPanel("Visualize", icon = icon("area-chart"),
14+
miniContentPanel(plotOutput("cars", height = "100%"))
15+
),
16+
miniTabPanel("Map", icon = icon("map-o"),
17+
miniContentPanel(padding = 0, leafletOutput("map", height = "100%")),
18+
miniButtonBlock(actionButton("resetMap", "Reset"))
19+
),
20+
miniTabPanel("Data", icon = icon("table"),
21+
miniContentPanel(DT::dataTableOutput("table"))
22+
)
23+
)
24+
)
25+
26+
server <- function(input, output, session) {
27+
output$cars <- renderPlot({
28+
require(ggplot2)
29+
ggplot(cars, aes(speed, dist)) + geom_point()
30+
})
31+
32+
output$map <- renderLeaflet({
33+
force(input$resetMap)
34+
35+
leaflet(quakes, height = "100%") %>% addTiles() %>%
36+
addMarkers(lng = ~long, lat = ~lat)
37+
})
38+
39+
output$table <- DT::renderDataTable({
40+
diamonds
41+
})
42+
43+
observeEvent(input$done, {
44+
stopApp(TRUE)
45+
})
46+
}
47+
48+
runGadget(shinyApp(ui, server), viewer = paneViewer())
49+
}
50+
51+
# tab_app()

0 commit comments

Comments
 (0)