简体   繁体   English

rbokeh中的Shiny_callback捕获了什么?

[英]What does shiny_callback capture in rbokeh?

I'm trying to use the tool_lasso_select feature in rbokeh. 我正在尝试在rbokeh中使用tool_lasso_select功能。 What I would like is for the user to use the tool to select a group of points in a scatterplot and summary statistics of the group of points selected would be created as a result in the next panel of a shiny tabBox. 我想让用户使用该工具在散点图中选择一组点,然后将在闪亮的tabBox的下一个面板中创建所选点组的摘要统计信息。 My primary question is: 我的主要问题是:

What exactly is captured by the shiny_callback option? Shiny_callback选项究竟捕获了什么?

This is an example of the type of figure I'm creating: 这是我正在创建的图形类型的示例:

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

What exactly are the contents placed in the "selection_info" id and how could I extract that information? “ selection_info” ID中放置的内容到底是什么?如何提取该信息? I'm just not sure what the next block of code I could write that would take the information captured by the lasso_tool. 我只是不确定接下来可以编写什么代码来获取lasso_tool捕获的信息。 The most I can find in the documentation is the example used but output seems to only be an index number. 我在文档中能找到的最多是使用的示例,但输出似乎只是一个索引号。

UPDATE: 更新:

Thanks for adding a reproducible example. 感谢您添加可复制的示例。 My apologies. 我很抱歉。 I've added a little more to it below: 我在下面添加了一些内容:

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)

So the above output$selection_summary does provide some type of info that is given by the tool_box_select. 因此,上面的output$selection_summary确实提供了tool_box_select提供的某种类型的信息。 I'm just not sure what it is. 我只是不确定那是什么。 I'd eventually like to produce a table or summary statistics on the attributes associated with the points selected by the tool. 我最终希望针对与该工具选择的点相关的属性生成表格或摘要统计信息。 But I don't know how to figure out what it is the callback feature is capturing. 但是我不知道如何弄清楚回调功能正在捕获什么。 I couldn't find anymore details in the documentation, unless I've missed something. 除非我有所遗漏,否则在文档中找不到更多详细信息。

Thanks. 谢谢。

Thanks a lot! 非常感谢! I also figured it out. 我也想通了。 The callback essentially grabs the index number of the point selected which corresponds to the row number of the dataset. 回调实质上获取与数据集的行号相对应的所选点的索引号。 So to get more information of the selected points, just match up the index number with the row number of the dataset and you can run with it from there. 因此,要获取有关所选点的更多信息,只需将索引号与数据集的行号匹配即可,然后就可以从那里运行它。 Below is a rough version of that. 下面是它的粗略版本。

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)

First, I turned the indices selected by the tool into a dataframe. 首先,我将工具选择的索引转换为数据框。 Then I created an "index" variable for the dataset based on its row number. 然后我根据数据集的行号为数据集创建了一个“索引”变量。 Then I implemented a semi_join between the two dataframes based on matching the "index" numbers. 然后,我根据匹配的“索引”数字在两个数据帧之间实现了semi_join。 And from there you have a dataframe of the selected points with their corresponding attributes. 从那里,您将获得一个包含所选点及其相应属性的数据框。

Thanks a lot guys. 非常感谢。

UPDATE: 更新:

After a while, I realized the index was off. 一段时间后,我意识到索引已关闭。 The index the callback refers to and the actual dataset's index was off. 回调引用的索引,并且实际数据集的索引已关闭。 After speaking with the creator of rbokeh, the problem was that the callback feature's index was zero-based. 与rbokeh的创建者交谈后,问题在于回调功能的索引从零开始。 So I added 1 to the index and that solved the problem. 所以我在索引上加了1,就解决了这个问题。

Now I found the perfect answer, taken from this piece of code here: https://www.snip2code.com/Snippet/1040781/rbokeh-shiny-callback-experiment 现在,我找到了一个完美的答案,摘自以下这段代码: https : //www.snip2code.com/Snippet/1040781/rbokeh-shiny-callback-experiment

What shinyCallback does is simply store the information of the respective event inside the input object . shinyCallback所做的只是将相应事件的信息存储input对象内部 Then, it is easy to listen to input changes via reactive() and work with the results. 然后,很容易通过reactive()监听input更改并处理结果。 You could also use your own client-side javascript callback with custom_callback() for a thinner server side: 您还可以将自己的客户端JavaScript回调与custom_callback() ,以实现服务器端的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")

This manual might also help you: http://hafen.github.io/rbokeh/rd.html#callback-interactivity 本手册还可以为您提供帮助: 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM