在闪亮的应用程序中重定向

Cla*_*d H 10 redirect r httr shiny

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

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

样品:

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")
))
Run Code Online (Sandbox Code Playgroud)

Por*_*hop 7

如果没有,这将导航你谷歌 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)
Run Code Online (Sandbox Code Playgroud)

  • 我只使用`window.top.location.href`而不是`window.location`,它运行正常.和平! (2认同)

小智 6

只是为了更新。还有一个更简单的方法...

shinyjs::runjs(paste0('window.location.href = "...";'))
Run Code Online (Sandbox Code Playgroud)

不要忘记useShinyjs()在用户界面中。