Recipe specification with shiny selectInput

Dear Community,
I am trying to create a shiny app that can talk directly (backend) with tidymodels framework.

I have a problem related with recipe function.

After downloading/uploading the data and visualize and analyze the data. I would like to give the user the freedom to specify what the target variable is in order to start the modeling process.

The function in this case is inside an observeEvent with the following specifications.

recipe <- recipe(input$select_input_target_variable ~ ., data = dataset) %>%
                 step_timeseries_signature(date) %>% 
                 step_normalize(all_predictors()) %>% 
                 step_corr(all_predictors(), threshold = 0.8)

And I get the following error message

"Warning: Error in : No in-line functions should be used here; use steps to define baking actions."

I would like to ask you how I could solve this problem, or a more elegant way in doing this

Thanks in advance

You can use as.formula() to construct a formula from text.

I have tried several solutions and ways to apply your suggestion but have not been able to overcome the problem.

Could you please write me below the code that can make the process you suggested work for me.

Thank you

library(shiny)
library(tidymodels)

tidymodels_prefer()
dataset<-iris

ui <- fluidPage(

  shiny::varSelectInput(inputId = "select_input_target_variable",
                        label = "Target Variable",
                        data = dataset,
                        selected = "Species",
                        multiple = FALSE),
  verbatimTextOutput("showout")
)



server <- function(input, output, session) {
  
  output$showout <- renderPrint({
    rr <- req(reactive_recipe())
    cat("reactive recipe gives\n")
    str(rr)
  })
  
reactive_recipe <- reactive({
  recipe(as.formula(paste0(
    input$select_input_target_variable,
    " ~ ."
  )),
  data = dataset
  ) %>%
    # step_timeseries_signature(date) %>%
    step_normalize(all_predictors()) %>%
    step_corr(all_predictors(), threshold = 0.8)
})
}

shinyApp(ui, server)
1 Like

Thanks a lot @nirgrahamuk

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.