-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathnested_modules.R
More file actions
121 lines (105 loc) · 2.04 KB
/
nested_modules.R
File metadata and controls
121 lines (105 loc) · 2.04 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# This app showcases how modules can be nested
library(shiny)
library(ggplot2)
library(DT)
selector_UI <- function(id, dataset) {
ns <- NS(id)
tagList(
selectInput(
inputId = ns("selected_col"),
label = "Selected column",
choices = colnames(dataset)
),
textOutput(
outputId = ns("range")
)
)
}
selector_server <- function(id, dataset) {
moduleServer(
id,
function(input, output, session) {
col_range <- reactive({
range(dataset[, input$selected_col])
})
output$range <- renderText({
paste0("The range is ", col_range()[1], " - ", col_range()[2])
})
return(reactive({input$selected_col}))
}
)
}
table_ui <- function(id, dataset) {
ns <- NS(id)
tagList(
selector_UI(
id = ns("my_col"),
dataset = dataset
),
DTOutput(
outputId = ns("table")
)
)
}
table_server <- function(id, dataset) {
moduleServer(
id,
function(input, output, session) {
selected_col <- selector_server(
id = "my_col",
dataset = dataset
)
output$table <- renderDT({
dataset[, selected_col(), drop = FALSE]
})
}
)
}
graph_ui <- function(id, dataset) {
ns <- NS(id)
tagList(
selector_UI(
id = ns("my_col"),
dataset = dataset
),
plotOutput(
outputId = ns("plot")
)
)
}
graph_server <- function(id, dataset) {
moduleServer(
id,
function(input, output, session) {
selected_col <- selector_server(
id = "my_col",
dataset = dataset
)
output$plot <- renderPlot({
ggplot(dataset, aes(x = .data[[selected_col()]])) +
geom_boxplot()
})
}
)
}
ui <- fluidPage(
table_ui(
id = "table_1",
dataset = mtcars
),
graph_ui(
id = "plot_1",
dataset = mtcars
)
)
server <- function(input, output, session) {
table_server(
id = "table_1",
dataset = mtcars
)
graph_server(
id = "plot_1",
dataset = mtcars
)
}
shinyApp(ui, server)