Hello,
The code below is generating a scatterplot from a shapefile file. It is generating normally (see the attached image). However, instead of inserting the file directory directly into the code, I would like to insert the file through a fileInput. I inserted the fileInput in the code, but I would like help in making it work. I inserted the shapefile in my github so that you can take the test if you can (GitHub - JovaniSouza/JovaniSouza5).
Thank you so much!
library(shiny)
library(ggplot2)
library(readxl)
library(shinythemes)
library(rdist)
library(geosphere)
library(rgdal)
function.cl<-function(df,k){
shape<-readOGR(dsn="C:/Users/Jovani Souza/Documents/Test",layer="Export_Output_3")
df<-shape@data
#clusters
coordinates<-df[c("Latitude","Longitude")]
d<-as.dist(distm(coordinates[,2:1]))
fit.average<-hclust(d,method="average")
clusters<-cutree(fit.average, k)
nclusters<-matrix(table(clusters))
df$cluster <- clusters
#all cluster data df1 and specific cluster df_spec_clust
df1<-df[c("Latitude","Longitude")]
df1$cluster<-as.factor(clusters)
#Colors
my_colors <- rainbow(length(df1$cluster))
names(my_colors) <- df1$cluster
#Scatter Plot for all clusters
g <- ggplot(data = df1, aes(x=Longitude, y=Latitude, color=cluster)) +
geom_point(aes(x=Longitude, y=Latitude), size = 4) +
scale_color_manual("Legend", values = my_colors)
plotGD <- g
return(list(
"Plot" = plotGD
))
}
ui <- bootstrapPage(
navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
"Cl",
tabPanel("Solution",
fileInput("shp", h3("Shapefile Import"), multiple = TRUE, accept = c('.shp', '.dbf','.sbn', '.sbx', '.shx', '.prj')),
sidebarLayout(
sidebarPanel(
sliderInput("Slider", h5(""),
min = 2, max = 4, value = 3),
),
mainPanel(
tabsetPanel(
tabPanel("Solution", plotOutput("ScatterPlot"))))
))))
server <- function(input, output, session) {
Modelcl<-reactive({
function.cl(df,input$Slider)
})
output$ScatterPlot <- renderPlot({
Modelcl()[[1]]
})
}
shinyApp(ui = ui, server = server)
I thought of inserting some reactive similar to what I did below in my server, but it didn't work either.
Modelcl <- eventReactive(input$shp,{
function.cl(input$shp,input$Slider)
})
Every help is welcome.
Thank you!