From e75a31b3f7d1b809a0b5e857532bb0abd59a13ef Mon Sep 17 00:00:00 2001 From: Winston Chang Date: Tue, 12 Mar 2013 12:40:56 -0500 Subject: [PATCH] Fix examples with currentFib --- _includes/tutorial/execution-scheduling.md | 4 ++-- _includes/tutorial/reactivity-overview.md | 23 +++++++++++++++++----- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/_includes/tutorial/execution-scheduling.md b/_includes/tutorial/execution-scheduling.md index 67d567713..7cc4ca739 100644 --- a/_includes/tutorial/execution-scheduling.md +++ b/_includes/tutorial/execution-scheduling.md @@ -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 %} diff --git a/_includes/tutorial/reactivity-overview.md b/_includes/tutorial/reactivity-overview.md index 69441e230..b181993bf 100644 --- a/_includes/tutorial/reactivity-overview.md +++ b/_includes/tutorial/reactivity-overview.md @@ -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 — 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