我计划制作一个应用程序来预处理数据,具体取决于用户想要应用于菜谱的预处理方法。 每个预处理方法都位于不同的observeEvent() 上,当按下自己的按钮时,该方法会执行操作。 这就是为什么我想要一个reactiveVal来存储配方,这样我就可以只应用我想要的(不同的插补,接近零方差,比例等)
因此,初始化一个reactiveVal:
object_recipe <- reactiveVal()
下一个代码块:
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)
) })
运行时出现错误:
错误于 <-: lado izquierdo de la asignación inválida (NULL) ( invalid (NULL) left side of assignment)
如果我尝试对第二个observeEvent使用局部变量,它会起作用,但是我无法在另一个想要在其之上执行另一个预处理方法的observeEvent中使用该配方。
ReactiveVal 的功能与reactiveValues 不同。
# declare
test <- reactiveVal(initialValue)
# getValue
value <- test()
# setValue
test(value)
如果你不想迷路,你可以使用单个值reactiveValues
# declare
test <- reactiveValues(value=initialValue)
# get value
value <- test$value
# set value
test$value <- value