Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect in Shiny app

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")
))
like image 456
Claud H Avatar asked Nov 07 '17 12:11

Claud H


Video Answer


2 Answers

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)
like image 55
Pork Chop Avatar answered Oct 10 '22 23:10

Pork Chop


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

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

Don't forget useShinyjs() in UI.

like image 20
Konrad Stawiski Avatar answered Oct 11 '22 00:10

Konrad Stawiski