从多次点击中获取绘图的x和y轴位置

huangapple go评论63阅读模式
英文:

Shiny: get x, y axis locations from a plot from multiple clicks

问题

以下是代码的翻译部分:

library(shiny)
ui <- basicPage(
  plotOutput("plot1",
             click = "plot_click",
             hover = "plot_hover"
             ),
  verbatimTextOutput("info")
  )

server <- function(input, output) {
  output$plot1 <- renderPlot({
    plot(mtcars$wt, mtcars$mpg)
  })
  
  output$info <- renderText({
    xy_str <- function(e) {
      if(is.null(e)) return("NULL\n")
      paste0("x=", round(e$x, 1), " y=", round(e$y, 1), "\n")
    }
  
    paste0(
      "click: ", xy_str(input$plot_click),
      "hover: ", xy_str(input$plot_hover)
    )
  })
}

shinyApp(ui, server)

希望这个翻译对您有所帮助。

英文:

The below code show how to get the x, y axis location from one click.

How can I get x, y axis locations from multiple clicks on the plot?

library(shiny)
ui &lt;- basicPage(
  plotOutput(&quot;plot1&quot;,
             click = &quot;plot_click&quot;,
             hover = &quot;plot_hover&quot;
             ),
  verbatimTextOutput(&quot;info&quot;)
  )

server &lt;- function(input, output) {
  output$plot1 &lt;- renderPlot({
    plot(mtcars$wt, mtcars$mpg)
  })
  
  output$info &lt;- renderText({
    xy_str &lt;- function(e) {
      if(is.null(e)) return(&quot;NULL\n&quot;)
      paste0(&quot;x=&quot;, round(e$x, 1), &quot; y=&quot;, round(e$y, 1), &quot;\n&quot;)
    }
  
    paste0(
      &quot;click: &quot;, xy_str(input$plot_click),
      &quot;hover: &quot;, xy_str(input$plot_hover)
    )
  })
}

shinyApp(ui, server)

答案1

得分: 2

我添加了一个反应式值 rv_click$tb 来记录点击事件。同时添加了一个重置按钮,用于重置操作。

英文:

Try this:

library(shiny)
library(dplyr)

ui &lt;- basicPage(
  plotOutput(&quot;plot1&quot;,
             click = &quot;plot_click&quot;,
             hover = &quot;plot_hover&quot;
  ),
  actionButton(&quot;reset&quot;, &quot;reset&quot;, class = &quot;btn-warning&quot;),
  verbatimTextOutput(&quot;info&quot;)
)

server &lt;- function(input, output) {
  
  rv_click &lt;- reactiveValues(tb = tibble(x = double(), y = double()))
  
  observeEvent(input$reset, {
    rv_click$tb &lt;- tibble(x = double(), y = double())
  })
  
  observeEvent(input$plot_click, {
    rv_click$tb &lt;- 
      isolate(rv_click$tb) %&gt;% 
      add_row(
        x = input$plot_click$x,
        y = input$plot_click$y
      )
  })
  
  output$plot1 &lt;- renderPlot({
    plot(mtcars$wt, mtcars$mpg)
  })
  
  output$info &lt;- renderText({
    tb_click &lt;- rv_click$tb
    if (nrow(tb_click) == 0L) return(&quot;Please click on the plot.&quot;)
    paste0(
      &quot;click: &quot;, sprintf(&quot;%02d&quot;, 1:nrow(tb_click)), &quot; &quot;,
      &quot;x: &quot;, round(tb_click$x, 1), &quot; &quot;,
      &quot;y: &quot;, round(tb_click$y, 1), &quot;\n&quot;
    )
  })
}

shinyApp(ui, server)

I added a reactive value rv_click$tb to record the click event. Also a reset button to, you know, reset.

huangapple
  • 本文由 发表于 2023年4月7日 05:06:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75953789.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定