如何在 R shiny 中保存以前的反应值?
How can I save previous reactive values in R shiny?
您好,感谢您的回答,
如何在 R shiny 中保存以前的反应值?我想提取最后两个标记位置(传单)的坐标。我尝试使用隔离功能(但没有成功)。在当前形式下,我的代码可以存储当前反应变量,但不能存储最后一个。
这是我的代码:
# Define UI
ui <- fluidPage(
leafletOutput("mymap",height=800)
)
# Define server logic
server <- function(input, output) {
# Leaflet
output$mymap <- renderLeaflet(
leaflet() %>%
addTiles() %>%
setView(lng = 8.53918, lat = 47.36864, zoom = 11) %>%
addDrawToolbar(
targetGroup='draw',
polylineOptions = F,
polygonOptions = F,
rectangleOptions = F,
circleOptions = F,
circleMarkerOptions = F,
editOptions = editToolbarOptions(selectedPathOptions = selectedPathOptions())) %>%
addLayersControl(overlayGroups = c('draw'), options =
layersControlOptions(collapsed=FALSE))
)
reactive({
PosData <- data.frame(PosX = c(NA,NA),
PosY = c(NA,NA),
PosDegree = c(NA,NA))
})
# Extract coordinates
observeEvent(input$mymap_draw_new_feature,{
if(input$mymap_draw_new_feature$geometry$type == "Point"){
PosData$PosX[1] <- input$mymap_draw_new_feature$geometry$coordinates[[1]]
PosData$PosY[1] <- input$mymap_draw_new_feature$geometry$coordinates[[2]]
PosData$PosX[2] <- PosData$PosX[1]
PosData$PosY[2] <- PosData$PosY[1]
print(PosData)
}else{f <- 2}
})
}
# Run the application
shinyApp(ui = ui, server = server)```
在此应用程序中,反应式 table PosData
将包含最后 2 个标记的位置:
library(shiny)
library(leaflet)
library(leaflet.extras)
library(tidyverse)
ui <- fluidPage(
leafletOutput("mymap", height = 800),
tableOutput("PosData")
)
# Define server logic
server <- function(input, output) {
# Leaflet
output$mymap <- renderLeaflet(
leaflet() %>%
addTiles() %>%
setView(lng = 8.53918, lat = 47.36864, zoom = 11) %>%
addDrawToolbar(
targetGroup = "draw",
polylineOptions = F,
polygonOptions = F,
rectangleOptions = F,
circleOptions = F,
circleMarkerOptions = F,
editOptions = editToolbarOptions(selectedPathOptions = selectedPathOptions())
) %>%
addLayersControl(
overlayGroups = c("draw"), options =
layersControlOptions(collapsed = FALSE)
)
)
PosData <- reactiveVal(value = tibble(PosX = numeric(), PosY = numeric()))
output$PosData <- renderTable(PosData())
# Extract coordinates
observeEvent(input$mymap_draw_new_feature, {
req(input$mymap_draw_new_feature$geometry$type == "Point")
PosData() %>%
add_row(
PosX = input$mymap_draw_new_feature$geometry$coordinates[[1]],
PosY = input$mymap_draw_new_feature$geometry$coordinates[[2]]
) %>%
# keep last 2 points
tail(2) %>%
PosData()
})
}
# Run the application
shinyApp(ui = ui, server = server)
您好,感谢您的回答,
如何在 R shiny 中保存以前的反应值?我想提取最后两个标记位置(传单)的坐标。我尝试使用隔离功能(但没有成功)。在当前形式下,我的代码可以存储当前反应变量,但不能存储最后一个。
这是我的代码:
# Define UI
ui <- fluidPage(
leafletOutput("mymap",height=800)
)
# Define server logic
server <- function(input, output) {
# Leaflet
output$mymap <- renderLeaflet(
leaflet() %>%
addTiles() %>%
setView(lng = 8.53918, lat = 47.36864, zoom = 11) %>%
addDrawToolbar(
targetGroup='draw',
polylineOptions = F,
polygonOptions = F,
rectangleOptions = F,
circleOptions = F,
circleMarkerOptions = F,
editOptions = editToolbarOptions(selectedPathOptions = selectedPathOptions())) %>%
addLayersControl(overlayGroups = c('draw'), options =
layersControlOptions(collapsed=FALSE))
)
reactive({
PosData <- data.frame(PosX = c(NA,NA),
PosY = c(NA,NA),
PosDegree = c(NA,NA))
})
# Extract coordinates
observeEvent(input$mymap_draw_new_feature,{
if(input$mymap_draw_new_feature$geometry$type == "Point"){
PosData$PosX[1] <- input$mymap_draw_new_feature$geometry$coordinates[[1]]
PosData$PosY[1] <- input$mymap_draw_new_feature$geometry$coordinates[[2]]
PosData$PosX[2] <- PosData$PosX[1]
PosData$PosY[2] <- PosData$PosY[1]
print(PosData)
}else{f <- 2}
})
}
# Run the application
shinyApp(ui = ui, server = server)```
在此应用程序中,反应式 table PosData
将包含最后 2 个标记的位置:
library(shiny)
library(leaflet)
library(leaflet.extras)
library(tidyverse)
ui <- fluidPage(
leafletOutput("mymap", height = 800),
tableOutput("PosData")
)
# Define server logic
server <- function(input, output) {
# Leaflet
output$mymap <- renderLeaflet(
leaflet() %>%
addTiles() %>%
setView(lng = 8.53918, lat = 47.36864, zoom = 11) %>%
addDrawToolbar(
targetGroup = "draw",
polylineOptions = F,
polygonOptions = F,
rectangleOptions = F,
circleOptions = F,
circleMarkerOptions = F,
editOptions = editToolbarOptions(selectedPathOptions = selectedPathOptions())
) %>%
addLayersControl(
overlayGroups = c("draw"), options =
layersControlOptions(collapsed = FALSE)
)
)
PosData <- reactiveVal(value = tibble(PosX = numeric(), PosY = numeric()))
output$PosData <- renderTable(PosData())
# Extract coordinates
observeEvent(input$mymap_draw_new_feature, {
req(input$mymap_draw_new_feature$geometry$type == "Point")
PosData() %>%
add_row(
PosX = input$mymap_draw_new_feature$geometry$coordinates[[1]],
PosY = input$mymap_draw_new_feature$geometry$coordinates[[2]]
) %>%
# keep last 2 points
tail(2) %>%
PosData()
})
}
# Run the application
shinyApp(ui = ui, server = server)