R 使用模块时防止plotOutput重置的单击事件

R 使用模块时防止plotOutput重置的单击事件,r,shiny,R,Shiny,对于我的应用程序,我使用了一个具有不同输入数的模块。在我的主应用程序中,我现在要创建一个交互式绘图。我将一个click事件(click=“onClick”)处理程序添加到plotOutput。当我点击一个点时,input$onClick会被更新,但随后会变成NULL 您可以在应用程序中进行尝试:如果单击左侧图形中的一个点,则会打印input$onClick的值,但随后会变为NULL 这与模块有关,因为如果单击右侧图形中的某个点,则信息是持久的 因此,似乎在客户端和服务器之间存在某种通信,在使用模

对于我的
应用程序,我使用了一个具有不同输入数的模块。在我的主应用程序中,我现在要创建一个交互式绘图。我将一个
click
事件(
click=“onClick”
)处理程序添加到
plotOutput
。当我点击一个点时,
input$onClick
会被更新,但随后会变成
NULL

您可以在应用程序中进行尝试:如果单击左侧图形中的一个点,则会打印
input$onClick
的值,但随后会变为
NULL

这与模块有关,因为如果单击右侧图形中的某个点,则信息是持久的

因此,似乎在
客户端
服务器
之间存在某种通信,在使用模块时,
输入$onclick
无效。我能做些什么吗

代码

library(shiny)
library(plyr)
library(ggplot2)

testUI <- function(id) {
   ns <- NS(id)
   uiOutput(ns("placeholder"))
}

test <- function(input, output, session, n) {
    output$placeholder <- renderUI({
        do.call(tagList, llply(1:n(), function(i)
                   numericInput(session$ns(paste("n", i, sep = ".")), 
                session$ns(paste("n", i, sep = ".")), sample(0:100, 1), 0, 100)))
    })
    getData <- reactive(unlist(reactiveValuesToList(input)[1:n()]))
    list(getData = getData)
}

ui <- fluidPage(
    flowLayout(
        numericInput("n", "Number of Elements", 3, 1, 10),
        testUI("x"),
        testUI("y")),
    flowLayout(
        plotOutput("plot", click = "onClick"),
        plotOutput("plot2", click = "onClick2")),
    verbatimTextOutput("debug")
)

server <- function(input, output, session) {
    getN <- reactive(input$n)
    handler <- list(x = callModule(test, "x", getN),
                    y = callModule(test, "y", getN))
    output$plot <- renderPlot({
        req(handler$x$getData(), handler$y$getData())
        dat <- data.frame(x = handler$x$getData(),
                          y = handler$y$getData())
        qplot(x, y, data = dat)})
    output$plot2 <- renderPlot(qplot(mpg, cyl, data = mtcars))
    output$debug <- renderPrint(list(input$onClick, input$onClick2))
}

runApp(shinyApp(ui, server))
库(闪亮)
图书馆(plyr)
图书馆(GG2)

testUI我重写了服务器,尝试跟踪问题。首先,我将强调我怀疑的问题,然后我将编写一个替代解决方案

第一:可能的问题


我认为
output$plot
会被渲染两次,如果你把
print(“这里”)
放在
output$plot里面,谢谢你的回答!它启发了我,我可以解决这个问题。仅供参考:我更改了
getData
,并删除了
reactiveValuesToList
,正如您所指出的,这是导致此问题的原因。我的
getData
现在看起来是这样的:
getData@thothal很高兴它有帮助。我用来跟踪被动值的行为的一个方面是。有时候可能会有帮助。是的,我试着用它,但不确定我是否完全理解它的输出。你手头有这方面的教程吗?(除此之外,在我的实际应用程序中,我有许多依赖项,因此它开始变得非常混乱)+1在任何情况下:)@thothal我认为,如果人们理解和中解释的流程,那么就更容易掌握反应日志可视化器的输出。
library(shiny)
library(plyr)
library(ggplot2)

