问题
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?
回答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.
- As long as the app is running,
make()
will never finish. - 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)
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)
来源:https://stackoverflow.com/questions/61359114/how-to-use-shiny-app-as-a-target-in-drake