Redirect in Shiny App

Redirect in Shiny app

Here this will navigate you to google if not true

library(shiny)

jscode <- "Shiny.addCustomMessageHandler('mymessage', function(message) {window.location = 'http://www.google.com';});"

ui <- fluidPage(
tags$head(tags$script(jscode)),
checkboxInput("Redirect","Redirect",value = T)
)

server <- function(input, output, session) {

observeEvent(input$Redirect,{
if(!input$Redirect){
session$sendCustomMessage("mymessage", "mymessage")
}
})
}

shinyApp(ui,server)

How to redirect to a dynamic URL in shiny?

You just pass the argument into the mymessage function, like so:

library(shiny)
library(ggplot2)

jscode <- "Shiny.addCustomMessageHandler('mymessage', function(message) { window.location = message;});"

ui <- fluidPage(
tags$head(tags$script(jscode)),
plotOutput("scatter", click = "plot_click")
)

server <- function(input, output, session) {

observeEvent(input$plot_click, {
selectedTiles = nearPoints(iris, input$plot_click, threshold = 100, maxpoints = 1)

if(nrow(selectedTiles)>0){
# todo how to include the species in the redirect URL?
url <- "https://stackoverflow.com/questions/57755830/how-to-redirect-to-a-dynamic-url-in-shiny/57756048#57756048"
session$sendCustomMessage("mymessage", url)
}
})

output$scatter = renderPlot({
ggplot(iris, aes(Sepal.Width, Petal.Width)) + geom_point()
})
}

shinyApp(ui,server)

How to make Shiny redirect immediately?

Motivation: I want to be able to download files directly into an R
console session from a URL pointing at a Shiny app.

...i.e. this amounts to a very roundabout way of trying to serve static content from a shiny app. Turns out I don't need to redirect or use downloadHandler at all. As this post on the Shiny forum says, any file I create inside the local www directory will be accessible as if it is at the root of my app directory. I.e. if I have my app do save.image(file='www/foo.rdata') then I will be able to access it from [http://www.myhost.com/appname/foo.rdata] if the app itself lives at [http://www.myhost.com/appname/]

Automatically redirect R Markdown app to a different link

We can add <meta> tags to the HTML header and trigger a redirect via

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

Note that this is working in the browser, but not in the RStudio Viewer.

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

How to auto redirect to tab in shiny without blocked by chrome by auto clicking link in shiny?

It seems like no immediate solution to solve the issue if the auto redirection is not first thing after users click the button. It will be always blocked by chrome. Therefore, I give up auto redirection and have to add pop up box and ask users to click "ok" button.

# something like below
ui <- fluidPage(useShinyjs(),
actionButton("download", "Download link"),
uiOutput("linktext"))

server <- function(input, output) {

observeEvent(input$download, {

# some functions to generate the link
##### Note: it will take ~20s #####
url <- funs(...)
output$linktext <- renderUI(tags$a(id="link-a", href = url, NULL, target = "_blank"))

## initiate fake popbox func to generate popup box
popbox(inputId = "popup-box", "okay", "cancel")
}

observeEvent(input$popup-box, {
req(input$popup-box == T)
runjs("$('#link-a')[0].click();"))
})


Related Topics



Leave a reply



Submit