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")
))
4
  • Can you provide sample code with username and password?
    – Pork Chop
    Nov 7, 2017 at 12:29
  • 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 needed
    – vladli
    Nov 7, 2017 at 12:31
  • You can still provide sample app without it, so when false then redirect
    – Pork Chop
    Nov 7, 2017 at 12:32
  • Is an example sufficient?
    – vladli
    Nov 7, 2017 at 12:41

2 Answers 2

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)
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?
    – vladli
    Nov 7, 2017 at 13:01
  • 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 fast
    – Pork Chop
    Nov 7, 2017 at 13:03
  • have a look at paste
    – Pork Chop
    Nov 7, 2017 at 13:04
  • 1
    Yeah yeah my bad, i'll handle it now. Thanks again.
    – vladli
    Nov 7, 2017 at 13:05
  • 3
    I just used window.top.location.href instead of window.location and it worked fine. Peace!
    – vladli
    Nov 9, 2017 at 15:28
6

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

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

Don't forget useShinyjs() in UI.

1
  • Note your address must begin with https://
    – Jeff Bezos
    Dec 24, 2022 at 20:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.