Javascript 如何重定向到shiny中的动态URL?

Javascript 如何重定向到shiny中的动态URL?,javascript,r,shiny,Javascript,R,Shiny,在shinty应用程序中,当用户单击绘图中的某个点时,我希望将其重定向到另一个URL。重定向本身是基于中介绍的解决方案工作的,但是,我不清楚如何将动态属性(在服务器端计算)合并到URL中 例如: library(shiny) library(ggplot2) jscode <- "Shiny.addCustomMessageHandler('mymessage', function(message) {window.location = 'http://www.google.com';}

在shinty应用程序中,当用户单击绘图中的某个点时,我希望将其重定向到另一个URL。重定向本身是基于中介绍的解决方案工作的,但是,我不清楚如何将动态属性(在服务器端计算)合并到URL中

例如:

library(shiny)
library(ggplot2)

jscode <- "Shiny.addCustomMessageHandler('mymessage', function(message) {window.location = 'http://www.google.com';});"

ui <- fluidPage(
tags$head(tags$script(jscode)),
plotOutput("scatter", click = "plot_click")
)

server <- function(input, output, session) {

    observeEvent(input$plot_click, {
        selectedTiles = nearPoints(iris, input$plot_click, threshold = 100, maxpoints = 1)

        if(nrow(selectedTiles)>0){
            # todo how to include the species in the redirect URL?
            # e.g. https://www.google.com/?q=versicolor
            session$sendCustomMessage("mymessage", "mymessage")
        }
    })

    output$scatter = renderPlot({
        ggplot(iris, aes(Sepal.Width, Petal.Width)) + geom_point()
    })
}

shinyApp(ui,server)
库(闪亮)
图书馆(GG2)

jscode您只需将参数传递到
mymessage
函数中,如下所示:

library(shiny)
library(ggplot2)

jscode <- "Shiny.addCustomMessageHandler('mymessage', function(message) { window.location = message;});"

ui <- fluidPage(
  tags$head(tags$script(jscode)),
  plotOutput("scatter", click = "plot_click")
)

server <- function(input, output, session) {

  observeEvent(input$plot_click, {
    selectedTiles = nearPoints(iris, input$plot_click, threshold = 100, maxpoints = 1)

    if(nrow(selectedTiles)>0){
      # todo how to include the species in the redirect URL?
      url <- "https://stackoverflow.com/questions/57755830/how-to-redirect-to-a-dynamic-url-in-shiny/57756048#57756048"
      session$sendCustomMessage("mymessage", url)
    }
  })

  output$scatter = renderPlot({
    ggplot(iris, aes(Sepal.Width, Petal.Width)) + geom_point()
  })
}

shinyApp(ui,server)
库(闪亮)
图书馆(GG2)

jscode您只需将参数传递到
mymessage
函数中,如下所示:

library(shiny)
library(ggplot2)

jscode <- "Shiny.addCustomMessageHandler('mymessage', function(message) { window.location = message;});"

ui <- fluidPage(
  tags$head(tags$script(jscode)),
  plotOutput("scatter", click = "plot_click")
)

server <- function(input, output, session) {

  observeEvent(input$plot_click, {
    selectedTiles = nearPoints(iris, input$plot_click, threshold = 100, maxpoints = 1)

    if(nrow(selectedTiles)>0){
      # todo how to include the species in the redirect URL?
      url <- "https://stackoverflow.com/questions/57755830/how-to-redirect-to-a-dynamic-url-in-shiny/57756048#57756048"
      session$sendCustomMessage("mymessage", url)
    }
  })

  output$scatter = renderPlot({
    ggplot(iris, aes(Sepal.Width, Petal.Width)) + geom_point()
  })
}

shinyApp(ui,server)
库(闪亮)
图书馆(GG2)
jscode