0

我正在尝试使用 rbokeh 中的 tool_lasso_select 功能。我希望用户使用该工具在散点图中选择一组点,并在闪亮的 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 给出的某种类型的信息。我只是不确定它是什么。我最终希望生成与工具选择的点相关联的属性的表格或汇总统计信息。但我不知道如何弄清楚回调功能正在捕获什么。我无法在文档中找到更多详细信息,除非我遗漏了什么。

谢谢。

4

2 回答 2

1

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

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)

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

非常感谢你们。

更新:

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

于 2016-09-03T13:30:58.707 回答
0

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

shinyCallback所做的只是将相应事件的信息存储inputobject。然后,很容易通过监听input更改reactive()并处理结果。您还可以使用自己的客户端 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)
于 2016-09-03T11:38:58.067 回答