访问在不同的RenderPlot中生成的数据帧

时间:2018-09-04 12:09:44

标签: r shiny

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

路线的可视化只是我想发生的一个组成部分。我还希望能够创建路线的摘要统计信息,并在不同的图中显示它们(因此,路线显示在左侧,统计信息显示在右侧)。

但是,我不确定如何在另一个图中访问该路由。我希望其他图可以访问的是

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

然后将使用高程创建汇总统计信息,该统计信息将显示在右列中。

任何有关如何执行此操作的想法都值得赞赏。还建议您采用其他方法完全做到这一点。

可复制的示例:

ui.R

# 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"))
    )
  )

server.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( {



  })
} 

0 个答案:

没有答案