如何在Shiny模块中获取图表的点击事件

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

How to get a click event on a graph in module shiny

问题

#模块

plUI <- function(id) {
  tagList(
    selectInput(NS(id, "var"), "变量", choices = names(mtcars)),
    numericInput(NS(id, "bins"), "柱数", value = 10, min = 1),
    plotOutput(NS(id, "hist"), click = "plot_click"),
    verbatimTextOutput("info")
  )
}

plServer <- function(id) {
  moduleServer(id, function(input, output, session) {
    data <- reactive(mtcars[[input$var]])
    output$hist <- renderPlot({
      plot(data(), breaks = input$bins, main = input$var)
    }, res = 96)

    output$info <- renderPrint({
      req(input$plot_click)
      x <- round(input$plot_click$x, 2)
      y <- round(input$plot_click$y, 2)
      cat("[", x, ", ", y, "]", sep = "")
    })
    
  })
}
#------------------------------用户界面(UI)-------------------------------------
ui <- fluidPage(
  plUI("p1")
)

#------------------------------服务器端(Server)-------------------------------------
server <- function(input, output, session) {
  plServer("p1")
}

shinyApp(ui, server)
英文:

I'm new to shiny.

When I try to handle a click or brush event without using modules, everything works ok.
What should I do to make the events work inside the module?
There is simple code

#MODULE

plUI &lt;- function(id) {
  tagList(
    selectInput(NS(id, &quot;var&quot;), &quot;Variable&quot;, choices = names(mtcars)),
    numericInput(NS(id, &quot;bins&quot;), &quot;bins&quot;, value = 10, min = 1),
    plotOutput(NS(id, &quot;hist&quot;), click = &quot;plot_click&quot;),
    verbatimTextOutput(&quot;info&quot;)
  )
}

plServer &lt;- function(id) {
  moduleServer(id, function(input, output, session) {
    data &lt;- reactive(mtcars[[input$var]])
    output$hist &lt;- renderPlot({
      plot(data(), breaks = input$bins, main = input$var)
    }, res = 96)

    output$info &lt;- renderPrint({
      req(input$plot_click)
      x &lt;- round(input$plot_click$x, 2)
      y &lt;- round(input$plot_click$y, 2)
      cat(&quot;[&quot;, x, &quot;, &quot;, y, &quot;]&quot;, sep = &quot;&quot;)
    })
    
  })
}
#------------------------------UI-------------------------------------
ui &lt;- fluidPage(
  
  ui &lt;- fluidPage(
    plUI(&quot;p1&quot;),
    
  )
)

#------------------------------SERVER-------------------------------------
server &lt;- function(input, output, session) {
  
  plServer(&quot;p1&quot;)
  
}
shinyApp(ui, server)

答案1

得分: 2

  • 替代每个id的NS(id, "var"),你可以使用ns <- NS(id),然后使用ns("var")等等。
  • plot_click也是一个id,所以也要用ns包装起来。
  • 你忘记在verbatimTextOutput("info")中使用NS
  • 主要的ui中有两个fluidPage,这可能是错误的,所以已将其移除。
library(shiny)

plUI <- function(id) {
  ns <- NS(id)
  tagList(
    selectInput(ns("var"), "Variable", choices = names(mtcars)),
    numericInput(ns("bins"), "bins", value = 10, min = 1),
    plotOutput(ns("hist"), click = ns("plot_click")),
    verbatimTextOutput(ns("info"))
  )
}

plServer <- function(id) {
  moduleServer(id, function(input, output, session) {
    ns <- session$ns
    
    data <- reactive(mtcars[[input$var]])
    
    output$hist <- renderPlot({
      plot(data(), breaks = input$bins, main = input$var)
    }, res = 96)
    
    output$info <- renderPrint({
      req(input$plot_click)
      x <- round(input$plot_click$x, 2)
      y <- round(input$plot_click$y, 2)
      cat("[", x, ", ", y, "]", sep = "")
    })
  })
}

#------------------------------UI-------------------------------------

ui <- fluidPage(
    plUI("p1")
)

#------------------------------SERVER-------------------------------------

server <- function(input, output, session) {
  plServer("p1")
}

shinyApp(ui, server)

如何在Shiny模块中获取图表的点击事件


<details>
<summary>英文:</summary>

- Instead of `NS(id, &quot;var&quot;)` for each id, you can do `ns &lt;- NS(id)` and then use `ns(&quot;var&quot;)` and so on. 
- `plot_click` is also an id so wrap it in `ns` as well. 
- You had forgotten `NS` in `verbatimTextOutput(&quot;info&quot;)`. 
- The main `ui` had two `fluidPage` that probably was by mistake so removed it. 

library(shiny)

plUI <- function(id) {
ns <- NS(id)
tagList(
selectInput(ns("var"), "Variable", choices = names(mtcars)),
numericInput(ns("bins"), "bins", value = 10, min = 1),
plotOutput(ns("hist"), click = ns("plot_click")),
verbatimTextOutput(ns("info"))
)
}

plServer <- function(id) {
moduleServer(id, function(input, output, session) {
ns <- session$ns

data &lt;- reactive(mtcars[[input$var]])

output$hist &lt;- renderPlot({
  plot(data(), breaks = input$bins, main = input$var)
}, res = 96)

output$info &lt;- renderPrint({
  req(input$plot_click)
  x &lt;- round(input$plot_click$x, 2)
  y &lt;- round(input$plot_click$y, 2)
  cat(&quot;[&quot;, x, &quot;, &quot;, y, &quot;]&quot;, sep = &quot;&quot;)
})

})
}
#------------------------------UI-------------------------------------
ui <- fluidPage(
plUI("p1")
)

#------------------------------SERVER-------------------------------------
server <- function(input, output, session) {
plServer("p1")

}
shinyApp(ui, server)


[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/PN78R.gif

</details>



huangapple
  • 本文由 发表于 2023年2月19日 11:32:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497808.html
匿名

发表评论

匿名网友

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

确定