如何从Drake计划中将闪亮的应用程序部署到Shinyapps.io [英] How to deploy shiny app to shinyapps.io from drake plan

查看:94
本文介绍了如何从Drake计划中将闪亮的应用程序部署到Shinyapps.io的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是从将应用程序和数据传递给Shiny部署功能时关闭循环的后续问题:

This is a follow-on question from closing the loop on passing the app and data to a Shiny deployment function:

如何使用闪亮的应用作为德雷克的目标

我想直接从以下Drake计划中部署Shiny应用程序.

I would like to deploy a Shiny app directly from a drake plan as below.

library(drake)
library(shiny)

plan <- drake_plan(
  cars_data = mtcars,
  deployment = custom_deployment_function(file_in("app.R"), cars_data)
)

custom_shiny_deployment <- function(file, data_input) {

    rsconnect::deployApp(
    appFiles = file,
    appName = "cars",
    forceUpdate = TRUE
  )
}

Saved as "app.R" in project working directory (the app works outside of drake if data_input <- mtcars) :

shinyApp(
  ui = fluidPage(
    selectInput("variable", "Variable:",
                c("Cylinders" = "cyl",
                  "Transmission" = "am")),
    tableOutput("data")
  ),
  server = function(input, output) {
    output$data <- renderTable({
      data_input[, c("mpg", input$variable), drop = FALSE]
    }, rownames = TRUE)
  }
)

shinyapps.io的日志显示"cars_data"来自德雷克的消息没有被传递给闪亮服务器"data_input".在custom_deployment_function环境中:

Logs from shinyapps.io shows "cars_data" from drake is not being passed to the Shiny server "data_input" in the custom_deployment_function environment:

