-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06_shiny.R
86 lines (72 loc) · 2.33 KB
/
06_shiny.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# https://shiny.posit.co/
#
library(shiny)
library(magrittr)
options("cb_render_all" = TRUE)
options("cb_active_filter" = FALSE)
ui <- fluidPage(
shiny::tags$style("code.hl.background {color: #000 !important};"),
# Application title
titlePanel("shinyCohortBuilder Demo"),
# Sidebar
sidebarLayout(
sidebarPanel(
shinyCohortBuilder::cb_ui("librarian")
),
# Show a librarian
mainPanel(
actionButton("update_filter", "Update filter"),
verbatimTextOutput("cohort_data")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
# Create binding keys
librarian_source <- librarian[c("books", "issues")] %>%
cohortBuilder::as.tblist() %>%
cohortBuilder::set_source(
binding_keys = cohortBuilder::bind_keys(
cohortBuilder::bind_key(
update = cohortBuilder::data_key('books', 'isbn'),
cohortBuilder::data_key('issues', 'isbn'),
activate = TRUE
),
cohortBuilder::bind_key(
update = cohortBuilder::data_key('issues', 'isbn'),
cohortBuilder::data_key('books', 'isbn'),
activate = TRUE
)
)
) %>%
shinyCohortBuilder::autofilter(gui_args = list(search = FALSE))
coh <- librarian_source %>%
cohortBuilder::cohort()
shinyCohortBuilder::cb_server("librarian", coh, feedback = TRUE)
returned_data <- shiny::eventReactive(input[["librarian-cb_data_updated"]], {
coh$get_data(step_id = coh$last_step_id(), state = "post")
}, ignoreInit = FALSE, ignoreNULL = FALSE)
output$cohort_data <- renderPrint({
returned_data()
})
observeEvent(input$update_filter, {
id <- coh$get_filter("1") %>% purrr::keep(~ .x$name == "copies") %>% names()
cohortBuilder::update_filter(
coh, step_id = 1, filter_id = id,
label = "Copies",
range = c(1, 2), active = TRUE
)
cohortBuilder::run(coh)
ns <- coh$attributes$session$ns
shiny::removeUI(glue::glue("#{ns(1)}"), session = coh$attributes$session, immediate = TRUE)
shinyCohortBuilder:::render_steps(coh, coh$attributes$session, init = FALSE)
})
}
# Run the application
shinyApp(ui = ui, server = server)