【问题标题】:extract and store coordinates in the dataframe using shiny使用闪亮在数据框中提取和存储坐标
【发布时间】:2022-01-15 23:03:14
【问题描述】:

我可以单击并从图中提取坐标。这是link。有人可以告诉我如何提取这些坐标并将其存储在 tibble 中吗?

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    一种方法可以是这样的:

    1. 创建一个reactiveVal 来存储tibble
    2. 每次选择新点时更新数据。
    library(shiny)
    library(tidyverse)
    
    ui <- basicPage(
      plotOutput("plot1", click = "plot_click",width = '1000px', height = "1000px"),
      verbatimTextOutput("info"),
      tableOutput("tibbl"),
      actionButton('save_to_global', "Save Table")
    )
    
    server <- function(input, output) {
      df <- reactiveVal(NULL)
      
      output$plot1 <- renderPlot({
        plot(mtcars$wt, mtcars$mpg)
      })
      
      output$info <- renderText({
        paste0("x=", input$plot_click$x, "\ny=", input$plot_click$y)
      })
      
      #create the table or update the values.
      observeEvent(c(input$plot_click$x, input$plot_click$y), {
        if (is.null(df())) {
          df(tibble(x = input$plot_click$x, y = input$plot_click$y))
        } else {
          df(df() %>%
               add_row(x = input$plot_click$x, y = input$plot_click$y))
        }
      })
      
      output$tibbl <- renderTable({
        df()
      })
      
      
      observeEvent(input$save_to_global, {
        assign('df_coordinates', df(), envir = .GlobalEnv)
      })
    }
    
    
    shinyApp(ui, server)
    
    

    【讨论】:

    • 谢谢,但我在我的环境中找不到输出(tibble)
    • 要保存到应用外的全局环境吗?
    • 是的,我想提取几个坐标并存储在全局环境中并做一些后期处理。
    • 另外,能否请您告诉我是否有办法查看更大的情节以便能够更准确地点击
    • @SiH 我添加了一个动作按钮,将 tibble 发送到全局环境。由于 R 在运行闪亮的应用程序时很忙,因此一旦应用程序关闭,数据将在全局环境中可用。关于绘图的大小,您可以使用plotOutput 函数中的高度和宽度参数进行控制。
    猜你喜欢
    • 1970-01-01
    • 2021-10-31
    • 2019-07-10
    • 1970-01-01
    • 2021-06-21
    • 1970-01-01
    • 1970-01-01
    • 2015-05-22
    • 1970-01-01
    相关资源
    最近更新 更多