2020-07-14T20:12:57.418989+00:00 shinyapps[2569696]: htmltools version: 0.5.0
2020-07-14T20:12:57.419186+00:00 shinyapps[2569696]: Using pandoc: /opt/connect/ext/pandoc2
2020-07-14T20:12:57.589960+00:00 shinyapps[2569696]: Using jsonlite for JSON processing
2020-07-14T20:12:57.593223+00:00 shinyapps[2569696]: 
2020-07-14T20:12:57.593224+00:00 shinyapps[2569696]: Starting R with process ID: '25'
2020-07-14T20:12:57.628389+00:00 shinyapps[2569696]: 
2020-07-14T20:12:57.628390+00:00 shinyapps[2569696]: Listening on http://127.0.0.1:38608
2020-07-14T20:13:14.686424+00:00 shinyapps[2569696]: Running on host: e89f96d02b6e
2020-07-14T20:13:14.691423+00:00 shinyapps[2569696]: Server version: 1.8.4-11
2020-07-14T20:13:14.691436+00:00 shinyapps[2569696]: LANG: en_US.UTF-8
2020-07-14T20:13:14.691436+00:00 shinyapps[2569696]: R version: 4.0.0
2020-07-14T20:13:14.691437+00:00 shinyapps[2569696]: shiny version: 1.5.0
2020-07-14T20:13:14.691467+00:00 shinyapps[2569696]: rmarkdown version: (none)
2020-07-14T20:13:14.691648+00:00 shinyapps[2569696]: Using pandoc: /opt/connect/ext/pandoc2
2020-07-14T20:13:14.691475+00:00 shinyapps[2569696]: knitr version: (none)
2020-07-14T20:13:14.857243+00:00 shinyapps[2569696]: Using jsonlite for JSON processing
2020-07-14T20:13:14.691443+00:00 shinyapps[2569696]: httpuv version: 1.5.4
2020-07-14T20:13:14.895595+00:00 shinyapps[2569696]: 
2020-07-14T20:13:14.691480+00:00 shinyapps[2569696]: jsonlite version: 1.7.0
2020-07-14T20:13:14.860404+00:00 shinyapps[2569696]: 
2020-07-14T20:13:14.860404+00:00 shinyapps[2569696]: Starting R with process ID: '41'
2020-07-14T20:13:14.895596+00:00 shinyapps[2569696]: Listening on http://127.0.0.1:42022
2020-07-14T20:13:14.691496+00:00 shinyapps[2569696]: RJSONIO version: (none)
2020-07-14T20:13:14.691506+00:00 shinyapps[2569696]: htmltools version: 0.5.0
2020-07-14T20:15:57.441648+00:00 shinyapps[2569696]: Running on host: a2e664ad9837
2020-07-14T20:15:57.447236+00:00 shinyapps[2569696]: Server version: 1.8.4-11
2020-07-14T20:15:57.447326+00:00 shinyapps[2569696]: R version: 4.0.0
2020-07-14T20:15:57.447337+00:00 shinyapps[2569696]: knitr version: (none)
2020-07-14T20:15:57.447337+00:00 shinyapps[2569696]: rmarkdown version: (none)
2020-07-14T20:15:57.447327+00:00 shinyapps[2569696]: shiny version: 1.5.0
2020-07-14T20:15:57.447260+00:00 shinyapps[2569696]: LANG: en_US.UTF-8
2020-07-14T20:15:57.447337+00:00 shinyapps[2569696]: httpuv version: 1.5.4
2020-07-14T20:15:57.447338+00:00 shinyapps[2569696]: jsonlite version: 1.7.0
2020-07-14T20:15:57.447338+00:00 shinyapps[2569696]: RJSONIO version: (none)
2020-07-14T20:15:57.667089+00:00 shinyapps[2569696]: 
2020-07-14T20:15:57.447494+00:00 shinyapps[2569696]: Using pandoc: /opt/connect/ext/pandoc2
2020-07-14T20:15:57.630039+00:00 shinyapps[2569696]: Starting R with process ID: '24'
2020-07-14T20:15:57.626886+00:00 shinyapps[2569696]: Using jsonlite for JSON processing
2020-07-14T20:15:57.447347+00:00 shinyapps[2569696]: htmltools version: 0.5.0
2020-07-14T20:15:57.630037+00:00 shinyapps[2569696]: 
2020-07-14T20:15:57.667090+00:00 shinyapps[2569696]: Listening on http://127.0.0.1:40107
2020-07-14T20:16:14.758082+00:00 shinyapps[2569696]: Warning: Error in renderTable: object 'data_input' not found
2020-07-14T20:16:14.772348+00:00 shinyapps[2569696]:   108: renderTable [/srv/connect/apps/cars/app.R#13]
2020-07-14T20:16:14.772349+00:00 shinyapps[2569696]:   107: func
2020-07-14T20:16:14.772350+00:00 shinyapps[2569696]:    94: origRenderFunc
2020-07-14T20:16:14.772350+00:00 shinyapps[2569696]:    93: output$data
2020-07-14T20:16:14.772350+00:00 shinyapps[2569696]:    13: runApp
2020-07-14T20:16:14.772351+00:00 shinyapps[2569696]:    12: fn
2020-07-14T20:16:14.772351+00:00 shinyapps[2569696]:     7: connect$retry
2020-07-14T20:16:14.772352+00:00 shinyapps[2569696]:     6: eval
2020-07-14T20:16:14.772352+00:00 shinyapps[2569696]:     5: eval

很抱歉,这很琐碎,但是在小鸭和闪亮之间,它逃避了我.

Apologies if this is trivial, but between drake and shiny, it is escaping me.

推荐答案

现在,我了解了您如何部署应用程序,我可以说这是预期的行为.是的,您的custom_shiny_deployment()有权访问数据,但已部署的应用程序无法访问该数据,因为rsconnect::deployApp()不会从调用环境中发送对象.如果您希望数据可用于该应用程序,建议您将其保存(并使用file_in()file_out()进行跟踪),然后通过custom_shiny_deployment()将其传递给deployApp()appFiles参数.

Now that I see how you are deploying the app, I can say that this is expected behavior. Yes, your custom_shiny_deployment() has access to the data, but the deployed app does not because rsconnect::deployApp() does not ship objects from the calling environment. If you want the data to be available to the app, I recommend saving it (and tracking it with file_in() and file_out()) then passing it to the appFiles argument of deployApp() via custom_shiny_deployment().

您的app.R可以保持原样.

app.R与您所写的相同.

library(shiny)

cars_data <- readRDS("cars_data.RDS")

shinyApp(
  ui = fluidPage(
    selectInput("variable", "Variable:",
                c("Cylinders" = "cyl",
                  "Transmission" = "am")),
    tableOutput("data")
  ),
  server = function(input, output) {
    output$data <- renderTable({
      cars_data[, c("mpg", input$variable), drop = FALSE]
    }, rownames = TRUE)
  } 
)

但是从drake的角度来看,您需要类似file_in()file_out()的东西,以确保正确的目标以正确的顺序运行并响应对cars_data.RDSapp.R的更改.

But from drake's side, you need something like file_in() and file_out() to ensure the correct targets run in the correct order and in response to changes to cars_data.RDS and app.R.

library(drake)

plan <- drake_plan(
  cars_data = mtcars,
  save_cars = saveRDS(cars_data, file_out("cars_data.RDS")),
  deployment = rsconnect::deployApp(
    appFiles = file_in("app.R", "cars_data.RDS"),
    appName = "cars",
    forceUpdate = TRUE
  )
)

make(plan)

如果您确实希望appFilesNULL,则drake仍然需要file_in(),因此它知道依赖于物理文件.

If you really want appFiles to be NULL, drake still needs file_in() so it knows to depend on physical files.

plan <- drake_plan(
  cars_data = mtcars,
  save_cars = saveRDS(cars_data, file_out("cars_data.RDS")),
  deployment = deploy_app(file_in("cars_data.RDS", "app.R"))
)

deploy_app <- function(...) {
  rsconnect::deployApp(
    appFiles = NULL,
    appName = "cars",
    forceUpdate = TRUE
  )
}

无论哪种方式,我建议您先使用plot()vis_drake_graph()检查计划,然后再使用make()运行计划.上面的两个计划都在数据文件的下游显示deployment(带有传入的箭头),这正是我们想要的.

Either way, I recommend checking the plan with plot() or vis_drake_graph() before you run it with make(). Both plans above show deployment downstream of the data file (with incoming arrows) which is exactly what we want.

plot(plan)

如果没有这些文件标记,则drake有机会在数据准备就绪之前就部署应用程序.

Without these file markers, there is a chance that drake will deploy the app before the data is ready to go.

disconnected_plan <- drake_plan(
  cars_data = mtcars,
  saveRDS(cars_data, "cars_data.RDS"),
  deployment =  rsconnect::deployApp(
    appFiles = NULL,
    appName = "cars",
    forceUpdate = TRUE
  )
)

plot(disconnected_plan)

这篇关于如何从Drake计划中将闪亮的应用程序部署到Shinyapps.io的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