为什么将来自另一个应用程序的 app.R 保存在新应用程序中时,Shiny 应用程序不起作用?

Why Shiny app does not work when app.R from another app is saved in a new one?

我注意到,每当我想创建一个新的闪亮应用程序时,我都必须在新的应用程序文件夹中创建一个新的 app.R 文件。如果我 'save as' 来自新文件夹中另一个闪亮应用程序的 app.R 并根据当前使用情况对其进行修改,则会创建 UI 但服务器似乎无法正常工作。这是为什么?

看看当您按下 "Run App" 按钮时会发生什么——在控制台中执行的命令是 shiny::runApp(appDir)。它不 运行 特别是 app.R 文件,而是 运行 整个目录。这是您看到此行为的第一个提示 - Shiny 期望 运行 将应用程序作为目录而不是文件,因此当您在同一文件夹中有不同的应用程序文件时,原始文件是可能还在 运行.

这让我研究了 runApp 函数。如果您查看 runApp 的文档,您会看到第一个参数是 appDir:

The directory of the application. Should contain server.R, plus, either ui.R or a www directory that contains the file index.html. Alternately, instead of server.R and ui.R, the directory may contain just app.R. Defaults to the working directory. Instead of a directory, this could be a list with ui and server components, or a Shiny app object created by shinyApp.

所以你有它 - runApp(因此 "Run App" 按钮)使用目录作为 Shiny 应用程序的参数。 Shiny 希望这个目录有 server.Rui.R 或者只有 app.R,但它没有说明当你有一个或多个 [=19] 的组合时的行为是什么=] 或以上都不是,所以你应该避免这种情况。