英文:
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 <- function(id) {
tagList(
selectInput(NS(id, "var"), "Variable", choices = names(mtcars)),
numericInput(NS(id, "bins"), "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(
ui <- fluidPage(
plUI("p1"),
)
)
#------------------------------SERVER-------------------------------------
server <- function(input, output, session) {
plServer("p1")
}
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)
<details>
<summary>英文:</summary>
- Instead of `NS(id, "var")` for each id, you can do `ns <- NS(id)` and then use `ns("var")` and so on.
- `plot_click` is also an id so wrap it in `ns` as well.
- You had forgotten `NS` in `verbatimTextOutput("info")`.
- 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 <- 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)
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/PN78R.gif
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论