Recipes not compatible with reactive Shiny?

Hey there, I've been struggling to use tidymodels on a Shiny dashboard, and I've narrowed it down to recipes being the crux of the error. Here is the reprex (Shiny in showcase mode, so you can see all the code, and I tried to make it minimal as possible): https://pathos.shinyapps.io/recipes_not_working

If in need of sample data, feel free to use this code to generate data:

# Generate some data that increases over time
dff_generated <- data.frame(yearr = sample(2015:2021, 2190, replace = TRUE),
                 monthh = sample(1:12, 2190, replace = TRUE),
                 dayy = sample(1:29, 2190, replace = TRUE)) |>
  mutate(datee = ymd(paste(yearr, monthh, dayy)),
         weekk = week(datee),
         yy  = sample(10000:20000, 2190, replace = TRUE) + (yearr^2) + (monthh^2) + (weekk^2),
         dummyy = as.factor(round(sample(0:1, 2190, replace = TRUE)))) |>
  filter(!is.na(datee)) |>
  arrange(-desc(datee))

write_csv(dff_generated, 'sample_data.csv')

Steps:

  1. Upload data
  2. Select yy as predicted variable, and datee as time variable (though this part really doesn't matter)
  3. See the second table not being rendered

Prerequisites:

  • It should be reactive to user uploading the data
  • It should be reactive to user selecting variables/columns from the uploaded data

Problem

Just by adding recipes into an otherwise identical code, the code does not work. This is the 'identical code' setup:

You can see that all it needs to do is return dataa in both cases.

Result

Expected result

Expected result is to show the identical table twice.


It's entirely possible that I'm doing something wrong, but if I am (variable referencing? Maybe I'm supposed to be using {{ }}? I tried a few different things, but no luck), please let me know.

What I searched so far online on this error message didn't help me, and there seems to be very few discussions of it online, which understandable because recipes is relatively recent.

Hello, this is not a shiny issue. but a question of how to construct formulas from character strings.

  recipe(dataa , as.formula(input$var_yy ~ .)) 

vs

  recipe(dataa, as.formula(paste0(input$var_yy, " ~ .")))
1 Like

Ah right

Been chasing errors for so long, I deviated from the original code which is same as yours. Thanks

Quick question about recipes in Shiny: would I have to edit something similar in the following recipe in rec_generic using paste0? I'm getting the error object of type 'closure' is not subsettable

dataa = df()
var_time_sym = rlang::sym(input$var_time)
recip = reactive(as.formula(paste0(input$var_predicted, ' ~ ', input$var_time)))

rec_generic = dataa |>
            recipe(recip) |>
            update_role(var_time_sym, new_role = 'ID') |>
            step_dummy(all_nominal()) |> # Set dummy vars
            step_normalize(all_numeric(), -all_outcomes()) |>
            step_zv(all_predictors())

If you are doing that within a renderTable there is no apparent purpose I see in your making recip a reactive.
But if you are set on making it a reactive , then when you refer to it to use it , you must treat it like a function. I.e. recip()

1 Like

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.