r - 在 Shiny 中插入警告信息
问题描述
当 fileInput 中插入的文件与“.xlsx”、“.shp”、“.shx”、“.dbf”不同时,我想插入一条警告消息。你能帮助我吗?我在下面输入了一个可执行代码。你甚至可以在我的 observeEvent (input$data, 我插入了类似的东西,但我希望它在 Shiny.
谢谢!
library(shiny)
library(ggplot2)
library(shinythemes)
library(rdist)
library(openxlsx)
library(geosphere)
library(rgdal)
function.cl<-function(df,k){
}
ui <- bootstrapPage(
navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
"Cl",
tabPanel("Solution",
fileInput("data", h3("Excel or Shapefile import"),
accept = c(".xlsx",".shp",".shx",".dbf"),
multiple= T),
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, {
if(any(grepl(".xlsx",input$data$name))){
v$df <- read.xlsx(input$data$datapath)
}else if(any(grepl(".shp",input$data$name))){
shpDF <- input$data
failed <- F
if(!any(grepl(".shx",input$data$name))){
failed<-T
}
if(!any(grepl(".dbf",input$data$name))){
failed<-T
}
if(failed){
print("You Need 3 files, '*.shp', '*shx' and '*.dbf'")
}else{
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")]
shpName<-substr(shpName,1,nchar(shpName)-4)
setwd(prevWD)
shpFile<-readOGR(dsn=uploadDirectory,layer=shpName)
v$df<-shpFile@data
}
}else{
print("Wrong File")
}
})
Modelcl<-reactive({if (!is.null(v$df)) {
function.cl(v$df,input$Slider)
}
})
output$ScatterPlot <- renderPlot({
Modelcl()[[1]]
})
}
shinyApp(ui = ui, server = server)
解决方案
正如@Stéphane Laurent 在评论中已经指出的那样,shinyWidgets
可用于显示甜蜜警报。对于某些输入,{shinyFeedback} 包也可以正常工作,但是fileInput
尚不支持 s。
下面是一种使用sendSweetAlert
替换print
调用的可能实现。
library(shiny)
library(shinyWidgets)
library(ggplot2)
library(shinythemes)
library(rdist)
library(openxlsx)
library(geosphere)
library(rgdal)
function.cl<-function(df,k){
}
ui <- bootstrapPage(
navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
"Cl",
tabPanel("Solution",
fileInput("data", h3("Excel or Shapefile import"),
accept = c(".xlsx",".shp",".shx",".dbf"),
multiple= T),
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, {
if(any(grepl(".xlsx",input$data$name))){
v$df <- read.xlsx(input$data$datapath)
}else if(any(grepl(".shp",input$data$name))){
shpDF <- input$data
failed <- F
if(!any(grepl(".shx",input$data$name))){
failed<-T
}
if(!any(grepl(".dbf",input$data$name))){
failed<-T
}
if(failed){
sendSweetAlert(
session = session,
title = "Error !!",
text = "You Need 3 files, '*.shp', '*shx' and '*.dbf'",
type = "error"
)
}else{
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")]
shpName<-substr(shpName,1,nchar(shpName)-4)
setwd(prevWD)
shpFile<-readOGR(dsn=uploadDirectory,layer=shpName)
v$df<-shpFile@data
}
}else{
sendSweetAlert(
session = session,
title = "Error !!",
text = "Wrong File",
type = "error"
)
}
})
Modelcl<-reactive({if (!is.null(v$df)) {
function.cl(v$df,input$Slider)
}
})
output$ScatterPlot <- renderPlot({
Modelcl()[[1]]
})
}
shinyApp(ui = ui, server = server)
推荐阅读
- qt - qml - 引用顶部。
不能从 ListView 工作? - javascript - 使用 keydown 短暂发生更改,然后消失
- python - AttributeError:写入文件时“NoneType”对象没有属性“get”
- javascript - form.io 如何在模糊上触发自定义验证?
- ruby-on-rails - 运行 graphQL 突变的正确方法是什么?
- python - 400 错误请求:无法解码 JSON 对象;PUT 请求(Flask)
- java - 我想按顺序打印薪水最后一个称为安排的函数必须完成这项工作,而不是代码正在工作
- reactjs - 使用带有 React-Scripts 版本 3.4.1 的 Create React App 代理多个 API 请求
- python - 网页抓取:花费大量时间
- java - Rhapsody (windows 7 version) for java , java 示例项目编译,但运行立即关闭程序