Hi, everybody,
I'm new to this community.
I've already developed a shiny app (https://agronomofiorentini.shinyapps.io/smart_farming_app_en/), but I'd like to give it a major upgrade, i.e. I'd like to use RData file which contains pre-trained ML templates to be able to predict.
Locally my shinyapp works great, and also the server upload works great without any markers or errors.
But the moment it is online it disconnects me from the server
(https://agronomofiorentini.shinyapps.io/smart_farming_app_upgrade/)
You help me solve this problem.
Thank you
I will post my entire Rcode
library(shiny)
library(shinydashboard)
library(leaflet)
library(leaflet.extras)
library(mapview)
library(raster)
library(rsconnect)
library(FRK)
library(sp)
library(rgdal)
library(sf)
library(pdfCluster)
library(grDevices)
library(RStoolbox)
library(NbClust)
library(stats)
library(CORElearn)
ui <- dashboardPage(skin = "red",
dashboardHeader(title = "Smart Farming App", titleWidth = 350,
dropdownMenu(type = "tasks",
messageItem(
from = "First step",
message = "Indicate the field to analyze",
icon = icon("dice-one")),
messageItem(
from = "Secondo step",
message = "Improve the area with the edit layer tool",
icon = icon("dice-two")),
messageItem(
from = "Terzo step",
message = "Display the desired vegetation index",
icon = icon("dice-three")),
messageItem(
from = "Quarto step",
message = "Visualize the prescription map",
icon = icon("dice-four"))),
dropdownMenu(type = "notification",
badgeStatus = "warning",
notificationItem(icon = icon("users"),
status = "info",
text = "User guide",
href = "https://www.agronomofiorentini.it/smart-farming-app-english-version/"))),
dashboardSidebar(width = 350,
sidebarMenu(
menuItem(
"Where is your farm?",
tabName = "shape",
icon = icon("map")),
menuItem(
"Vegetation index",
tabName = "vegetation_index",
icon = icon("layer-group"),
menuSubItem("ARVI", tabName = "ARVI", icon = icon("map")),
menuSubItem("EVI", tabName = "EVI", icon = icon("map")),
menuSubItem("EVI2", tabName = "EVI2", icon = icon("map")),
menuSubItem("GARI", tabName = "GARI", icon = icon("map")),
menuSubItem("GCI", tabName = "GCI", icon = icon("map")),
menuSubItem("GDVI", tabName = "GDVI", icon = icon("map")),
menuSubItem("GIT3", tabName = "GIT3", icon = icon("map")),
menuSubItem("GNDVI", tabName = "GNDVI", icon = icon("map")),
menuSubItem("GOSAVI", tabName = "GOSAVI", icon = icon("map")),
menuSubItem("GRVI", tabName = "GRVI", icon = icon("map")),
menuSubItem("GSAVI", tabName = "GSAVI", icon = icon("map")),
menuSubItem("IPVI", tabName = "IPVI", icon = icon("map")),
menuSubItem("MSAVI2", tabName = "MSAVI2", icon = icon("map")),
menuSubItem("MSR", tabName = "MSR", icon = icon("map")),
menuSubItem("NDVI", tabName = "NDVI", icon = icon("map")),
menuSubItem("NNIR", tabName = "NNIR", icon = icon("map")),
menuSubItem("OSAVI", tabName = "OSAVI", icon = icon("map")),
menuSubItem("SAVI", tabName = "SAVI", icon = icon("map")),
menuSubItem("TDVI", tabName = "TDVI", icon = icon("map")),
menuSubItem("VDVI", tabName = "VDVI", icon = icon("map")),
menuSubItem("WDRVI", tabName = "WDRVI", icon = icon("map"))),
menuItem("Machine Learning - Only for Durum Wheat", tabName = "ML", icon = icon("layer-group"),
menuSubItem("Nitrogen Nutrition Index", tabName = "NNI", icon = icon("map")),
menuSubItem("Chlorophyll", tabName = "Clorofilla", icon = icon("map")),
menuSubItem("Grams of nitrogen per meter square", tabName = "gr_N_m2", icon = icon("map")),
menuSubItem("Grams of dry matter per meter square", tabName = "peso_secco_g_m2", icon = icon("map"))),
menuItem("Prescription map", tabName = "precription_map", icon = icon("map")))),
dashboardBody(
tabItems(
tabItem(tabName = "shape",
leafletOutput(outputId = "mymap", height = 650)),
tabItem(tabName = "NDVI",
mapviewOutput(outputId = "NDVI", height = 650)),
tabItem(tabName = "EVI2",
mapviewOutput(outputId = "EVI2", height = 650)),
tabItem(tabName = "MSAVI2",
mapviewOutput(outputId = "MSAVI2", height = 650)),
tabItem(tabName = "IPVI",
mapviewOutput(outputId = "IPVI", height = 650)),
tabItem(tabName = "MSR",
mapviewOutput(outputId = "MSR", height = 650)),
tabItem(tabName = "OSAVI",
mapviewOutput(outputId = "OSAVI", height = 650)),
tabItem(tabName = "SAVI",
mapviewOutput(outputId = "SAVI", height = 650)),
tabItem(tabName = "TDVI",
mapviewOutput(outputId = "TDVI", height = 650)),
tabItem(tabName = "GARI",
mapviewOutput(outputId = "GARI", height = 650)),
tabItem(tabName = "ARVI",
mapviewOutput(outputId = "ARVI", height = 650)),
tabItem(tabName = "EVI",
mapviewOutput(outputId = "EVI", height = 650)),
tabItem(tabName = "GCI",
mapviewOutput(outputId = "GCI", height = 650)),
tabItem(tabName = "GDVI",
mapviewOutput(outputId = "GDVI", height = 650)),
tabItem(tabName = "GIT3",
mapviewOutput(outputId = "GIT3", height = 650)),
tabItem(tabName = "GNDVI",
mapviewOutput(outputId = "GNDVI", height = 650)),
tabItem(tabName = "GOSAVI",
mapviewOutput(outputId = "GOSAVI", height = 650)),
tabItem(tabName = "GRVI",
mapviewOutput(outputId = "GRVI", height = 650)),
tabItem(tabName = "NNIR",
mapviewOutput(outputId = "NNIR", height = 650)),
tabItem(tabName = "GSAVI",
mapviewOutput(outputId = "GSAVI", height = 650)),
tabItem(tabName = "VDVI",
mapviewOutput(outputId = "VDVI", height = 650)),
tabItem(tabName = "WDRVI",
mapviewOutput(outputId = "WDRVI", height = 650)),
tabItem(tabName = "NNI",
mapviewOutput(outputId = "NNI", height = 650)),
tabItem(tabName = "Clorofilla",
mapviewOutput(outputId = "Clorofilla", height = 650)),
tabItem(tabName = "gr_N_m2",
mapviewOutput(outputId = "gr_N_m2", height = 650)),
tabItem(tabName = "peso_secco_g_m2",
mapviewOutput(outputId = "peso_secco_g_m2", height = 650)),
tabItem(
tabName = "precription_map",
mapviewOutput(outputId = "zone_map_prescription", height = 650)
)
)
)
)
server <- function(input, output, session) {
load("data/ML_models_new.RData")
blue<-raster("data/blue_regione_marche.tif")
green<-raster("data/green_regione_marche.tif")
red<-raster("data/red_regione_marche.tif")
nir<-raster("data/nir_regione_marche.tif")
marche<-readOGR(dsn = "data/Marche_shape_a", layer = "Limiti_regionale_Marche")
marche <- spTransform(marche, CRS("+proj=longlat +datum=WGS84"))
output$mymap <- renderLeaflet({
leaflet(marche) %>%
addPolylines(color = "red") %>%
addProviderTiles(providers$CartoDB.DarkMatter, options = tileOptions(minZoom = 2, maxZoom = 12)) %>%
addProviderTiles(providers$OpenStreetMap, options = tileOptions(minZoom = 12, maxZoom = 15)) %>%
addProviderTiles(providers$Esri.WorldImagery,
options = tileOptions(minZoom = 15, maxZoom = 20),
group = "Esri.WorldImagery") %>%
addDrawToolbar(
targetGroup='drawPoly',
polylineOptions = F,
circleOptions = F,
markerOptions = F,
circleMarkerOptions = F,
rectangleOptions = F,
singleFeature = TRUE,
editOptions = editToolbarOptions(selectedPathOptions = selectedPathOptions())) %>%
addLayersControl(overlayGroups = c('Multispectral images updated to: 08-04-2020'),
options = layersControlOptions(collapsed=FALSE)) %>%
addMeasure(primaryLengthUnit = "meters",
primaryAreaUnit = "hectares",
position = "topleft",
activeColor = "red",
completedColor = "red",
localization = "en") %>%
addControlGPS(options = gpsOptions(position = "topleft",
activate = TRUE,
autoCenter = TRUE,
maxZoom = 5,
setView = TRUE))
})
observeEvent(input$mymap_draw_new_feature,{
feature <- input$mymap_draw_new_feature
progress <- shiny::Progress$new()
progress$set(message = "WORKING IN PROGRESS")
df<-as.data.frame(feature$geometry$coordinates)
df<-as.data.frame(matrix(unlist(df, use.names=FALSE),ncol=2, byrow=TRUE))
names(df)[names(df) == "V1"] <- "x"
names(df)[names(df) == "V2"] <- "y"
df$ID<-1
pols <- df_to_SpatialPolygons(df, "ID", c("x","y"), CRS("+proj=longlat +datum=WGS84"))
pols <- spTransform(pols, CRS("+proj=utm +zone=33 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0"))
clip<-function(raster,shape) {
a1_crop<-crop(raster,shape)
step1<-rasterize(shape,a1_crop)
a1_crop*step1}
blue<-clip(blue, pols)
green<-clip(green, pols)
red<-clip(red, pols)
nir<-clip(nir, pols)
NDVI <- ((nir-red)/(nir+red))
EVI2 <- 2.5*((nir-red)/(nir+(2.4*red)+1))
MSAVI2 <- (((2*nir)+1) - (sqrt((((2*nir)+1)^2)-(8*(nir-red)))))/2
IPVI <- nir/(nir+red)
MSR <- (((nir/red)-1)/(sqrt(nir/red))+1)
OSAVI <- ((nir-red)/(nir+red+0.16))
SAVI <- (1.5*(nir-red))/(nir+red+0.5)
TDVI <- 1.5*(((nir-red)/(sqrt((nir^2)+red+0.5))))
GARI <- ((nir-(green-(1.7*(blue-red))))/(nir+(green-(1.7*(blue-red)))))
ARVI <- ((nir-(red-(blue-red)))/(nir+(red-(blue-red))))
EVI <- ((nir-red)/(nir+(6*red))-(7.5*blue)+1)
GCI <- ((nir/green)-1)
GDVI <- nir-green
GIT3 <- ((nir-blue)/(green-blue)-1)
GNDVI <- ((nir-green)/(nir+green))
GOSAVI <- ((nir-green)/(nir+green+0.16))
GRVI <- nir/green
NNIR <- nir/(nir+red+green)
GSAVI <- 1.5*((nir-green)/(nir+green+0.5))
VDVI <- ((2*green)-red-blue)/((2*green)+red+blue)
WDRVI <- ((0.2*nir)-red)/((0.2*nir)+red)
brick<-brick(ARVI, GARI, GNDVI, GOSAVI, IPVI, NNIR)
names(brick)=c("ARVI", "GARI", "GNDVI", "GOSAVI", "IPVI", "NNIR")
xy<-coordinates(brick)
brick_a<-as.data.frame(brick)
brick_a <- data.frame(xy, brick_a)
brick_a<-na.omit(brick_a)
brick_for_prediction<-brick_a[, -1]
brick_for_prediction<-brick_a[, -1]
NNI<-as.data.frame(predict(gbm_NNI, brick_for_prediction))
Clorofilla<-as.data.frame(predict(gbm_Clorofilla, brick_for_prediction))
gr_N_m2<-as.data.frame(predict(gbm_gr_N_m2, brick_for_prediction))
peso_secco_g_m2<-as.data.frame(predict(gbm_peso_secco_g_m2, brick_for_prediction))
NNI$x<-brick_a$x
NNI$y<-brick_a$y
coordinates(NNI) <- ~x+y
gridded(NNI) <- TRUE
Clorofilla$x<-brick_a$x
Clorofilla$y<-brick_a$y
coordinates(Clorofilla) <- ~x+y
gridded(Clorofilla) <- TRUE
gr_N_m2$x<-brick_a$x
gr_N_m2$y<-brick_a$y
coordinates(gr_N_m2) <- ~x+y
gridded(gr_N_m2) <- TRUE
peso_secco_g_m2$x<-brick_a$x
peso_secco_g_m2$y<-brick_a$y
coordinates(peso_secco_g_m2) <- ~x+y
gridded(peso_secco_g_m2) <- TRUE
NNI <- raster(NNI)
Clorofilla <- raster(Clorofilla)
gr_N_m2 <- raster(gr_N_m2)
peso_secco_g_m2 <- raster(peso_secco_g_m2)
crs(NNI) <- "+proj=utm +zone=33 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0"
crs(Clorofilla) <- "+proj=utm +zone=33 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0"
crs(gr_N_m2) <- "+proj=utm +zone=33 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0"
crs(peso_secco_g_m2) <- "+proj=utm +zone=33 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0"
RdYlGr = colorRampPalette(c('Red', 'Yellow', "Green"))
output$NDVI<-renderMapview({
mapview(NDVI, map.types = c("Esri.WorldImagery"), col.regions=RdYlGr)
})
output$EVI2<-renderMapview({
mapview(EVI2, map.types = c("Esri.WorldImagery"), col.regions=RdYlGr)
})
output$MSAVI2<-renderMapview({
mapview(MSAVI2, map.types = c("Esri.WorldImagery"), col.regions=RdYlGr)
})
output$IPVI<-renderMapview({
mapview(IPVI, map.types = c("Esri.WorldImagery"), col.regions=RdYlGr)
})
output$MSR<-renderMapview({
mapview(MSR, map.types = c("Esri.WorldImagery"), col.regions=RdYlGr)
})
output$OSAVI<-renderMapview({
mapview(OSAVI, map.types = c("Esri.WorldImagery"), col.regions=RdYlGr)
})
output$SAVI<-renderMapview({
mapview(SAVI, map.types = c("Esri.WorldImagery"), col.regions=RdYlGr)
})
output$TDVI<-renderMapview({
mapview(TDVI, map.types = c("Esri.WorldImagery"), col.regions=RdYlGr)
})
output$GARI<-renderMapview({
mapview(GARI, map.types = c("Esri.WorldImagery"), col.regions=RdYlGr)
})
output$ARVI<-renderMapview({
mapview(ARVI, map.types = c("Esri.WorldImagery"), col.regions=RdYlGr)
})
output$EVI<-renderMapview({
mapview(EVI, map.types = c("Esri.WorldImagery"), col.regions=RdYlGr)
})
output$GCI<-renderMapview({
mapview(GCI, map.types = c("Esri.WorldImagery"), col.regions=RdYlGr)
})
output$GDVI<-renderMapview({
mapview(GDVI, map.types = c("Esri.WorldImagery"), col.regions=RdYlGr)
})
output$GIT3<-renderMapview({
mapview(GIT3, map.types = c("Esri.WorldImagery"), col.regions=RdYlGr)
})
output$GNDVI<-renderMapview({
mapview(GNDVI, map.types = c("Esri.WorldImagery"), col.regions=RdYlGr)
})
output$GOSAVI<-renderMapview({
mapview(GOSAVI, map.types = c("Esri.WorldImagery"), col.regions=RdYlGr)
})
output$GRVI<-renderMapview({
mapview(GRVI, map.types = c("Esri.WorldImagery"), col.regions=RdYlGr)
})
output$NNIR<-renderMapview({
mapview(NNIR, map.types = c("Esri.WorldImagery"), col.regions=RdYlGr)
})
output$GSAVI<-renderMapview({
mapview(GSAVI, map.types = c("Esri.WorldImagery"), col.regions=RdYlGr)
})
output$VDVI<-renderMapview({
mapview(VDVI, map.types = c("Esri.WorldImagery"), col.regions=RdYlGr)
})
output$WDRVI<-renderMapview({
mapview(WDRVI, map.types = c("Esri.WorldImagery"), col.regions=RdYlGr)
})
output$NNI<-renderMapview({
mapview(NNI, map.types = c("Esri.WorldImagery"), col.regions=RdYlGr)
})
output$Clorofilla<-renderMapview({
mapview(Clorofilla, map.types = c("Esri.WorldImagery"), col.regions=RdYlGr)
})
output$gr_N_m2<-renderMapview({
mapview(gr_N_m2, map.types = c("Esri.WorldImagery"), col.regions=RdYlGr)
})
output$peso_secco_g_m2<-renderMapview({
mapview(peso_secco_g_m2, map.types = c("Esri.WorldImagery"), col.regions=RdYlGr)
})
pca<-rasterPCA(brick, spca=TRUE)
NDVI_a<-pca$map$PC1
NDVI_ZM <- rasterToPoints(NDVI_a, spatial=TRUE)
NDVI_ZM<-as.data.frame(NDVI_ZM)
zone <- pdfCluster(NDVI_ZM$PC1)
zone <- as.data.frame(zone@clusters)
xy <- as.data.frame(coordinates(NDVI_ZM))
ZMmap_d <- data.frame(zone, xy)
ZMmap_a <- st_as_sf(x = ZMmap_d, coords = c("x", "y"), crs = "+proj=utm +zone=33 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0")
coordinates(ZMmap_d) <- ~ x + y
crs.geo <- CRS("+proj=utm +zone=33 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0")
proj4string(ZMmap_d) <- crs.geo
gridded(ZMmap_d) <- TRUE
ZMmap_b <- raster(ZMmap_d)
Zone_management<-rasterToPolygons(ZMmap_b)
output$zone_map_prescription<-renderMapview({
mapview(Zone_management, map.types = c("Esri.WorldImagery"))
})
on.exit(progress$close())
})
}
shinyApp(ui = ui, server = server)