Fix examples with currentFib

This commit is contained in:
Winston Chang
2013-03-12 12:40:56 -05:00
parent 317b484f58
commit e75a31b3f7
2 changed files with 20 additions and 7 deletions

View File

@@ -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 %}

View File

@@ -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:
![Fibonacci app with conductor](reactivity_diagrams/conductor.png)
Keep in mind that if your application tries to access reactive values or expressions from outside a reactive context &mdash; that is, outside of a reactive expression or observer &mdash; 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 &mdash; that is, outside of a reactive expression or observer &mdash; 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