Redirect in Shiny app
Asked Answered
Y

2

15

I'm trying to make my Shiny app to redirect the user to another page. I'm using httr to send GET requests and see if the user is logged in. If he's not, I want to redirect him to another link.

Can I do that using R / Shiny only, or do I need some extra libraries?

sample:

library(httr)
library(shiny)
shinyServer(function(input, output) {
rv <- reactiveValues()
rv$mytoken = session$request$token

observeEvent(input$button1, {
  rv$a <- GET("my.url:3405/authtoken", 
              add_headers(
                .headers = c("token" = rv$mytoken)
              ))
  if (rv$a$status_code == 200) {
  } else {
    # redirect magic
  }
})
}

shinyUI(fluidPage(
  actionButton(button1, "btn")
))
Yuji answered 7/11, 2017 at 12:21 Comment(4)
Can you provide sample code with username and password?Skijoring
It's working in a different way: i have auth token in the session$request environment, and i check with GET if it's the same on the server. So I redirect user based on status code I'm getting in response. I will provide some code if neededYuji
You can still provide sample app without it, so when false then redirectSkijoring
Is an example sufficient?Yuji
S
13

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)
Skijoring answered 7/11, 2017 at 12:59 Comment(9)
As usual, you make it work :)The thing I was looking for, thank you! It seems though that you 'force' it to redirect the user. Doesn't shiny support this behaviour by default?Yuji
Its easy to do within the ui where you can bind buttons and links but you need to send it back the redirect from server and theshiny team added the sendCustomMessage which is very useful. So just go through it, you will pick it up quite fastSkijoring
have a look at pasteSkijoring
Yeah yeah my bad, i'll handle it now. Thanks again.Yuji
Hi, I don't know if I should create new question - if my app is inside an iframe - it seems that it redirects only inside it, and not the whole webpage. Unfortunately, I can't produce any examples here. Do you think by chance there is a way to redirect the whole page to another url, using only shiny?Yuji
There is an example how to capture click event within an iframe by John Harrison, have a look at that and study it, then you will be able to apply to your case. #46094896Skijoring
I just used window.top.location.href instead of window.location and it worked fine. Peace!Yuji
What if I need to include some reactive value in the url?Vedis
Is there no way to do this without JavaScript? Why can't the server side return a 302 response? Also what happens if JavaScript is disabled on client browser in this example?Mascot
O
6

Just to update. There is also an easier way...

shinyjs::runjs(paste0('window.location.href = "...";'))

Don't forget useShinyjs() in UI.

Ovi answered 15/9, 2021 at 17:16 Comment(1)
Note your address must begin with https://Marigraph

© 2022 - 2024 — McMap. All rights reserved.