如何使用逻辑参数选择在哪里打开R shiny应用程序(RStudio查看器窗格或浏览器)

bvk5enib  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(116)

我正在开发一个R shiny应用程序,它被 Package 在一个函数中。让我们称之为launchApp()。我想将Browser参数添加到launchApp()中,以便当Browser = TRUE时,应用程序在RStudio中启动,而当Browser = TRUE时,应用程序在浏览器中启动。
设置Browser = TRUE可以正常工作,但是Browser = TRUE会导致应用程序在启动时没有打开通常的RStudio查看器。我不知道如何解决这个问题。
这是一个reprex,其中包含一个通过函数launchApp(Browser = browser)启动的假应用程序。我希望当Browser = browser时,它能在RStudio Viewer中正确打开应用程序,但这并没有发生。

library(shiny)

# Define a function to launch the Shiny app
launchApp <- function(Browser = FALSE) {
  # UI
  ui <- fluidPage(
    titlePanel("Minimal Shiny App"),
    sidebarLayout(
      sidebarPanel(
        sliderInput("slider", "Number of bins:",
                    min = 1, max = 50, value = 30)
      ),
      mainPanel(
        plotOutput("distPlot")
      )
    )
  )
  
  # server
  server <- function(input, output) {
    output$distPlot <- renderPlot({
      x    <- faithful$waiting
      bins <- seq(min(x), max(x), length.out = input$slider + 1)
      hist(x, breaks = bins, col = 'darkgray', border = 'white')
    })
  }
  
  # Launch the app with the specified browser option
  shinyApp(ui = ui, server = server, options = list(launch.browser = Browser))
}

# This seems to launch the app, but I do not see the RStudio viewer pop-up
launchApp()
# I only get a console message: Listening on http://127.0.0.1:3889
# if open that link in the browser, I see the app and I can interact with it

# This one launches the app in the browser correctly
# launchApp(TRUE)

字符串

k2arahey

k2arahey1#

设置launch.browser = FALSE旨在用于非交互式会话。
我看不出将应用 Package 在一个函数中以启动它的意义。请参阅?runApp
launch.browser:如果为true,则系统的默认Web浏览器将在应用程序启动后自动启动。仅在交互式会话中设置为true。此参数的值也可以是一个函数,用于使用应用程序的URL进行调用。
并检查以下内容:

library(shiny)

# UI
ui <- fluidPage(
  titlePanel("Minimal Shiny App"),
  sidebarLayout(
    sidebarPanel(
      sliderInput("slider", "Number of bins:",
                  min = 1, max = 50, value = 30)
    ),
    mainPanel(
      plotOutput("distPlot")
    )
  )
)

# server
server <- function(input, output) {
  output$distPlot <- renderPlot({
    x    <- faithful$waiting
    bins <- seq(min(x), max(x), length.out = input$slider + 1)
    hist(x, breaks = bins, col = 'darkgray', border = 'white')
  })
}

# Launch the app with the specified browser option
appObj <- shinyApp(ui = ui, server = server)

# opens default browser
# runApp(appObj, launch.browser = TRUE)

# opens app in viewer pane
runApp(appObj, launch.browser = function(url) {invisible(.Call("rs_shinyviewer", url, getwd(), "pane", NULL, PACKAGE = "(embedding)"))})

字符串
PS:如果你仍然想写一个自定义函数,只需复制并粘贴上面的匿名函数,并将其传递给launch.browser参数。

jc3wubiy

jc3wubiy2#

我想出了一个非常简单的解决办法-我真的不知道为什么这不是我尝试的第一件事。

library(shiny)

# Define a function to launch the Shiny app
launchApp <- function(Browser = FALSE) {
  # UI
  ui <- fluidPage(
    titlePanel("Minimal Shiny App"),
    sidebarLayout(
      sidebarPanel(
        sliderInput("slider", "Number of bins:",
                    min = 1, max = 50, value = 30)
      ),
      mainPanel(
        plotOutput("distPlot")
      )
    )
  )
  
  # server
  server <- function(input, output) {
    output$distPlot <- renderPlot({
      x    <- faithful$waiting
      bins <- seq(min(x), max(x), length.out = input$slider + 1)
      hist(x, breaks = bins, col = 'darkgray', border = 'white')
    })
  }
  
  # Launch the app with the specified browser option
  # simple ifelse wrap prevents from calling launch.browser = FALSE when Browser = FALSE 
  if (Browser == FALSE) {
    shinyApp(ui = ui, server = server)
  } else {
    shinyApp(ui = ui, server = server, options = list(launch.browser = TRUE))
  }
  
}

launchApp() # and launchApp(TRUE) correctly launches into the browser

字符串

相关问题