I need to put an automatic redirect to a different link in an R Markdown app with Shiny runtime. I tried several approaches but none works for me. (They work fine in Shiny but not in R Markdown).
How can I make my R Markdown app redirect the user to another page?
Here's a list of things I tried.
This solution: Redirect in Shiny app works in Shiny, but I couldn't get it to work in R Markdown.
This solution, again, works in Shiny but fails in 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");')))
```
I also tried shinyjs
approach, based on How do I redirect to another webpage?, which works fine in Shiny but not in 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";')
```
I also tried a hacky approach that creates a link, ties it to a button and then programmatically clicks the button using 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")
```
Strangely when the R Markdown page loads it does not redirect automatically. But if I click the button manually with the mouse, then it will redirect to the link. But I'm stumped why this would not work programmatically.