【发布时间】:2020-06-23 06:28:04
【问题描述】:
下面的代码从 shapefile 文件生成散点图。它正在正常生成(见附图)。但是,我不想将文件目录直接插入到代码中,而是想通过 fileInput 插入文件。我在下面插入了 fileInput,但我想帮助调整我的服务器。 我认为有必要调整与反应相关的东西。
非常感谢!
library(shiny)
library(ggplot2)
library(shinythemes)
library(rdist)
library(geosphere)
library(rgdal)
function.cl<-function(df,k){
shape<-readOGR(dsn="C:/Users/Jose 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)
【问题讨论】:
-
你想在服务器上做什么?加载选定的文件?
-
感谢您的回答。我想在通过 fileInput 上传文件后进行聚类。您不必为此文件输入在服务器中进行反应吗?
-
为什么 function.cl 有一个 df 参数,你在函数的第二行覆盖了它?