在 Shiny 应用程序中重定向 [英] Redirect in Shiny app

查看:66
本文介绍了在 Shiny 应用程序中重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让我的 Shiny 应用程序将用户重定向到另一个页面.我正在使用 httr 发送 GET 请求并查看用户是否登录.如果他没有登录,我想将他重定向到另一个链接.

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.

我可以只使用 R/Shiny 来做到这一点,还是需要一些额外的库?

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

示例:

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

推荐答案

这里如果不是 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)

这篇关于在 Shiny 应用程序中重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