mirror of
https://github.com/rstudio/shiny.git
synced 2026-02-07 21:26:08 -05:00
Fix examples with currentFib
This commit is contained in:
@@ -64,8 +64,8 @@ fib <- function(n) ifelse(n<3, 1, fib(n-1)+fib(n-2))
|
||||
shinyServer(function(input, output) {
|
||||
currentFib <- reactive({ fib(as.numeric(input$n)) })
|
||||
|
||||
output$nthValue <- renderText({ currentFib(as.numeric(input$n)) })
|
||||
output$nthValueInv <- renderText({ 1 / currentFib(as.numeric(input$n)) })
|
||||
output$nthValue <- renderText({ currentFib() })
|
||||
output$nthValueInv <- renderText({ 1 / currentFib() })
|
||||
})
|
||||
{% endhighlight %}
|
||||
|
||||
|
||||
@@ -92,8 +92,8 @@ fib <- function(n) ifelse(n<3, 1, fib(n-1)+fib(n-2))
|
||||
shinyServer(function(input, output) {
|
||||
currentFib <- reactive({ fib(as.numeric(input$n)) })
|
||||
|
||||
output$nthValue <- renderText({ currentFib(as.numeric(input$n)) })
|
||||
output$nthValueInv <- renderText({ 1 / currentFib(as.numeric(input$n)) })
|
||||
output$nthValue <- renderText({ currentFib() })
|
||||
output$nthValueInv <- renderText({ 1 / currentFib() })
|
||||
})
|
||||
{% endhighlight %}
|
||||
|
||||
@@ -103,15 +103,28 @@ Here is the new graph structure:
|
||||

|
||||
|
||||
|
||||
Keep in mind that if your application tries to access reactive values or expressions from outside a reactive context — that is, outside of a reactive expression or observer — then it will result in an error. You can think of there being a reactive "world" which can see and change the non-reactive world, but the non-reactive world can't do the same to the reactive world. Code like this will not work, because the call to `fib()` is not in the reactive world, but it tries to access some that is, the reactive value `input$n`:
|
||||
Keep in mind that if your application tries to access reactive values or expressions from outside a reactive context — that is, outside of a reactive expression or observer — then it will result in an error. You can think of there being a reactive "world" which can see and change the non-reactive world, but the non-reactive world can't do the same to the reactive world. Code like this will not work, because the call to `fib()` is not in the reactive world (it's not in a `reactive()` or `renderXX()` call) but it tries to access something that is, the reactive value `input$n`:
|
||||
|
||||
{% highlight r %}
|
||||
shinyServer(function(input, output) {
|
||||
currentFib <- fib(as.numeric(input$n))
|
||||
output$nthValue <- renderText({ currentFib })
|
||||
# Will give error
|
||||
currentFib <- fib(as.numeric(input$n))
|
||||
output$nthValue <- renderText({ currentFib })
|
||||
})
|
||||
{% endhighlight %}
|
||||
|
||||
On the other hand, if `currentFib` is a function that accesses a reactive value, and that function is called within the reactive world, then it will work:
|
||||
|
||||
{% highlight r %}
|
||||
shinyServer(function(input, output) {
|
||||
# OK, as long as this is called from the reactive world:
|
||||
currentFib <- function() {
|
||||
fib(as.numeric(input$n))
|
||||
}
|
||||
|
||||
output$nthValue <- renderText({ currentFib })
|
||||
})
|
||||
{% endhighlight %}
|
||||
|
||||
### Summary
|
||||
|
||||
|
||||
Reference in New Issue
Block a user