自动将 R Markdown 应用重定向到另一个 link
Automatically redirect R Markdown app to a different link
我需要在具有 Shiny 运行时的 R Markdown 应用程序中自动重定向到不同的 link。我尝试了几种方法,但 none 对我有用。 (它们在 Shiny 中运行良好,但在 R Markdown 中运行不佳)。
如何让我的 R Markdown 应用程序将用户重定向到另一个页面?
这是我尝试过的事情的列表。
此解决方案: 在 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://whosebug.com");')))
```
我也尝试了 shinyjs
方法,基于 How do I redirect to another webpage?,它在 Shiny 中工作正常但在 R Markdown 中不工作:
```{r redirect_lab}
library(shinyjs)
useShinyjs(rmd = TRUE)
##both fail in Rmd
runjs('window.location.replace("https://whosebug.com");')
# runjs('window.location.href = "https://whosebug.com";')
```
我还尝试了一种创建 link 的 hacky 方法,将其绑定到一个按钮,然后使用 shinyjs
以编程方式单击该按钮:
```{r redirect_lab}
library(shinyjs)
useShinyjs(rmd = TRUE)
tags$a(href = "https://whosebug.com",
# set this button to `display: none;` but *not* to `hidden`
shiny::actionButton("btn2", "redirect"
# , style = "display: none"
)
)
click("btn2")
```
奇怪的是,当 R Markdown 页面加载时,它不会自动重定向。但是如果我用鼠标手动点击按钮,那么它会重定向到 link。但我很困惑为什么这不能以编程方式工作。
我们可以将 <meta>
标签添加到 HTML header 并通过
触发重定向
<meta http-equiv="refresh" content="0; url=http://www.whosebug.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.whosebug.com/"/>', fileConn)
close(fileConn)
```
我找到的一个简单的解决方案是直接在 markdown 中包含一个重定向 javascript 作为显式 <script>
,触发是直接事件,或者是显式 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 显式创建输入元素。对于不那么复杂的第二个选项,该函数也可以从 runjs
调用中显式调用。
我需要在具有 Shiny 运行时的 R Markdown 应用程序中自动重定向到不同的 link。我尝试了几种方法,但 none 对我有用。 (它们在 Shiny 中运行良好,但在 R Markdown 中运行不佳)。
如何让我的 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://whosebug.com");')))
```
我也尝试了 shinyjs
方法,基于 How do I redirect to another webpage?,它在 Shiny 中工作正常但在 R Markdown 中不工作:
```{r redirect_lab}
library(shinyjs)
useShinyjs(rmd = TRUE)
##both fail in Rmd
runjs('window.location.replace("https://whosebug.com");')
# runjs('window.location.href = "https://whosebug.com";')
```
我还尝试了一种创建 link 的 hacky 方法,将其绑定到一个按钮,然后使用 shinyjs
以编程方式单击该按钮:
```{r redirect_lab}
library(shinyjs)
useShinyjs(rmd = TRUE)
tags$a(href = "https://whosebug.com",
# set this button to `display: none;` but *not* to `hidden`
shiny::actionButton("btn2", "redirect"
# , style = "display: none"
)
)
click("btn2")
```
奇怪的是,当 R Markdown 页面加载时,它不会自动重定向。但是如果我用鼠标手动点击按钮,那么它会重定向到 link。但我很困惑为什么这不能以编程方式工作。
我们可以将 <meta>
标签添加到 HTML header 并通过
<meta http-equiv="refresh" content="0; url=http://www.whosebug.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.whosebug.com/"/>', fileConn)
close(fileConn)
```
我找到的一个简单的解决方案是直接在 markdown 中包含一个重定向 javascript 作为显式 <script>
,触发是直接事件,或者是显式 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 显式创建输入元素。对于不那么复杂的第二个选项,该函数也可以从 runjs
调用中显式调用。