-
Notifications
You must be signed in to change notification settings - Fork 8
/
nested_renderUI_observe.R
64 lines (57 loc) · 1.53 KB
/
nested_renderUI_observe.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
library(shiny)
## Nested modules with observe().
## Both modules use observe() to reset slider value after checkboxInput().
## But here no need to pass argument about reset.
sliderTextUI <- function(id) {
ns <- NS(id)
tagList(
uiOutput(ns("set_slider")),
checkboxInput(ns("check"), "Check reset to rest slider", FALSE),
textOutput(ns("number"))
)
}
sliderText <- function(input, output, session, show) {
ns <- session$ns
output$set_slider <- renderUI({
sliderInput(ns("slider"), "Slide me", 0, 100, 5)
})
output$number <- renderText({
if(show())
input$slider
else
NULL
})
observe({
check <- req(input$check)
if(check) {
updateSliderInput(session, "slider", value=0)
updateCheckboxInput(session, "check", value=FALSE)
}
})
reactive({input$slider + 5})
}
sliderNestUI <- function(id) {
ns <- NS(id)
tagList(
sliderTextUI(ns("inner1"))
)
}
sliderNest <- function(input, output, session, show) {
out <- callModule(sliderText, "inner1", show)
reactive({out() + 10})
}
ui <- fluidPage(
checkboxInput("display", "Show Value"),
sliderTextUI("module"),
h2(textOutput("value")),
sliderNestUI("nester"),
h2(textOutput("value2"))
)
server <- function(input, output) {
display <- reactive({input$display})
num2 <- callModule(sliderNest, "nester", display)
num <- callModule(sliderText, "module", display)
output$value <- renderText({paste0("slider1+5: ", num())})
output$value2 <- renderText({paste0("slider2+5+10: ", num2())})
}
shinyApp(ui, server)