Skip to content

Commit

Permalink
Update basic-reactivity.Rmd (#500)
Browse files Browse the repository at this point in the history
  • Loading branch information
bm5tev3 authored Oct 18, 2024
1 parent 222013a commit 82803e9
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions basic-reactivity.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ If you attempt to modify an input inside the server function, you'll get an erro

```{r, eval = FALSE}
server <- function(input, output, session) {
input$count <- 10
input$count <- 10
}
shinyApp(ui, server)
Expand Down Expand Up @@ -307,7 +307,7 @@ Hopefully, the shapes will help you remember how the components fit together.

### Execution order

It's important to understand that the order your code runs is solely determined by the reactive graph.
It's important to understand that the order in which your code runs is solely determined by the reactive graph.
This is different from most R code where the execution order is determined by the order of lines.
For example, we could flip the order of the two lines in our simple server function:

Expand Down Expand Up @@ -375,7 +375,7 @@ This concept is very important and different to most other R code, so I'll say i
server3 <- function(input, output, session) {
d <- reactive(c() ^ input$d)
a <- reactive(input$a * 10)
c <- reactive(b() / input$c)
c <- reactive(b() / input$c)
b <- reactive(a() + input$b)
}
```
Expand Down Expand Up @@ -434,7 +434,7 @@ freqpoly <- function(x1, x2, binwidth = 0.1, xlim = c(-3, 3)) {
t_test <- function(x1, x2) {
test <- t.test(x1, x2)
# use sprintf() to format t.test() results compactly
sprintf(
"p value: %0.3f\n[%0.2f, %0.2f]",
Expand Down Expand Up @@ -473,13 +473,13 @@ The second row has a wide column for the plot, and a narrow column for the hypot
```{r}
ui <- fluidPage(
fluidRow(
column(4,
column(4,
"Distribution 1",
numericInput("n1", label = "n", value = 1000, min = 1),
numericInput("mean1", label = "µ", value = 0, step = 0.1),
numericInput("sd1", label = "σ", value = 0.5, min = 0.1, step = 0.1)
),
column(4,
column(4,
"Distribution 2",
numericInput("n2", label = "n", value = 1000, min = 1),
numericInput("mean2", label = "µ", value = 0, step = 0.1),
Expand All @@ -505,14 +505,14 @@ server <- function(input, output, session) {
output$hist <- renderPlot({
x1 <- rnorm(input$n1, input$mean1, input$sd1)
x2 <- rnorm(input$n2, input$mean2, input$sd2)
freqpoly(x1, x2, binwidth = input$binwidth, xlim = input$range)
}, res = 96)
output$ttest <- renderText({
x1 <- rnorm(input$n1, input$mean1, input$sd1)
x2 <- rnorm(input$n2, input$mean2, input$sd2)
t_test(x1, x2)
})
}
Expand Down Expand Up @@ -638,7 +638,7 @@ Even if you didn't get that error, you'd still have a problem: `x1` and `x2` wou
If you try to use a function, the app will work:

```{r}
server <- function(input, output, session) {
server <- function(input, output, session) {
x1 <- function() rnorm(input$n1, input$mean1, input$sd1)
x2 <- function() rnorm(input$n2, input$mean2, input$sd2)
Expand Down Expand Up @@ -672,7 +672,7 @@ This yields a smaller UI object and server function:
```{r}
ui <- fluidPage(
fluidRow(
column(3,
column(3,
numericInput("lambda1", label = "lambda1", value = 3),
numericInput("lambda2", label = "lambda2", value = 5),
numericInput("n", label = "n", value = 1e4, min = 0)
Expand Down Expand Up @@ -718,7 +718,7 @@ This change yields the reactive graph shown in Figure \@ref(fig:sim-timer)
```{r}
server <- function(input, output, session) {
timer <- reactiveTimer(500)
x1 <- reactive({
timer()
rpois(input$n, input$lambda1)
Expand All @@ -727,7 +727,7 @@ server <- function(input, output, session) {
timer()
rpois(input$n, input$lambda2)
})
output$hist <- renderPlot({
freqpoly(x1(), x2(), binwidth = 1, xlim = c(0, 40))
}, res = 96)
Expand Down Expand Up @@ -755,7 +755,7 @@ This is a great use case for an `actionButton()`:
```{r}
ui <- fluidPage(
fluidRow(
column(3,
column(3,
numericInput("lambda1", label = "lambda1", value = 3),
numericInput("lambda2", label = "lambda2", value = 5),
numericInput("n", label = "n", value = 1e4, min = 0),
Expand Down Expand Up @@ -852,7 +852,7 @@ ui <- fluidPage(
server <- function(input, output, session) {
string <- reactive(paste0("Hello ", input$name, "!"))
output$greeting <- renderText(string())
observeEvent(input$name, {
message("Greeting performed")
Expand Down

0 comments on commit 82803e9

Please sign in to comment.