0

How to pass previous target (df) to ui and server functions that I use in the next command shinyApp. My plan looks like this:

plan <- drake_plan(
  df = faithful,
  app = shinyApp(ui, server)
)

ui and server are copied from the shiny tutorial. There's only one difference - I changed faithful to df (data in the previous target). Now I'm getting an error:

Warning: Error in $: object of type 'closure' is not subsettable
  [No stack trace available]

How to solve this? What's the best practice?

mihagazvoda
  • 1,057
  • 13
  • 23

1 Answers1

1

drake targets should return fixed data objects that can be stored with saveRDS() (or alternative kinds of files if you are using specialized formats). I recommend having a look at https://books.ropensci.org/drake/plans.html#how-to-choose-good-targets. There issues with defining a running instance of a Shiny app as a target.

  1. As long as the app is running, make() will never finish.
  2. It does not really make sense to save the return value of shinyApp() as a data object. That's not really what a target is for. The purpose of a target is to reproducibly cache the results of a long computation so you do not need to rerun it unless some upstream code or data change.

Instead, I think the purpose of the app target should be to deploy to a website like https://shinyapps.io. To make the app update when df changes, be sure to mention df as a symbol in a command so that drake's static code analyzer can pick it up. Also, use file_in() to declare your Shiny app scripts as dependencies so drake automatically redeploys the app when the code changes.

library(drake)

plan <- drake_plan(
  df = faithful,
  deployment = custom_deployment_function(file_in("app.R"), df)
)

custom_deployment_function <- function(file, ...) {
  rsconnect::deployApp(
    appFiles = file,
    appName = "your_name",
    forceUpdate = TRUE
  )
}

Also, be sure to check the dependency graph so you know drake will run the correct targets in the correct order.

vis_drake_graph(plan)

enter image description here

In your previous plan, the command for the app did not mention the symbol df, so drake did not know it needed to run one before the other.

plan <- drake_plan(
  df = faithful,
  app = shinyApp(ui, server)
)
vis_drake_graph(plan)

enter image description here

landau
  • 5,636
  • 1
  • 22
  • 50
  • Thank you so much for drake, I wasn't clear if this was fully resolved for the initial query and I also am not quite there. When I used custom_deployment_function with this faithful app (https://shiny.rstudio.com/articles/basics.html), only changing faithful$waiting to df$waiting, but still get "closure not subsettable". The app works perfectly on its own outside of drake, but seems like some data and/or packages from the drake plan may not be getting passed into Shiny. – David Lucey Jul 13 '20 at 21:28
  • Maybe change df to a name that does not conflict with a base function? If that doesn’t work or does not generate a useful error message, I think this would be easier to troubleshoot in a new post with an end to end reproducible example. – landau Jul 13 '20 at 21:43
  • I couldn't get there changing df so put up a new post. – David Lucey Jul 14 '20 at 20:32
  • Thanks. Would you comment with the link? Having trouble finding it. – landau Jul 14 '20 at 23:34
  • https://stackoverflow.com/questions/62903543/how-to-deploy-shiny-app-to-shinyapps-io-from-drake-plan – David Lucey Jul 14 '20 at 23:40