flexdashboard: shared processing among reactive output chunks

525 views
Skip to first unread message

John Williams

unread,
Jun 20, 2016, 6:18:35 PM6/20/16
to Shiny - Web Framework for R
I have common, time-consumming code (with reactive input)  that needs to run (within reactive output objects)  in multiple chunks in a flexdashboard 

How can I:
  • process the code once, sharing results among multiple reactive chunks?,  and/or
  • express the code as a function, to be called from each reactive object?,  and/or
  • other way(s)?
Thanks!

John Williams

unread,
Jun 20, 2016, 8:35:42 PM6/20/16
to Shiny - Web Framework for R

Maybe with a reactive observer?  I.e., in a flex dashboard, create a reactive obsserver in one code chunck, and access that reactive observer in subsequent code chunk, from within a reactive output function?

Joe Cheng

unread,
Jun 20, 2016, 8:46:56 PM6/20/16
to John Williams, Shiny - Web Framework for R
Close--not a reactive observer (observe() and observeEvent()), but a reactive expression (reactive()).
--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/0bd81e11-b197-4647-a5e7-3acb432ad023%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Message has been deleted
Message has been deleted

John Williams

unread,
Jun 21, 2016, 10:32:06 AM6/21/16
to Shiny - Web Framework for R
got it -  had to wrap code in a function before wrapping in reactive() - thank you,  Joe Chang & Rstudio,  for Shiny - its awesome!!!


On Monday, June 20, 2016 at 10:39:44 PM UTC-4, John Williams wrote:
CORRECTION:

Keep getting:  "Error: data must be a data frame class object"  ... caused by the renderSimpleNetwork() in 2nd chunk ...

```{r  flexdashboardChunk1}

reactiveCode <- quote(
{
 code producing a dataframe (using reactive input)  
 }
)    
```

```{r flexdashboardChunk2}

values <- reactiveValues()
values.topdocs <- reactiveCode

renderSimpleNetwork({
  simpleNetwork(values.topdocs, Source = "src", Target = "tar", linkDistance = 150, fontSize = 12)
  })

````

On Monday, June 20, 2016 at 10:36:34 PM UTC-4, John Williams wrote:
Keep getting:  "Error: data must be a data frame class object"  ... caused by the 

```{r  flexdashboardChunk1}

reactiveCode <- quote(
{
 code producing a dataframe (using reactive input)  
 }
)    
```

```{r flexdashboardChunk2}

values <- reactiveValues()
values.topdocs <- topDocsandProcs

renderSimpleNetwork({
  simpleNetwork(values.topdocs, Source = "src", Target = "tar", linkDistance = 150, fontSize = 12)
  })

````










On Monday, June 20, 2016 at 6:18:35 PM UTC-4, John Williams wrote:

John Williams

unread,
Jun 21, 2016, 11:54:21 AM6/21/16
to Shiny - Web Framework for R

well, not so fast ... it seems that my reactive function - reactiveTDPS() - is fully executing each time it is called, not just when inputs change --- anybody know how to correct this? 

also,  how/where  to implement progress indicator after initial loading?


Thanks!

Joe Cheng [RStudio]

unread,
Jun 21, 2016, 3:12:20 PM6/21/16
to Shiny - Web Framework for R
I'm not sure where the confusion is without a more complete code sample, but, there's not much different here vs. any other server function (just imagine the entire R Markdown document is a single Shiny server function).

reactiveTDPS <- reactive({
  ... yield a data frame...
})

renderSimpleNetwork({
  simpleNetwork(reactiveTDPS(), ...)
})

It doesn't matter if these two pieces of code are in the same or different code chunks.
Reply all
Reply to author
Forward
0 new messages