我正在使用R Shiny應用程序,它在輸入中採用兩個shapefile,然後將它們相交併計算面積。 當第一個shapefile上傳時,我想重置並移除輸入中的第二個shapefile,所以在新分析中,我想將第二個shapefile(file2
)設置爲NULL。 我嘗試使用shinyjs::reset("file2")
但第二shape文件(input$file2
)仍然在內存中,當我上傳了新的shape文件(file1
,input$file1
),然後點擊分析按鈕(內部消除上傳其他file2
)應用程序開始分析,如file2
沒有重置了。R閃亮不重置fileInput並將其保存在內存中
這是我使用的代碼:
庫和功能
library(shiny)
library(shinyjs)
library(leaflet)
library(mapview)
library(rgdal)
library(rgeos)
library(maptools)
library(DT)
fIntersect<-function(file1,file2){
CRSfrom <- CRS("+proj=utm +zone=33 +datum=WGS84 +units=m+no_defs")
CRSto <- CRS("+proj=longlat +datum=WGS84")
shpInt <- disaggregate(intersect(file1, file2))
[email protected]$area<- round(gArea(shpInt, byid = TRUE)/10000,digits= 2)
IntData<-data.table([email protected])
return(list("IntData"=IntData))
}
ui.R
ui <- fluidPage(
useShinyjs(),
fileInput('file1', 'Choose File',multiple = TRUE),
fileInput('file2', 'Choose File',multiple = TRUE),
actionButton("Analize", "Analize"),
box(leafletOutput("Map",width ="100%")),
box(dataTableOutput("IntData"))),
server.R
server <- function(input, output) {
#CRS setting
CRSfrom <- CRS("+proj=utm +zone=33 +datum=WGS84 +units=m +no_defs")
CRSto <- CRS("+proj=longlat +datum=WGS84")
#Render Input file and upload
output$Map <- renderLeaflet({
leaflet() %>%setView(16,40,zoom=6)%>%
addTiles() })
output$file1 <- renderText({
file1 <- input$file1
if (is.null(input$file1))
return(NULL)
})
output$file2 <- renderText({
file2 <- input$file2
if (is.null(file2))
return(NULL)
})
uploadfile1 <- reactive({
if (!is.null(input$file1)) {
shpDF <- input$file1
prevWD <- getwd()
uploadDirectory <- dirname(shpDF$datapath[1])
setwd(uploadDirectory)
for (i in 1:nrow(shpDF)) {
file.rename(shpDF$datapath[i], shpDF$name[i])
}
shpName <- shpDF$name[grep(x = shpDF$name, pattern = "*.shp")]
shpPath <- paste(uploadDirectory, shpName, sep = "/")
setwd(prevWD)
file <- readShapePoly(shpPath,
proj4string =CRS("+proj=utm +zone=33 +datum=WGS84 +units=m +no_defs"))
return(file)
} else {
return(NULL)
}
})
uploadfile2 <- reactive({
if (!is.null(input$file2)) {
shpDF <- input$file2
prevWD <- getwd()
uploadDirectory <- dirname(shpDF$datapath[1])
setwd(uploadDirectory)
for (i in 1:nrow(shpDF)) {
file.rename(shpDF$datapath[i], shpDF$name[i])
}
shpName <- shpDF$name[grep(x = shpDF$name, pattern = "*.shp")]
shpPath <- paste(uploadDirectory, shpName, sep = "/")
setwd(prevWD)
file <- readShapePoly(shpPath,
proj4string =CRS("+proj=utm +zone=33 +datum=WGS84 +units=m +no_defs"))
return(file)
}
else {
return(NULL)
}
})
output$IntData <- renderDataTable(datatable(data.table("id" = "0")))
observeEvent(input$file1, {
# Show upload polygon on Map
shinyjs::reset('file2')
leafletProxy("Map")%>%clearGroup(c("file1")) ####
shpUpload <- spTransform(uploadfile1(), CRSto)
leafletProxy("Map") %>%
addPolygons(data = shpUpload,
color = "#33a02c",
group = "file1",
fill = FALSE,
weight = 2.5)
})
observeEvent(input$file2, {
# Show upload polygon on Map
leafletProxy("Map")%>%clearGroup(c("file2")) ####
shpUpload <- spTransform(uploadfile2(), CRSto)
leafletProxy("Map") %>%
addPolygons(data = shpUpload,
color = "#33a02c",
group = "file2",
fill = FALSE,
weight = 2.5)
})
#Start analysis
observeEvent(input$Analize,{
if(input$Analize>0){ withProgress(message = "Sto eseguendo l'analisi...",
value =0, {
Intersection<-fIntersect(uploadfile1(),uploadfile2())
observe({
output$IntData<-renderDataTable({
datatable(Intersection$IntData)
})
})
}
)
}else{}
}
)
#End Analysis
}
shinyApp( ui,服務器)
感謝您的任何建議。
我想問題在於_function call_ ie。當'input $ file1,input $ file2'被上傳時,** observeEvent **下的操作被執行一次。但是當你第二次上傳文件時,**注意:**'輸入$ file1,輸入$ file2'仍然是用舊值初始化的,所以沒有'observeEvent'被觸發。 您需要將'observeEvent'操作創建爲'reactive'函數並在服務器讀取文件時調用它們 – parth