我想知道如何创建一般警报,如果fileInput中加载的数据库与我的代码的理想基础不符,因为可能存在多个问题,例如:[.data.frame中的错误:选择了未定义的列,hclust中的错误:NA / NaN / Inf,以及其他错误。那么,有什么办法可以做到这一点?如果加载的数据库有问题,显示警报吗?我在可执行代码下方插入了仅用于测试的代码。可以从以下网站下载数据库:
https://github.com/JovaniSouza/JovaniSouza5/blob/master/Example.xlsx
library(shiny)
library(ggplot2)
library(shinythemes)
library(rdist)
library(geosphere)
library(rgdal)
function.cl<-function(df,k){
#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("data", h3("Excel import")),
sidebarLayout(
sidebarPanel(
sliderInput("Slider", h5(""),
min = 2, max = 4, value = 3),
),
mainPanel(
tabsetPanel(
tabPanel("Solution", plotOutput("ScatterPlot"))))
))))
server <- function(input, output, session) {
v <- reactiveValues(df = NULL)
observeEvent(input$data, {
v$df <- read_excel(input$data$datapath)
})
Modelcl<-reactive({if (!is.null(v$df)) {
function.cl(v$df,input$Slider)
}
})
output$ScatterPlot <- renderPlot({
Modelcl()[[1]]
})
}
shinyApp(ui = ui, server = server)
也许像这样(我没有尝试过)。这需要的shinyWidgets
软件包sendSweetAlert
。
Modelcl <- reactive({
req(v$df)
out <- NULL
tryCatch({
out <<- function.cl(v$df, input$Slider)
}, error = function(e){
sendSweetAlert(
session,
"An error occured",
"Try to upload another file.",
"error"
)
})
out
})
output$ScatterPlot <- renderPlot({
req(Modelcl())
Modelcl()[[1]]
})
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句