如何在从模板创建 RStudio 项目期间 运行 闪亮应用程序?
How to run Shiny app during RStudio project creation from template?
我正在创建一个自定义 RStudio 项目模板,详见 here。
我已准备好创建新项目的所有工作,而且我还让 Shiny 应用程序自行运行。
但是,我现在想同步 运行 应用程序,相当于我在上面网页中的 hello_world()
功能。
我可以围绕我的 Shiny 应用程序编写包装函数,这些函数在通过 RStudio 菜单从模板创建新项目的上下文之外按需要工作,但在创建新项目的上下文中,就好像运行 该应用程序不存在,因为没有应用程序出现,并且没有发出任何消息、警告或错误。
# function works as expected outside context of creating a new project
run_app <- function() {
ui <- shiny::fluidPage(shiny::titlePanel("New Project"))
server <- function(input, output, session) {
session$onSessionEnded(function() {
shiny::stopApp()
})
}
shiny::shinyApp(ui = ui, server = server,
options = list(launch.browser = TRUE))
}
# but nothing Shiny related happens if called within the new project creation function
# the new project creation process continues as if the call to start the app is not present
hello_world <- function(path, ...) {
run_app()
}
是否可以在项目创建期间运行一个闪亮的应用程序?
?shiny::shinyApp
Normally when this function is used at the R console, the Shiny app object is automatically passed to the print() function, which runs the app. If this is called in the middle of a function, the value will not be passed to print() and the app will not be run. To make the app run, pass the app object to print() or runApp().
应用程序在其自包含函数中正确运行,因为返回值被无形地传递给打印,但当函数包含额外代码时不会发生这种情况。
因此,一个解决方案是将函数调用包装在 print()
.
中
hello_world <- function(path, ...) {
print(run_app())
}
我正在创建一个自定义 RStudio 项目模板,详见 here。
我已准备好创建新项目的所有工作,而且我还让 Shiny 应用程序自行运行。
但是,我现在想同步 运行 应用程序,相当于我在上面网页中的 hello_world()
功能。
我可以围绕我的 Shiny 应用程序编写包装函数,这些函数在通过 RStudio 菜单从模板创建新项目的上下文之外按需要工作,但在创建新项目的上下文中,就好像运行 该应用程序不存在,因为没有应用程序出现,并且没有发出任何消息、警告或错误。
# function works as expected outside context of creating a new project
run_app <- function() {
ui <- shiny::fluidPage(shiny::titlePanel("New Project"))
server <- function(input, output, session) {
session$onSessionEnded(function() {
shiny::stopApp()
})
}
shiny::shinyApp(ui = ui, server = server,
options = list(launch.browser = TRUE))
}
# but nothing Shiny related happens if called within the new project creation function
# the new project creation process continues as if the call to start the app is not present
hello_world <- function(path, ...) {
run_app()
}
是否可以在项目创建期间运行一个闪亮的应用程序?
?shiny::shinyApp
Normally when this function is used at the R console, the Shiny app object is automatically passed to the print() function, which runs the app. If this is called in the middle of a function, the value will not be passed to print() and the app will not be run. To make the app run, pass the app object to print() or runApp().
应用程序在其自包含函数中正确运行,因为返回值被无形地传递给打印,但当函数包含额外代码时不会发生这种情况。
因此,一个解决方案是将函数调用包装在 print()
.
hello_world <- function(path, ...) {
print(run_app())
}