我需要把一个自动重定向到一个不同的链接在一个runtime应用程序与闪亮的运行时。我尝试了几种方法,但没有一种适合我。(它们在闪闪发亮的地方很好,但在中不起作用)。
如何使我的应用程序将用户重定向到另一个页面?
这是我试过的事情清单。
这个解决方案:在闪亮的应用程序中重定向在闪亮中工作,但我无法让它在R中工作。
同样,这个解决方案在R中运行得很好,但失败了:
---
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的如何重定向到另一个网页?方法,它在闪亮的环境中工作得很好,但在R中却不起作用:
```{r redirect_lab}图书馆(Shinyjs)
useShinyjs(rmd = TRUE)
##both在Rmd中失败
runjs('window.location.replace("https://stackoverflow.com");')
runjs('window.location.href = "https://stackoverflow.com";')
我还尝试了一种创建链接、将其绑定到按钮并以编程方式使用shinyjs:如何在R标记中选择特定的选项卡?单击按钮的方法。
```{r redirect_lab}图书馆(Shinyjs)
useShinyjs(rmd = TRUE)
标签$a(href= "https://stackoverflow.com",
将此按钮设置为display: none;,但不设置为hidden
闪亮::actionButton(“btn2”,“重定向”)
# , style = "display: none" ))
点击(Btn2)
奇怪的是,当R页面加载时,它不会自动重定向。但是如果我用鼠标手动单击按钮,它就会重定向到链接。但我很难理解为什么这不能通过编程实现。
发布于 2021-08-20 21:12:18
我们可以将<meta>标记添加到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(回声=假)
fileConn <- file("myheader.html")
writeLines('',fileConn)
关闭(FileConn)
发布于 2021-08-21 18:35:24
我发现的一个简单解决方案是直接将重定向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",“重定向”)
#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",“重定向!”)
observeEvent(输入$myButton2 2,{
shinyjs::runjs('go_away()')
})
第一个选项起作用,因为r/Shiny通过ID显式地创建输入元素。对于较少的hacky第二个选项,函数也可以从runjs调用显式调用。
https://stackoverflow.com/questions/68837294
复制相似问题