【问题标题】:R Shiny redirect to two URLsR Shiny 重定向到两个 URL
【发布时间】:2021-08-03 22:36:43
【问题描述】:

只要您按下“提交”按钮,下面的 Shiny 应用就会重定向到 Google。我想要一个类似的应用程序,它重定向到两个 URL(按顺序)。因此,例如,它会先访问“https://www.google.com”,然后再访问“https://www.stackoverflow.com”。这可能吗?

library(shiny)

jscode <- "Shiny.addCustomMessageHandler('mymessage', function(message) { window.location = message;});"

ui <- fluidPage(
  tags$head(tags$script(jscode)),
  actionButton("submit", "Submit")
)

server <- function(input, output, session) {
  
  observeEvent(input$submit, {
      url <- "https://www.google.com"
      session$sendCustomMessage("mymessage", url)
  })
  
}

shinyApp(ui,server)

【问题讨论】:

  • 没有。这根本不是闪亮的。当您进行重定向时,您将无法再控制用户的浏览器。他们已经离开了您的网站,之后您无法让他们去任何地方。这样的功能将是一场安全噩梦。一种可能的解决方法是让您的网站使用 iframe 来包含来自其他网站的内容。但这是一种非常不同的设置。

标签: javascript r shiny


【解决方案1】:
jscode <- "Shiny.addCustomMessageHandler('redirect', function(URLS) {
  for (var i = 0; i < URLS.length; i++) {
    setTimeout(function() {
      var a = document.createElement('a');
      a.style.display = 'none';
      a.href = URLS[i];
      a.target = '_blank';
      document.body.appendChild(a);
      a.click();
      a.remove();
    }, i * 500);
  }
});"

  observeEvent(input$submit, {
      urls <- list("https://www.google.com", "https://www.stackoverflow.com")
      session$sendCustomMessage("redirect", urls)
  })

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-08
    • 1970-01-01
    • 2013-03-15
    • 1970-01-01
    • 1970-01-01
    • 2017-09-16
    • 2014-04-12
    • 1970-01-01
    相关资源
    最近更新 更多