-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.R
More file actions
99 lines (90 loc) · 3.52 KB
/
Copy pathapp.R
File metadata and controls
99 lines (90 loc) · 3.52 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
library(shiny)
# The question that led to this app was about when to use () when accessing
# reactive() objects vs reactiveValues(). The app has four input text boxes,
# and four boxes that shows a combined string of the three inputs. The output combos:
# 1. r is just a tibble stored as a reactive object
# 2. rVal is based on the output of a tibble stored in a reactiveValues object.
# 3. rValr is r being stored inside of rVal
# To use reactlog, press ctrl/cmd F3 to step through the log
reactlog::reactlog_enable()
# Define UI
ui <- fluidPage(
titlePanel("Using reactive vs reactiveValues"),
tags$body(
p("The goal of this app is to show how using", code("reactive"), "and", code("reactiveValues"), "can differ.
The app puts the inputs into a tibble and prints out the results using the following methods:")),
tags$ol(
tags$li(code("reactive()")),
tags$li(code("reactiveValues()")),
tags$li(code("reactiveValues(df = reactive())"))
),
p("We can see the text entered for each val are reactive as they update each as changes are made. The print code button shows what happens when we try and print the values of the objects
The code for this app can be found", a(href="https://github.com/jonbry/reactive_playground/blob/main/app.R", "here")),
# Text Inputs
textInput("val1", "Val 1"),
textInput("val2", "Val 2"),
textInput("val3", "Val 3"),
# Print the actual objects
actionButton("print", "Print Objects"),
# Clear text inputs
actionButton("clear_all", "Clear All"),
# Actual reactivey objects
htmlOutput("printr"),
verbatimTextOutput("r"),
htmlOutput("printrVal"),
verbatimTextOutput("rVal"),
htmlOutput("printrValr"),
verbatimTextOutput("rValr")
)
# Define server logic
server <- function(input, output) {
# Save tibble of inputs into a reactive object
vals <- reactive(tibble::tibble(
val1 = input$val1,
val2 = input$val2,
val3 = input$val3
))
# Output the concatenated text
output$r <- renderText(paste(vals()$val1, " ", vals()$val2, " ", vals()$val3))
# Save tibble of inputs into reactiveValues object
rv <- reactiveValues()
# Watch for inputs into a tibble
observe({
rv$df <- tibble::tibble(
val1 = input$val1,
val2 = input$val2,
val3 = input$val3
)
})
# Output the reactive value object
output$rVal <- renderText(paste(rv$df$val1, " ", rv$df$val2, " ", rv$df$val3))
# reactiveValues object to save the the reactive object r into
rvr <- reactiveValues()
# Save reactive object r into reactiveValues object rvr
rvr$r <- reactive(
tibble::tibble(
val1 = input$val1,
val2 = input$val2,
val3 = input$val3
)
)
# Print the values within rvr
output$rValr <- renderText(paste(rvr$r()$val1, " ", rvr$r()$val2, " ", rvr$r()$val3))
# Clear inputs
observeEvent(input$clear_all, {
updateTextInput(inputId = "val1", value = "")
updateTextInput(inputId ="val2", value = "")
updateTextInput(inputId = "val3", value = "")
})
# Print out values
observeEvent(input$print, {
output$printr <- renderText("<b>reactive Object (vals())</b>")
output$printrVal <- renderText("<b>reactiveValues object (rv)</b>")
output$printrValr <- renderText("<b>reactive object in a reactiveValues object (rvr$r)</b>")
output$r <- renderPrint(vals())
output$rval <- renderPrint(rv)
output$rValr <- renderPrint(rvr$r)
})
}
# Run the application
shinyApp(ui = ui, server = server)