【问题标题】:Return dataframe of click coordinates返回点击坐标的数据框
【发布时间】:2020-04-18 20:04:21
【问题描述】:

我想简单地单击我的绘图,并让它返回一个 data.frame 到 R 列出单击的坐标。我试过这个:

library(shiny)
ui <- basicPage(
  plotOutput("plot1", click = "plot_click"),
  verbatimTextOutput("info")
)
server <- function(input, output) {
  output$plot1 <- renderPlot({
    plot(mtcars$wt, mtcars$mpg)
  })

  output$info <- renderText({
    paste0("x=", input$plot_click$x, "\ny=", input$plot_click$y)
  })
  output$temp <- renderTable({ 
    temp <- data.frame(input$plot_click$x,input$plot_click$y)
    return(temp)
  })
}
shinyApp(ui, server)

但 'temp' 永远不会返回到 R 环境。我究竟做错了什么?我该怎么做?

sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 19.10

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.8.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.8.0

locale:
 [1] LC_CTYPE=en_IL.UTF-8       LC_NUMERIC=C               LC_TIME=en_IL.UTF-8        LC_COLLATE=en_IL.UTF-8     LC_MONETARY=en_IL.UTF-8    LC_MESSAGES=en_IL.UTF-8   
 [7] LC_PAPER=en_IL.UTF-8       LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_IL.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] shiny_1.4.0.2

loaded via a namespace (and not attached):
 [1] compiler_3.6.1  fastmap_1.0.1   magrittr_1.5    R6_2.4.1        promises_1.1.0  later_1.0.0     htmltools_0.4.0 tools_3.6.1     Rcpp_1.0.3      jsonlite_1.6.1  digest_0.6.25  
[12] xtable_1.8-4    httpuv_1.5.2    mime_0.9        rlang_0.4.5   

【问题讨论】:

  • 这可能是您的 Shiny 版本的问题。它看起来像我预期的那样工作。你能提供截图和你的会话信息吗?
  • @EliMiller 更新了会话信息。返回到 R 环境的只有 'ui' 和 'server'。
  • 我在这里没有看到任何重大差异。你能更清楚你的期望吗?您是否试图从渲染表函数中获取来自 temp 的 data.frame 并在其他地方使用它而不是仅显示信息?
  • @EliMiller 是的,我想将它用作 R 中的普通数据框。

标签: r shiny


【解决方案1】:

试试下面的。您应该能够使用rv$df 变量代替temp。问题是 temp 对用户是反应性的,因此需要将其放入反应性上下文中。

server <- function(input, output) {

  rv <- reactiveValues(
    df = data.frame(x=NA,y=NA)
  )

  observeEvent(input$plot_click, {
    rv$df$x <- input$plot_click$x
    rv$df$y <- input$plot_click$y
  })

  output$plot1 <- renderPlot({
    plot(mtcars$wt, mtcars$mpg)
  })

  output$info <- renderText({
    paste0("x=", input$plot_click$x, "\ny=", input$plot_click$y)
  })
  output$temp <- renderTable({ 
    rv$df
  })
}

【讨论】:

  • 由于某种原因,我仍然无法让 rv$df 返回 R 环境。
猜你喜欢
  • 1970-01-01
  • 2012-02-11
  • 2021-05-31
  • 2021-08-25
  • 1970-01-01
  • 1970-01-01
  • 2013-03-20
  • 2019-07-18
  • 2020-10-21
相关资源
最近更新 更多