Is there a tutorial for Shiny, with reactive dataframe -> tidymodels -> ggplot?

I've been learning a lot through troubleshooting with tidymodels through a shiny app.

Essentially, I have a reactive dataframe, df() per usual in tutorials and guides. The dataframe is reactive because the user uploads the data, using fileInput() in ui.R.

This makes rendering ggplots a bit trickier after processing the data with recipes. I'm currently stuck at setting up the recipes for tidymodels to work with. Recipes doesn't seem to accept df().

I did find, so far is that I need to set formula explicitly using as.formula() so it would be recipe(as.formula(paste(input$independent_variable ~', .)) instead of the usual recipe(y ~ .)

So, I'm wondering if there is such a tutorial.

Thanks

Can you please provide a minimal reprex (reproducible example)? The goal of a reprex is to make it as easy as possible for me to recreate your problem so that I can fix it: please help me help you!

If you've never heard of a reprex before, start by reading "What is a reprex", and follow the advice further down that page.

Thanks for your reply.

Not exactly minimal reprex I think, but I took a bit of time to reduce it down from the original and the dashboard is in showcase mode, so all of the code is presented there. I also think that the included parts are relevant. Here is the link for it: enet_dashboard

Although the error is more generic on the website, the error I get when running from R Studio is Error: cannot coerce type 'closure' to vector of type 'character'

The part I'm having trouble with is the Plotly output gg_enet in the 'Model' tab, more specifically the #MODEL part and below in server.R.

To clarify, I've been trying to narrow it down, and worked out that at least until this part, it's ok:

folds = dff() |>
      sliding_period(lookback = input$lookback, # if Inf, then it's chain
                     period = input$period,
                     index = {{ var_time_sym }},
                     #assess_stop = 1L, # include how many 'periods' in the future
                     every = input$every, # group how many 'periods' together
                     step = input$step, # how many 'periods' * 'every' to move the window (?)
                     skip = input$skip)

(That index = {{ var_time_sym }} part took a good amount of time to figure out)

Then I un-commented the part after that, which is the #MODEL part (starting with defining fit_enet), and that part is where I'm having difficulties, even though it works without reactive programming part with user input from the UI.


In case you need a sample .csv file to upload into the dashboard, here's some code to generate it:

df <- 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(df, 'sample_data.csv')

After upload, change Select predicted variable to yy, Select time variable to datee, and click on the 'model' tab.

This topic was automatically closed 21 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.