I've written a Shiny app that allows the user to select two points on a raster, resulting in the computation of a route using different parameters.
The visualisation of the route is only one component I want to happen. I also want to be able to create summary statistics of the route and show these in a different plot (so the route is shown on the left, and the statistics on the right).
However, I'm not sure how to make the route accessible within another Plot. What I want to be accessible to the other Plot is the
elevation <- data.frame(extract(dem, AtoB4))
Elevation will then be used to create the summary statistics that will be shown in the right column.
Any thoughts on how to do this is appreciated. Recommendations of a different way to do it completely is also appreciated.
Reproducible example (current version view-able here: https://josephlewis1992.shinyapps.io/methodology/):
ui.R
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( {
})
}