【问题标题】:Automatically redirect R Markdown app to a different link自动将 R Markdown 应用程序重定向到不同的链接
【发布时间】:2021-08-18 17:59:36
【问题描述】:

我需要在带有 Shiny 运行时的 R Markdown 应用程序中自动重定向到不同的链接。我尝试了几种方法,但都不适合我。 (它们在 Shiny 中运行良好,但在 R Markdown 中却不行)。

如何让我的 R Markdown 应用将用户重定向到另一个页面?


这是我尝试过的事情的清单。

这个解决方案:Redirect in Shiny app 在 Shiny 中工作,但我无法在 R Markdown 中工作。

This solution 再次在 Shiny 中工作,但在 R Markdown 中失败:

---
title: "Untitled"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
runtime: shiny
---


```{r redirect}
    singleton(tags$head(tags$script('window.location.replace("https://stackoverflow.com");')))

```

我还尝试了基于How do I redirect to another webpage?shinyjs 方法,该方法在Shiny 中运行良好,但在R Markdown 中则不行:

```{r redirect_lab}
library(shinyjs)
useShinyjs(rmd = TRUE)

##both fail in Rmd
runjs('window.location.replace("https://stackoverflow.com");')
# runjs('window.location.href = "https://stackoverflow.com";')
```

我还尝试了一种 hacky 方法,它创建一个链接,将其绑定到一个按钮,然后使用 shinyjs 以编程方式单击该按钮: How to select a specific tab in R Markdown?

```{r redirect_lab}
library(shinyjs)
useShinyjs(rmd = TRUE) 

tags$a(href = "https://stackoverflow.com",
  # set this button to `display: none;` but *not* to `hidden`
  shiny::actionButton("btn2", "redirect"
                      # , style = "display: none"
                      )
)
click("btn2")

```

奇怪的是,当 R Markdown 页面加载时,它不会自动重定向。但是如果我用鼠标手动单击按钮,那么它将重定向到链接。但我很困惑为什么这不能以编程方式工作。

【问题讨论】:

  • 您的解决方案不起作用的原因是因为您的 click 调用发生在反应性闪亮环境之外,并且不能保证在页面加载后发生(因此按钮可能不存在时该函数被调用)。在纯 JavaScript 页面中,这是由 $(document).ready({}) 回调处理的。

标签: r shiny r-markdown


【解决方案1】:

我们可以在 HTML header 中添加<meta> 标签并通过触发重定向

<meta http-equiv="refresh" content="0; url=http://www.stackoverflow.com/"/>'

请注意,这在浏览器中有效,但在 RStudio 查看器中无效。

---
title: "Untitled"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
    includes:
      in_header: myheader.html
runtime: shiny
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE )

fileConn <- file("myheader.html")
writeLines('<meta http-equiv="refresh" content="0; url=http://www.stackoverflow.com/"/>', fileConn)
close(fileConn)
```

【讨论】:

    【解决方案2】:

    我发现一个简单的解决方案是直接在 markdown 中包含重定向 javascript 作为显式 &lt;script&gt;,并且触发器是作为直接事件,或者作为显式 js 调用。

    #Option one: direct link to JS event
    <script type="text/javascript">
        document.getElementById("myButton").onclick = function () {
            location.href = "http://www.google.com";
        };
    </script>
    
    ```{r, echo=F}
    actionButton("myButton", "Redirect")
    ```
    
    #Option two: dedicate redirect js, triggered by shinyjs
    <script type="text/javascript">
        go_away = function () {
            location.href = "http://www.google.com";
        };
    </script> 
    
    ```{r, echo=F}
    shinyjs::useShinyjs(rmd = TRUE)
    actionButton("myButton2", "Redirect!!")
    
    observeEvent(input$myButton2, {
      shinyjs::runjs('go_away()')
    })
    
    ```
    

    第一个选项有效,因为 r Markdown/Shiny 通过 ID 显式创建输入元素。对于不那么 hacky 的第二个选项,也可以从 runjs 调用中显式调用该函数。

    【讨论】:

    • 不知道为什么,但这些选项都不适合我。你在 Rmd 标头中做了什么特别的事情吗?
    猜你喜欢
    • 2017-01-03
    • 1970-01-01
    • 1970-01-01
    • 2013-12-27
    • 1970-01-01
    • 2013-02-10
    • 2013-08-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多