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

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

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

问题

  1. #模块
  2. plUI <- function(id) {
  3. tagList(
  4. selectInput(NS(id, "var"), "变量", choices = names(mtcars)),
  5. numericInput(NS(id, "bins"), "柱数", value = 10, min = 1),
  6. plotOutput(NS(id, "hist"), click = "plot_click"),
  7. verbatimTextOutput("info")
  8. )
  9. }
  10. plServer <- function(id) {
  11. moduleServer(id, function(input, output, session) {
  12. data <- reactive(mtcars[[input$var]])
  13. output$hist <- renderPlot({
  14. plot(data(), breaks = input$bins, main = input$var)
  15. }, res = 96)
  16. output$info <- renderPrint({
  17. req(input$plot_click)
  18. x <- round(input$plot_click$x, 2)
  19. y <- round(input$plot_click$y, 2)
  20. cat("[", x, ", ", y, "]", sep = "")
  21. })
  22. })
  23. }
  24. #------------------------------用户界面(UI)-------------------------------------
  25. ui <- fluidPage(
  26. plUI("p1")
  27. )
  28. #------------------------------服务器端(Server)-------------------------------------
  29. server <- function(input, output, session) {
  30. plServer("p1")
  31. }
  32. 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

  1. #MODULE
  2. plUI &lt;- function(id) {
  3. tagList(
  4. selectInput(NS(id, &quot;var&quot;), &quot;Variable&quot;, choices = names(mtcars)),
  5. numericInput(NS(id, &quot;bins&quot;), &quot;bins&quot;, value = 10, min = 1),
  6. plotOutput(NS(id, &quot;hist&quot;), click = &quot;plot_click&quot;),
  7. verbatimTextOutput(&quot;info&quot;)
  8. )
  9. }
  10. plServer &lt;- function(id) {
  11. moduleServer(id, function(input, output, session) {
  12. data &lt;- reactive(mtcars[[input$var]])
  13. output$hist &lt;- renderPlot({
  14. plot(data(), breaks = input$bins, main = input$var)
  15. }, res = 96)
  16. output$info &lt;- renderPrint({
  17. req(input$plot_click)
  18. x &lt;- round(input$plot_click$x, 2)
  19. y &lt;- round(input$plot_click$y, 2)
  20. cat(&quot;[&quot;, x, &quot;, &quot;, y, &quot;]&quot;, sep = &quot;&quot;)
  21. })
  22. })
  23. }
  24. #------------------------------UI-------------------------------------
  25. ui &lt;- fluidPage(
  26. ui &lt;- fluidPage(
  27. plUI(&quot;p1&quot;),
  28. )
  29. )
  30. #------------------------------SERVER-------------------------------------
  31. server &lt;- function(input, output, session) {
  32. plServer(&quot;p1&quot;)
  33. }
  34. shinyApp(ui, server)

答案1

得分: 2

  • 替代每个id的NS(id, "var"),你可以使用ns <- NS(id),然后使用ns("var")等等。
  • plot_click也是一个id,所以也要用ns包装起来。
  • 你忘记在verbatimTextOutput("info")中使用NS
  • 主要的ui中有两个fluidPage,这可能是错误的,所以已将其移除。
  1. library(shiny)
  2. plUI <- function(id) {
  3. ns <- NS(id)
  4. tagList(
  5. selectInput(ns("var"), "Variable", choices = names(mtcars)),
  6. numericInput(ns("bins"), "bins", value = 10, min = 1),
  7. plotOutput(ns("hist"), click = ns("plot_click")),
  8. verbatimTextOutput(ns("info"))
  9. )
  10. }
  11. plServer <- function(id) {
  12. moduleServer(id, function(input, output, session) {
  13. ns <- session$ns
  14. data <- reactive(mtcars[[input$var]])
  15. output$hist <- renderPlot({
  16. plot(data(), breaks = input$bins, main = input$var)
  17. }, res = 96)
  18. output$info <- renderPrint({
  19. req(input$plot_click)
  20. x <- round(input$plot_click$x, 2)
  21. y <- round(input$plot_click$y, 2)
  22. cat("[", x, ", ", y, "]", sep = "")
  23. })
  24. })
  25. }
  26. #------------------------------UI-------------------------------------
  27. ui <- fluidPage(
  28. plUI("p1")
  29. )
  30. #------------------------------SERVER-------------------------------------
  31. server <- function(input, output, session) {
  32. plServer("p1")
  33. }
  34. shinyApp(ui, server)

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

  1. <details>
  2. <summary>英文:</summary>
  3. - 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.
  4. - `plot_click` is also an id so wrap it in `ns` as well.
  5. - You had forgotten `NS` in `verbatimTextOutput(&quot;info&quot;)`.
  6. - 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

  1. data &lt;- reactive(mtcars[[input$var]])
  2. output$hist &lt;- renderPlot({
  3. plot(data(), breaks = input$bins, main = input$var)
  4. }, res = 96)
  5. output$info &lt;- renderPrint({
  6. req(input$plot_click)
  7. x &lt;- round(input$plot_click$x, 2)
  8. y &lt;- round(input$plot_click$y, 2)
  9. cat(&quot;[&quot;, x, &quot;, &quot;, y, &quot;]&quot;, sep = &quot;&quot;)
  10. })

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

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

}
shinyApp(ui, server)

  1. [![enter image description here][1]][1]
  2. [1]: https://i.stack.imgur.com/PN78R.gif
  3. </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:

确定