How to use shiny app as a target in drake

断了今生、忘了曾经 提交于 2020-08-26 10:12:06

问题


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.

  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)

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!