Use output of an operation in my shiny UI

48 Views Asked by At

I have an observeEvent where i do some preprocessing to my reactive data:

observeEvent(input$preprocess_btn,{
    req(input$imputation_numeric, input$imputation_categorical)
    
      object_recipe <- recipe(as.formula(paste(input$variables, "~ .")), train_data())
      
      **
      preprocessing steps here
      **

      trained_recipe <- prep(object_recipe, training = train_data())
      
      print(trained_recipe) #This shows the desired output, but on console
      
      train_data_prep(bake(trained_recipe, new_data = train_data()))
      test_data_prep(bake(trained_recipe, new_data = test_data()))
      
  })

The desired output as displayed on the code fragment would be:

── Recipe ────────────

── Inputs Number of variables by role outcome: 1 predictor: 7

── Training information Training data contained 713 data points and 148 incomplete rows.

── Operations • Bagged tree imputation for: Age, SibSp, Parch, Fare | Trained • Mode imputation for: Pclass, Sex, Embarked | Trained • Sparse, unbalanced variable filter removed: | Trained • Centering for: Age, SibSp, Parch, Fare | Trained • Scaling for: Age, SibSp, Parch, Fare | Trained • Dummy variables from: Pclass, Sex, Embarked | Trained

Thing is I don't know how to pass it to a renderPrint because the variables can only be called inside the observeEvent, and tried storing It inside a reactiveVal but couldn't find success

1

There are 1 best solutions below

9
guasi On

Store the output of the observeEvent() in a reactiveValues() then print that. You would need a UI element too of course.

library(shiny)
library(dplyr)
library(recipes)

shinyApp(
  ui = fluidPage(
    actionButton("preprocess_btn", "Start Process"),
    verbatimTextOutput("textout")
  ),

  server = function(input, output) {
    rvals <- reactiveValues()

    observeEvent(input$preprocess_btn, {
      rec <- recipe(mtcars) %>%
        step_center(everything()) %>%
        step_scale(everything()) %>%
        check_missing(everything())

      rvals$trained_recipe <- tidy(rec)
    })

    output$textout <- renderPrint({
      rvals$trained_recipe
    })
  }
)