0

我想开发一个用户可以在图像shiny上绘制多边形的应用程序。raster用户完成多边形绘制后,我希望应用程序向他们显示所选像素的表格。

例如,terra提供一个draw可以用作draw("polygon"). 但是,我无法让它与我的shiny应用程序一起使用。

该应用程序的基本思想如下(我已用 注释了有问题的部分#):

library(terra)
library(shiny)

r = rast(system.file("ex/elev.tif", package="terra"))

ui = fluidPage(
        plotOutput("map"),
        tableOutput("chosen_pixels")
)

server = function(input, output, session) {
        output$map = renderPlot({
                plot(r)
                # draw("polygon") # I comment it otherwise the app does not run
        })
        
        # output$chosen_pixels = renderPlot({
                # here I want to write code that shows a table of chosen pixels
        #})
}

shinyApp(ui, server)
4

1 回答 1

1
library(shiny)
library(tidyverse)

ui <- basicPage(
  plotOutput("plot1", click = "plot_click"),
  tableOutput("table"),
  textInput("polygon_name", label = "Polygon name", value = "polygon 1")
)

server <- function(input, output) {
  coords <- reactiveVal(value = tibble(x = numeric(), y = numeric(), name = character()))

  observeEvent(input$plot_click, {
    add_row(coords(),
      x = isolate(input$plot_click$x),
      y = isolate(input$plot_click$y),
      name = isolate(input$polygon_name)
    ) %>% coords()
  })

  output$plot1 <- renderPlot({
    plot(r)
    
    coords() %>%
      nest(-name) %>%
      deframe() %>%
      map(~ polygon(.x$x, .x$y))
  })

  output$table <- renderTable(coords())
}

shinyApp(ui, server)

在此处输入图像描述

于 2021-11-09T09:01:27.887 回答