Can't assign value to a reactiveVal because It is NULL

42 Views Asked by At

I plan to make an app where I preprocess data, depending on what preprocessing method user wants to apply to the recipe. Each preprocessing method is on a different observeEvent() that takes action when their own button is pressed. That's why i want a reactiveVal where the recipe is stored so I can apply only the ones I want (different imputations, near zero variance, scale, etc)

Because of that, have a reactiveVal initialized:

object_recipe <- reactiveVal()

And the next block of code:

 observeEvent(input$recipe{
    req(train_data())
    
    object_recipe() <- recipe(formula = Survived ~ ., data = train_data())
  })




observeEvent(input$imputation_btn,{
    req(input$imputation_method, input$imputation_vars, object_recipe())
    
      if(input$imputation_method == "knn")  
        object_recipe() <- step_impute_knn(
          recipe = object_recipe(),
          predictor = all_of(input$imputation_vars),
          neighbors = input$knn_value,
        )
      
      if(input$imputation_method == "bagged_trees")  
        object_recipe() <- step_impute_bag(
          recipe = object_recipe(),
          predictor = all_of(input$imputation_vars)
          )
      
      if(input$imputation_method == "mean")  
        object_recipe() <- step_impute_mean(
          recipe = object_recipe(),
          predictor = all_of(input$imputation_vars)
        )
      
      if(input$imputation_method == "mode")  
        object_recipe() <- step_impute_mode(
          recipe = object_recipe(),
          predictor = all_of(input$imputation_vars)
        )   })

When It runs, an error occurs:

Error in <-: lado izquierdo de la asignación inválida (NULL) ( invalid (NULL) left side of assignment)

If I try to use a local variable to the second observeEvent It works, but then I cannot use that recipe In another observeEvent that wants to do another preprocess method on top of It.

1

There are 1 best solutions below

2
Billy34 On

reactiveVal do not function the same way as reactiveValues.

# declare
test <- reactiveVal(initialValue)

# getValue
value <- test()

# setValue
test(value)

If you don't want to get lost with different semantics, you can use a single value reactiveValues that will behave more or less the same way as input and ouput.

# declare
test <- reactiveValues(value=initialValue)

# get value
value <- test$value

# set value
test$value <- value