我注意到,每当我想创建一个新的 Shiny 应用程序时,我都必须在新的应用程序文件夹中创建一个新的 app.R 文件。如果我从新文件夹中的另一个 Shiny 应用程序“另存为”app.R 并将其修改为当前使用情况,则会创建 UI,但服务器似乎无法工作。这是为什么?
最佳答案
看看当您按下“运行应用程序”按钮时会发生什么 - 在控制台中执行的命令是 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
(因此“运行应用程序”按钮)使用目录作为 Shiny 应用程序的参数。 Shiny 期望此目录具有 server.R
和 ui.R
或仅 app.R
,但它没有说明行为是什么是指您拥有该应用程序或多个应用程序
的组合,或者没有上述应用程序,因此您应该避免这种情况。
关于r - 为什么当另一个应用程序中的 app.R 保存在新应用程序中时,Shiny 应用程序无法工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31507518/