shiny_callback 在 rbokeh 中捕获了什么?

What does shiny_callback capture in rbokeh?

我正在尝试使用 rbokeh 中的 tool_lasso_select 功能。我想要的是让用户使用该工具 select 散点图中的一组点,并且将在下一个面板中创建点组 selected 的汇总统计数据闪亮的 tabBox。我的主要问题是:

shiny_callback 选项究竟捕获了什么?

这是我正在创建的图形类型的示例:

figure() %>%
ly_points(x = cty, y = hwy, data = mpg,
        hover = c(cty, hwy), lname = "points") %>%
tool_hover(shiny_callback(id = "hover_info"), "points") %>%
tool_tap(shiny_callback(id = "tap_info"), "points") %>%
tool_box_select(shiny_callback(id = "selection_info"), "points")

"selection_info" id 中的内容到底是什么?我如何提取该信息?我只是不确定我可以编写下一个代码块来获取 lasso_tool 捕获的信息。我在文档中能找到的最多的是使用的示例,但输出似乎只是一个索引号。

更新:

感谢您添加可重现的示例。我很抱歉。我在下面添加了更多内容:

library("dplyr")
library("rbokeh")
library("shiny")

attach(mtcars)

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

  output$myChart <- renderRbokeh({
    print(paste0("now rendering!"," ",date()))
    input$myText
    figure() %>%
      ly_points(x = mtcars$cyl, y = mtcars$hp, data = mtcars,
                hover = c(mtcars$cyl, mtcars$hp), lname = "points") %>%
      tool_hover(callback = shiny_callback(id = "hover_info"), "points") %>%
      tool_tap(callback = shiny_callback(id = "tap_info"), "points") %>%
      tool_box_select(callback = shiny_callback(id = "selection_info"), "points")
  })

  output$selection_summary <- renderText({
    input$selection_info
  })


})

ui <- shinyUI(
  fluidPage(

    rbokehOutput(outputId = "myChart", width = "400px", height = "400px"),
    textInput(inputId="myText",label="Input Text please."),
    textInput(inputId="selection_info",label="Selection info"),

    textOutput("selection_summary")
  )
)

shinyApp(server = server, ui = ui)

所以上面的 output$selection_summary 确实提供了 tool_box_select 给出的某种类型的信息。我只是不确定它是什么。我最终想生成一个 table 或关于与该工具编辑的点 select 关联的属性的汇总统计数据。但我不知道如何弄清楚回调功能正在捕获什么。我在文档中找不到更多详细信息,除非我遗漏了什么。

谢谢。

现在我找到了完美的答案,取自这段代码: https://www.snip2code.com/Snippet/1040781/rbokeh-shiny-callback-experiment

shinyCallback 所做的只是将相应事件的信息 存储在 input 对象 中。然后,很容易通过 reactive() 收听 input 变化并处理结果。您还可以将自己的客户端 javascript 回调与 custom_callback() 一起用于更薄的服务器端:

tool_hover(callback = custom_callback(code = "$('#hover_info').val(cb_data.geometry.x+'|'+cb_data.geometry.y);"), "points") %>%
tool_box_select(callback = custom_callback(code = "$('#selection_info').val(points_data.changed.selected['1d'].indices.join());","points"), "points")

本手册也可能对您有所帮助:http://hafen.github.io/rbokeh/rd.html#callback-interactivity

library("dplyr")
library("rbokeh")
library("shiny")

data(mtcars)

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

  output$myChart <- renderRbokeh({
    print(paste0("now rendering!"," ",date()))
    input$myText # make it dependent on some input
    figure() %>%
      ly_points(x = mtcars$cyl, y = mtcars$hp, data = mtcars,
                hover = c(mtcars$cyl, mtcars$hp), lname = "points") %>%
      tool_hover(shiny_callback("hover_info"), "points") %>%
      tool_tap(shiny_callback("tap_info"), "points") %>%
      tool_box_select(shiny_callback("selection_info"), "points")
  })

  output$hover_text <- reactive({
    hi <- input$hover_info
    if(!is.null(hi)) {
      paste0("index: ", hi$index[["1d"]]$indices, ", x: ",
             hi$geom$sx, ", y:", hi$geom$sy)
    } else {
      "waiting for hover event (hover over plot or points on plot to trigger)..."
    }
  })

  output$tap_text <- reactive({
    ti <- input$tap_info
    if(!is.null(ti)) {
      paste("index:", paste(ti, collapse = ", "))
    } else {
      "waiting for tap/click event (click point(s) to trigger)..."
    }
  })

  output$selection_text <- reactive({
    si <- input$selection_info
    if(!is.null(si)) {
      paste("index:", paste(si, collapse = ", "))
    } else {
      "waiting for selection event (click point(s) or use box select tool to trigger)..."
    }
  })


})

ui <- shinyUI(
  fluidPage(
    rbokehOutput(outputId = "myChart", width = "400px", height = "400px"),
    strong("hover event:"),
    textOutput("hover_text"),

    strong("triggered by tap/click:"),
    htmlOutput("tap_text"),

    strong("index of selected triggered by any selection:"),
    textOutput("selection_text")
  )
)

shinyApp(server = server, ui = ui)

非常感谢!我也想通了。回调本质上是获取与数据集的行号相对应的所选点的索引号。因此,要获得所选点的更多信息,只需将索引号与数据集的行号匹配,您就可以从那里 运行 使用它。下面是一个粗略的版本。

library("dplyr")
library("rbokeh")
library("shiny")

attach(mtcars)
d <- mtcars
server <- shinyServer(function(input,output){

  output$myChart <- renderRbokeh({
    print(paste0("now rendering!"," ",date()))
    input$myText
    figure() %>%
      ly_points(x = mtcars$cyl, y = mtcars$hp, data = mtcars,
                hover = c(mtcars$cyl, mtcars$hp), lname = "points") %>%
      tool_hover(callback = shiny_callback(id = "hover_info"), "points") %>%
      tool_tap(callback = shiny_callback(id = "tap_info"), "points") %>%
      tool_box_select(callback = shiny_callback(id = "selection_info"), "points")
  })

  output$selection_table <- renderTable({
    index <- input$selection_info
    index <- as.data.frame(index)
    d$index<-seq.int(nrow(d))

    d <- semi_join(d, index, by="index")
    d
  })

  output$summary_stats_selected <- renderPrint({
    index <- input$selection_info
    index <- as.data.frame(index)
    d$index<-seq.int(nrow(d))

    d <- semi_join(d, index, by="index")
    summary(d)
  })

})

ui <- shinyUI(
  fluidPage(

    rbokehOutput(outputId = "myChart", width = "400px", height = "400px"),
    textInput(inputId="myText",label="Input Text please."),
    textInput(inputId="selection_info",label="Selection info"),

    tableOutput("selection_table"),
    verbatimTextOutput("summary_stats_selected")
  )
)

shinyApp(server = server, ui = ui)

首先,我将工具选择的索引转换为数据框。然后我根据行号为数据集创建了一个 "index" 变量。然后我根据匹配 "index" 数字在两个数据帧之间实现了 semi_join 。从那里你有一个选定点的数据框及其相应的属性。

非常感谢大家。

更新:

过了一会儿,我意识到索引已关闭。回调引用的索引和实际数据集的索引已关闭。在与 rbokeh 的创建者交谈后,问题是回调功能的索引是从零开始的。所以我在索引中加了 1 就解决了问题。