testUI <- function(id) {

  ns <- NS(id)

  uiOutput(ns("placeholder"))
}

test <- function(input, output, session, n) {

  output$placeholder <- renderUI({
    do.call(tagList,
            llply(1:n(), function(i)
              numericInput(session$ns(paste("n", i, sep = ".")), 
                           session$ns(paste("n", i, sep = ".")), sample(0:100, 1), 0, 100)))
  })

  getData <- reactive(unlist(reactiveValuesToList(input)[1:n()]))

  ## TEST: this will work ----------
  # getData <- reactive(1:n())  

  list(getData = getData)
}

ui <- fluidPage(
  flowLayout(
    numericInput("n", "Number of Elements", 3, 1, 10),
    testUI("x"),
    testUI("y")),
  flowLayout(
    plotOutput("plot", click = "onClick"),
    plotOutput("plot2", click = "onClick2")),
  verbatimTextOutput("debug")
)

server <- function(input, output, session) {


  # handler <- list(x = callModule(test, "x", getN),
  #                 y = callModule(test, "y", getN))
  # 
  # output$plot <- renderPlot({
  #   req(handler$x$getData(), handler$y$getData())
  #   dat <- data.frame(x = handler$x$getData(),
  #                     y = handler$y$getData())
  #   qplot(x, y, data = dat)})


  getN <- reactive(input$n)

  ## call modules -------------------
  xx <- callModule(test, "x", getN)
  yy <- callModule(test, "y", getN)

  ## data to be plotted in left plot
  dat <- reactive({
    data.frame(x = xx$getData(),
               y = yy$getData())
  })

  ## left plot ------------------
  output$plot <- renderPlot({
    req(xx$getData(),yy$getData())
    print("here")
    qplot(x, y, data = dat())
  })

  ## right plot ------------------
  output$plot2 <- renderPlot({
    qplot(mpg, cyl, data = mtcars)
  })

  output$debug <- renderPrint(c(input$onClick$x,input$onClick2$y))
  # output$debug <- renderPrint(dat())

}

shinyApp(ui = ui, server = server)
library(shiny)
library(plyr)
library(ggplot2)

testUI <- function(id) {

  ns <- NS(id)

  uiOutput(ns("placeholder"))
}

test <- function(input, output, session, n) {

  output$placeholder <- renderUI({
    do.call(tagList,
            llply(1:n(), function(i)
              numericInput(session$ns(paste("n", i, sep = ".")), 
                           session$ns(paste("n", i, sep = ".")), sample(0:100, 1), 0, 100)))
  })

}

ui <- fluidPage(
  flowLayout(
    numericInput("n", "Number of Elements", 3, 1, 10),
    testUI("x"),
    testUI("y")),
  verbatimTextOutput("debug"),
  flowLayout(
    plotOutput("plot", click = "onClick"),
    plotOutput("plot2", click = "onClick2"))

)

server <- function(input, output, session) {

  getN <- reactive(input$n)

   ## call modules -------------------
   callModule(test, "x", getN)
   callModule(test, "y", getN)


   ## get coordinates fromnumeric inputs ----------
   x_coord <- reactive(sapply((1:input$n),function(x) input[[paste0("x-n.",x)]]))
   y_coord <- reactive(sapply((1:input$n),function(x) input[[paste0("y-n.",x)]]))

   ## create data frame
   dat <- reactive({
     req(input[[paste0("y-n.",input$n)]]) # could be changed 
     data.frame(x = x_coord(),
                y = y_coord())
   })

  ## render left plot ------------------
  output$plot <- renderPlot({
    req(input[[paste0("y-n.",input$n)]]) # could be changed 
    qplot(x, y, data = dat())
  })

  ## render right plot ------------------
  output$plot2 <- renderPlot({
    qplot(mpg, cyl, data = mtcars)
  })

  ## cat coordinates of clicked points ---------------
  output$debug <- renderPrint(c(input$onClick$x,input$onClick$y))
}

shinyApp(ui = ui, server = server)