gpt4 book ai didi

r - 自动将 R Markdown 应用程序重定向到不同的链接

转载 作者:行者123 更新时间:2023-12-04 14:05:06 25 4
gpt4 key购买 nike

我需要在带有 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");')))

```

我还尝试了 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://stackoverflow.com");')
# runjs('window.location.href = "https://stackoverflow.com";')
```

我还尝试了一种创建链接的 hacky 方法,将其绑定(bind)到按钮,然后使用 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 页面加载时,它不会自动重定向。但是如果我用鼠标手动点击按钮,那么它会重定向到链接。但我很困惑为什么这不能以编程方式工作。

最佳答案

我们可以添加<meta>标记到 HTML header 并通过

触发重定向

<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)
```

关于r - 自动将 R Markdown 应用程序重定向到不同的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68837294/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com