1. <small id='4mKsE'></small><noframes id='4mKsE'>

      <i id='4mKsE'><tr id='4mKsE'><dt id='4mKsE'><q id='4mKsE'><span id='4mKsE'><b id='4mKsE'><form id='4mKsE'><ins id='4mKsE'></ins><ul id='4mKsE'></ul><sub id='4mKsE'></sub></form><legend id='4mKsE'></legend><bdo id='4mKsE'><pre id='4mKsE'><center id='4mKsE'></center></pre></bdo></b><th id='4mKsE'></th></span></q></dt></tr></i><div id='4mKsE'><tfoot id='4mKsE'></tfoot><dl id='4mKsE'><fieldset id='4mKsE'></fieldset></dl></div>
      • <bdo id='4mKsE'></bdo><ul id='4mKsE'></ul>

        <tfoot id='4mKsE'></tfoot>
      1. <legend id='4mKsE'><style id='4mKsE'><dir id='4mKsE'><q id='4mKsE'></q></dir></style></legend>

        R plotly:如何通过带有多个图的图例单击来观察跟踪是隐藏还是显示

        R plotly: how to observe whether a trace is hidden or shown through legend clicks with multiple plots(R plotly:如何通过带有多个图的图例单击来观察跟踪是隐藏还是显示)
        <tfoot id='1mqLo'></tfoot>

              <tbody id='1mqLo'></tbody>
          1. <legend id='1mqLo'><style id='1mqLo'><dir id='1mqLo'><q id='1mqLo'></q></dir></style></legend>

            <small id='1mqLo'></small><noframes id='1mqLo'>

          2. <i id='1mqLo'><tr id='1mqLo'><dt id='1mqLo'><q id='1mqLo'><span id='1mqLo'><b id='1mqLo'><form id='1mqLo'><ins id='1mqLo'></ins><ul id='1mqLo'></ul><sub id='1mqLo'></sub></form><legend id='1mqLo'></legend><bdo id='1mqLo'><pre id='1mqLo'><center id='1mqLo'></center></pre></bdo></b><th id='1mqLo'></th></span></q></dt></tr></i><div id='1mqLo'><tfoot id='1mqLo'></tfoot><dl id='1mqLo'><fieldset id='1mqLo'></fieldset></dl></div>
              <bdo id='1mqLo'></bdo><ul id='1mqLo'></ul>

                  本文介绍了R plotly:如何通过带有多个图的图例单击来观察跟踪是隐藏还是显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我试图通过在 plotly 的交互式图例中取消选择它们来确定用户从散点图中隐藏了哪些跟踪.

                  我已阅读此

                  <小时>

                  编辑

                  当双击图例项时,以前的解决方案存在问题.这是一个更好的解决方案:

                  图书馆(情节)图书馆(闪亮)图书馆(htmlwidgets)js <- c(函数(el,x){"," var d3 = Plotly.d3;"," el.on('plotly_restyle', 函数(evtData) {"," 变量输出 = {};"," d3.select('g.legend').selectAll('.traces').each(function(){"," var trace = d3.select(this)[0][0].__data__[0].trace;"," out[trace.name] = trace.visible;"," });"," Shiny.setInputValue('traces', out);"," });","}")ui <-流体页面(情节输出(情节"),逐字文本输出(图例"))服务器 <- 功能(输入,输出,会话){输出$plot <- renderPlotly({p <- plot_ly()for(c 中的名称(drat",wt",qsec")){p = add_markers(p, x = as.numeric(mtcars$cyl), y = as.numeric(mtcars[[name]]), name = name)}p %>% onRender(js)})output$legendItem <- renderPrint({输入$跟踪})}闪亮的应用程序(用户界面,服务器)

                  <小时>

                  如果您有多个绘图,请在图例选择器中添加绘图 id,并使用函数生成 JavaScript 代码:

                  js <- function(i) {C(函数(el,x){"," var id = el.getAttribute('id');"," var d3 = Plotly.d3;"," el.on('plotly_restyle', 函数(evtData) {"," 变量输出 = {};"," d3.select('#' + id + 'g.legend').selectAll('.traces').each(function(){"," var trace = d3.select(this)[0][0].__data__[0].trace;"," out[trace.name] = trace.visible;"," });",sprintf(" Shiny.setInputValue('traces%d', out);", i)," });","}")}

                  然后执行 p1 %>% onRender(js(1)), p2 %>% onRender(js(2)), ..., 和你在 input$traces1, input$traces2, ....

                  中获得有关跟踪可见性的信息

                  另一种方法是在 JavaScript 函数的第三个参数中传递所需的名称,借助 onRenderdata 参数:

                  js <- c("函数(el, x, inputName){"," var id = el.getAttribute('id');"," var d3 = Plotly.d3;"," el.on('plotly_restyle', 函数(evtData) {"," 变量输出 = {};"," d3.select('#' + id + 'g.legend').selectAll('.traces').each(function(){"," var trace = d3.select(this)[0][0].__data__[0].trace;"," out[trace.name] = trace.visible;"," });"," Shiny.setInputValue(inputName, out);"," });","}")p1 %>% onRender(js, data = "tracesPlot1")p2 %>% onRender(js, data = "tracesPlot2")

                  I am trying to figure out which traces the user hides from a scatter plot by means of deselecting them in the interactive legend of plotly.

                  I have read this SO post, and the similar questions linked in the comments below and this brought me closer to the solution

                  The current solution is only doing partially what I need. Two things I am looking for to improve it is: - how to see which plot's legend is clicked (looking at source 'id' ?) - I can now see that a legend entry is clicked, but I need to be able to see whether it is clicked 'ON'(show trace) or 'OFF'

                  The output i'm looking for would look something like this: input$trace_plot1 : which is then a list of all traces that are off and which are on, or a single trace nr on every click but that tells whether that specific trace is now "ON" or "OFF"

                  The goal for me is to link the visual hiding and showing to an overview of all my groups in the data where the user can now give them new names, colors, and choose to keep or drop the group with a button that has a T/F state switch behind it. I would like to link that T/F state of the buttons to the 'show'/'hidden' of traces from a specific plot (since I have 5 copies of these plots in my app showing the data in different stages of the analysis process.

                  Here is my attempt that does not react to the legend somehow, only to zooom:

                  library(plotly)
                  library(shiny)
                  library(htmlwidgets)
                  
                  js <- c(
                    "function(el, x){",
                    "  el.on('plotly_legendclick', function(evtData) {",
                    "    Shiny.setInputValue('trace', evtData.data[evtData.curveNumber].name);",
                    "  });",
                    "}")
                  
                  iris$group <- c(rep(1,50), rep(2, 50), rep(3,50))
                  
                  ui <- fluidPage(
                    plotlyOutput("plot1"),
                    plotlyOutput("plot2"),
                    verbatimTextOutput("legendItem")
                  
                  )
                  
                  
                  server <- function(input, output){
                  
                    output$plot1 <- renderPlotly({
                      p <- plot_ly(source = 'plotly1', data = iris, x = ~Sepal.Length, y = ~Petal.Length, color = ~as.factor(group), type = 'scatter', mode = 'markers') %>%
                        layout(showlegend = TRUE)
                  
                      p %>% onRender(js)
                  
                      })
                  
                    output$plot2 <- renderPlotly({
                      p <- plot_ly(source = 'plotly2', data = iris, x = ~Sepal.Length, y = ~Petal.Length, color = ~as.factor(group), type = 'scatter', mode = 'markers') %>%
                        layout(showlegend = TRUE)
                  
                      p %>% onRender(js)
                  
                    })
                  
                    output$legendItem <- renderPrint({
                      d <- input$trace
                      if (is.null(d)) "Clicked item appear here" else d
                    })
                  
                    }
                  
                  shinyApp(ui = ui, server = server)
                  

                  EDIT: WORKING SOLUTION THANKS TO THE EXTENSIVE ANSWER from S.L.

                  library(plotly)
                  library(shiny)
                  library(htmlwidgets)
                  
                  js <- c(
                    "function(el, x, inputName){",
                    "  var id = el.getAttribute('id');",
                    "  var d3 = Plotly.d3;",
                    "  el.on('plotly_restyle', function(evtData) {",
                    "    var out = {};",
                    "    d3.select('#' + id + ' g.legend').selectAll('.traces').each(function(){",
                    "      var trace = d3.select(this)[0][0].__data__[0].trace;",
                    "      out[trace.name] = trace.visible;",
                    "    });",
                    "    Shiny.setInputValue(inputName, out);",
                    "  });",
                    "}")
                  
                  
                  ui <- fluidPage(
                    plotlyOutput("plot1"),
                    plotlyOutput("plot2"),
                    verbatimTextOutput("tracesPlot1"),
                    verbatimTextOutput("tracesPlot2")
                  )
                  
                  server <- function(input, output, session) {
                  
                  output$plot1 <- renderPlotly({
                      p1 <- plot_ly()
                      p1 <-  add_trace(p1, data = mtcars, x = ~disp, y = ~mpg, type = 'scatter', mode = 'markers', color = ~as.factor(cyl))
                      p1 %>% onRender(js, data = "tracesPlot1")    
                    })
                  
                    output$plot2 <- renderPlotly({
                      p2 <- plot_ly()
                      p2 <- add_trace(p2, data = mtcars, x = ~disp, y = ~mpg, type = 'scatter', mode = 'markers', color = ~as.factor(cyl))
                      p2 %>% onRender(js, data = "tracesPlot2")  })
                  
                  
                    output$tracesPlot1 <- renderPrint({ unlist(input$tracesPlot1)  })
                  
                    output$tracesPlot2 <- renderPrint({unlist(input$tracesPlot2)
                    })
                  
                  }
                  
                  shinyApp(ui, server)
                  

                  解决方案

                  Does it help?

                  library(plotly)
                  library(shiny)
                  library(htmlwidgets)
                  
                  js <- c(
                    "function(el, x){",
                    "  el.on('plotly_legendclick', function(evtData) {",
                    "    Shiny.setInputValue('trace', evtData.data[evtData.curveNumber].name);",
                    "  });",
                    "  el.on('plotly_restyle', function(evtData) {",
                    "    Shiny.setInputValue('visibility', evtData[0].visible);",
                    "  });",
                    "}")
                  
                  ui <- fluidPage(
                    plotlyOutput("plot"),
                    verbatimTextOutput("legendItem")
                  )
                  
                  server <- function(input, output, session) {
                  
                    output$plot <- renderPlotly({
                      p <- plot_ly()
                      for(name in c("drat", "wt", "qsec"))
                      {
                        p = add_markers(p, x = as.numeric(mtcars$cyl), y = as.numeric(mtcars[[name]]), name = name)
                      }
                      p %>% onRender(js)
                    })
                  
                    output$legendItem <- renderPrint({
                      trace <- input$trace
                      ifelse(is.null(trace), 
                             "Clicked item will appear here",
                             paste0("Clicked: ", trace, 
                                    " --- Visibility: ", input$visibility)
                      )
                    })
                  }
                  
                  shinyApp(ui, server)
                  


                  EDIT

                  There's an issue with the previous solution when one double-clicks on a legend item. Here is a better solution:

                  library(plotly)
                  library(shiny)
                  library(htmlwidgets)
                  
                  js <- c(
                    "function(el, x){",
                    "  var d3 = Plotly.d3;",
                    "  el.on('plotly_restyle', function(evtData) {",
                    "    var out = {};",
                    "    d3.select('g.legend').selectAll('.traces').each(function(){",
                    "      var trace = d3.select(this)[0][0].__data__[0].trace;",
                    "      out[trace.name] = trace.visible;",
                    "    });",
                    "    Shiny.setInputValue('traces', out);",
                    "  });",
                    "}")
                  
                  
                  ui <- fluidPage(
                    plotlyOutput("plot"),
                    verbatimTextOutput("legendItem")
                  )
                  
                  server <- function(input, output, session) {
                  
                    output$plot <- renderPlotly({
                      p <- plot_ly()
                      for(name in c("drat", "wt", "qsec"))
                      {
                        p = add_markers(p, x = as.numeric(mtcars$cyl), y = as.numeric(mtcars[[name]]), name = name)
                      }
                      p %>% onRender(js)
                    })
                  
                    output$legendItem <- renderPrint({
                      input$traces
                    })
                  }
                  
                  shinyApp(ui, server)
                  


                  If you have multiple plots, add the plot id in the legend selector, and use a function to generate the JavaScript code:

                  js <- function(i) { 
                    c(
                    "function(el, x){",
                    "  var id = el.getAttribute('id');",
                    "  var d3 = Plotly.d3;",
                    "  el.on('plotly_restyle', function(evtData) {",
                    "    var out = {};",
                    "    d3.select('#' + id + ' g.legend').selectAll('.traces').each(function(){",
                    "      var trace = d3.select(this)[0][0].__data__[0].trace;",
                    "      out[trace.name] = trace.visible;",
                    "    });",
                    sprintf("    Shiny.setInputValue('traces%d', out);", i),
                    "  });",
                    "}")
                  }
                  

                  Then do p1 %>% onRender(js(1)), p2 %>% onRender(js(2)), ..., and you get the info about the traces visibility in input$traces1, input$traces2, ....

                  Another way is to pass the desired name in the third argument of the JavaScript function, with the help of the data argument of onRender:

                  js <- c(
                    "function(el, x, inputName){",
                    "  var id = el.getAttribute('id');",
                    "  var d3 = Plotly.d3;",
                    "  el.on('plotly_restyle', function(evtData) {",
                    "    var out = {};",
                    "    d3.select('#' + id + ' g.legend').selectAll('.traces').each(function(){",
                    "      var trace = d3.select(this)[0][0].__data__[0].trace;",
                    "      out[trace.name] = trace.visible;",
                    "    });",
                    "    Shiny.setInputValue(inputName, out);",
                    "  });",
                    "}")
                  
                  
                  p1 %>% onRender(js, data = "tracesPlot1")
                  p2 %>% onRender(js, data = "tracesPlot2")
                  

                  这篇关于R plotly:如何通过带有多个图的图例单击来观察跟踪是隐藏还是显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  业务场景:使用update语句去更新数据库字段。 原因:update接收值不正确。原来代码: $query = "UPDATE student SET date = now() WHERE id = $id";$result = $mysqli-query($query2) or die($mysqli-error); // 问题出现了在这句 $data = $result-fetch_ass
                  在layui树状组件tree中,勾选问题可以通过以下方法解决: 通过tree的oncheck事件来监听勾选操作,然后根据勾选状态进行相应的处理。例如: tree.on('check', function(obj) { // 获取勾选状态 var isChecked = obj.checked; // 获取当前节点数据 var data =
                  经常用到layui的朋友都知道,layui tree默认是不能自定义图标的,那么我们要自定义的话要怎么操作呢? 首先用编辑器软件(修改时候用编辑器记得编码),打开layui.js。搜索: i class="layui-icon layui-icon-file" 改为如下代码: i class="'+ (i.icon || "l
                  在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
                  append() 方法在被选元素的结尾(仍然在内部)插入指定内容。 语法: $(selector).append( content ) var creatPrintList = function(data){ var innerHtml = ""; for(var i =0;i data.length;i++){ innerHtml +="li class='contentLi'"; innerHtml +="a href
                  问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转

                        <tbody id='wWIwO'></tbody>

                        <legend id='wWIwO'><style id='wWIwO'><dir id='wWIwO'><q id='wWIwO'></q></dir></style></legend>
                      • <i id='wWIwO'><tr id='wWIwO'><dt id='wWIwO'><q id='wWIwO'><span id='wWIwO'><b id='wWIwO'><form id='wWIwO'><ins id='wWIwO'></ins><ul id='wWIwO'></ul><sub id='wWIwO'></sub></form><legend id='wWIwO'></legend><bdo id='wWIwO'><pre id='wWIwO'><center id='wWIwO'></center></pre></bdo></b><th id='wWIwO'></th></span></q></dt></tr></i><div id='wWIwO'><tfoot id='wWIwO'></tfoot><dl id='wWIwO'><fieldset id='wWIwO'></fieldset></dl></div>

                        <tfoot id='wWIwO'></tfoot>

                            <bdo id='wWIwO'></bdo><ul id='wWIwO'></ul>

                            <small id='wWIwO'></small><noframes id='wWIwO'>