Nested observeEvent() in observer() gets executed

2019-09-19 02:53发布

问题:

My code looks like

observe ({
    #subset someDataframe which I need in the observeEvent() 

    observeEvent(input$Numbers{
        #if not NULL, do something with subsetted dataframe
    })
})

I have an observe() function which gets input from the ui.R. Depending on the input, I subset a dataframe which will be displayed in the app. There is the need for further subsetting, but this field can be empty. To avoid the Unhandled Error if the selection is empty (thus, NULL) is use the observeEvent() function. If the field stays empty, there is no error raised.

There is some weird behaviour though. I print out what is selected (in my R console) observed by the observeEvent(). When I select something observed by the observeEvent() function, the selected stuff gets print out once. When I change a selection which is observed by the simple observe() and then change something in the selection observed by observeEvent() it gets printed out twice. When I change a selection observed by the observe() function and then the selection from the observeEvent() it gets printed out three times - and so forth.

So, depending on how often I change a selection observed by the observed() function I change, the observeEvent() gets executed that often.

Why is that?!

回答1:

As per my comment:

subset <- reactive({
  # Do your sub-setting here 

  if(!is.null(input$Numbers)){
    #Do something else
  } 
  # return (your_subset)
})