r - 访问在不同的 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( {
})
}
解决方案
推荐阅读
- python - Tkinter Text 滚动延迟问题
- api - 邮递员获取请求 200 OK 但正文不完整
- c# - 如何通过单击 MainForm 上另一个 UserControl 中的 ListView 项将 UserControl 从 MainForm 带到前面
- android - LocationListener 仅适用于 Android 8+(API 26 及更高版本)
- flutter - Flutter 中的 FadeIn/FadeOut 页面过渡
- c++ - 我可以按行第一个值对二维向量进行排序吗?
- python - 使用生成器训练 TensorFlow 模型
- python - html中存储的信息在哪里?(网络抓取)
- python - 我需要多个输入来运行主要功能电话簿python
- php - WooCommerce:在订单状态更改时仅运行一次代码