首页 > 解决方案 > 访问在不同的 RenderPlot 中生成的数据框

问题描述

我编写了一个 Shiny 应用程序,它允许用户在栅格上选择两个点,从而使用不同的参数计算路线。

路线的可视化只是我想要发生的一个组件。我还希望能够创建路线的汇总统计数据并将其显示在不同的图中(因此路线显示在左侧,统计数据显示在右侧)。

但是,我不确定如何使该路线在另一个 Plot 中可访问。我希望其他情节可以访问的是

elevation <- data.frame(extract(dem, AtoB4))  

然后,海拔将用于创建将显示在右列中的汇总统计信息。

任何关于如何做到这一点的想法表示赞赏。还可以建议以不同的方式完全做到这一点。

可重现的例子:

用户界面

# Define UI for application that plots features of movies 
ui <- fluidPage(

  titlePanel("xx"),


  # Sidebar layout with a input and output definitions 
  fluidRow(

    # Inputs
    column(width = 2,
      p("Drag a box on the Elevation plot to generate Least Cost Paths using different number of neighbours"),
      p("Least Cost Path generated using",strong("4 neighbours"), style = "color:red"),
      p("Least Cost Path generated using",strong("8 neighbours"), style = "color:black"),
      p("Least Cost Path generated using",strong("16 neighbours"), style = "color:blue")
    ),
    # Outputs
    column(4,
      plotOutput(outputId = "mapPlot", brush = "plot_brush")
      ), 
    column(6,
           plotOutput(outputId = "stats_plots"))
    )
  )

服务器.R

library(shiny)
library(raster)
library(gdistance)
library(sp)
library(rgdal)

dem <- raster(system.file("external/maungawhau.grd", package="gdistance"))


# Define server function required to create the scatterplot

conductance_calc <- function(input_dem, neighbours) {
  altDiff <- function(x){x[2] - x[1]}
  hd <- transition(input_dem, altDiff, neighbours, symm=FALSE)
  slope <- geoCorrection(hd)
  adj <- adjacent(input_dem, cells=1:ncell(input_dem), pairs=TRUE, directions=16)
  speed <- slope
  speed[adj] <- 6 * exp(-3.5 * abs(slope[adj] + 0.05))
  Conductance <- geoCorrection(speed)
  return(Conductance)
}

server <- function(input, output) {

  output$mapPlot <- renderPlot( {

    plot(dem, axes = FALSE, legend = FALSE)
    Conductance <-conductance_calc(dem, 16)

        if(is.null(input$plot_brush)) return("NULL\n")
      A <- c(as.numeric(unlist(input$plot_brush))[1], as.numeric(unlist(input$plot_brush))[3])
      B <- c(as.numeric(unlist(input$plot_brush))[2], as.numeric(unlist(input$plot_brush))[4])

      AtoB16 <- shortestPath(Conductance, A, B, output="SpatialLines")

      ###

      Conductance <- conductance_calc(dem, 8)

      if(is.null(input$plot_brush)) return("NULL\n")
      A <- c(as.numeric(unlist(input$plot_brush))[1], as.numeric(unlist(input$plot_brush))[3])
      B <- c(as.numeric(unlist(input$plot_brush))[2], as.numeric(unlist(input$plot_brush))[4])

      AtoB8 <- shortestPath(Conductance, A, B, output="SpatialLines")

      ###

      Conductance <-conductance_calc(dem, 4)

      if(is.null(input$plot_brush)) return("NULL\n")
      A <- c(as.numeric(unlist(input$plot_brush))[1], as.numeric(unlist(input$plot_brush))[3])
      B <- c(as.numeric(unlist(input$plot_brush))[2], as.numeric(unlist(input$plot_brush))[4])

      AtoB4 <- shortestPath(Conductance, A, B, output="SpatialLines")

      ####
  plot(dem, axes = FALSE, legend = FALSE)
  lines(AtoB4, col = "red")
  lines(AtoB8, col = "black")
  lines(AtoB16, col = "blue")

  elevation <<- data.frame(extract(dem, AtoB4))
  names(elevation) <- "metres"

  })
  output$stats_plots <- renderPlot( {



  })
} 

标签: rshiny

解决方案


推荐阅读