four-eyes
four-eyes

Reputation: 12439

How to assign reactiveValues() Shiny

I want to use reactiveValues() in my shiny app but I am not sure how to do that.

I have an actionButton and depending on the state of that actionButton I want to assign a data.frame to some reactiveValues which then will be processed by some observers.

The way I wrote it is like this

server.R

myDataFrame <- reactiveValues()

observeEvent(input$myButton, {
    myDataFrame$forProcessing <- some.data.frame
})

observe({
    #do something with myDataFrame$forProcecessing
})

However, that crashes. I guess its because myDataFrame gets created sort of "empty" which then causes my observe() #do something... to crash?!

How would I create the reactiveValues properly so that it triggers the observer() only when its filled?

Upvotes: 1

Views: 1077

Answers (1)

Paul Hiemstra
Paul Hiemstra

Reputation: 60984

You have to create the reactive values with the content already there. The solution is to put an empty data frame in the reactive values, and in observe ensure that nothing happens when the data.frame is empty. So:

myDataFrame <- reactiveValues(forProcessing = data.frame())

and:

observe({
    if (nrow(myDataFrame$forProcessing) != 0) {
        #do something with myDataFrame$forProcecessing 
    }
})

Upvotes: 2

Related Questions