R上传错误文件时闪烁警告消息 [英] R Shiny warning message when wrong file is uploaded
本文介绍了R上传错误文件时闪烁警告消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在我的闪亮的应用程序的一个功能,当有人上传的文件不是.CSV格式,他们收到一个警告,当他们做的上传.CSV格式,它打印表。这里是我的UI代码
I want to have a feature in my shiny app that when someone uploads a file which is not in .csv format they recieve a warning and when they do upload in .csv format it prints the table. here is my UI code
shinyUI(
tabPanel("File Upload",
h4("File Upload"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose CSV file to upload',
accept = c(
'text/csv',
'text/comma-separated-values',
'text/tab-separated-values',
'text/plain',
'.csv',
'.tsv'
)
)
),
mainPanel(
tableOutput('upload'),
h1(textOutput("warning1"))
)
)
)
)
和我的服务器代码
shinyServer(function(input, output) {
output$upload <- renderTable({
#assign uploaded file to a variable
File <- input$file1
#catches null exception
if (is.null(File))
return(NULL)
read.csv(File$datapath)
})
output$warning1 <- renderPrint({
upload<- input$file1
if (is.null(upload))
return(NULL)
if (upload$type != c(
'text/csv',
'text/comma-separated-values',
'text/tab-separated-values',
'text/plain',
'.csv',
'.tsv'
)
)
return ("Wrong File Format try again!")
})
}
推荐答案
你真正需要的是 validate
语句。另外,你需要使用%in%
函数来检查一个值是否在向量中,只是FYI。以下是一个更简单的实现您的警告/错误。它使用 tools
包中的 file_ext
函数。
All you really need is a validate
statement. Also, you would need to use the %in%
function to check if a value is in a vector, just FYI. The following is a much simpler implementation for your warning/error. It utilizes the file_ext
function from the tools
package.
library(shiny)
library(tools)
runApp(
list(
ui = tabPanel("File Upload",
h4("File Upload"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose CSV file to upload',
accept = c(
'text/csv',
'text/comma-separated-values',
'text/tab-separated-values',
'text/plain',
'csv',
'tsv'
)
)
),
mainPanel(
tableOutput('upload')
)
)
),
server = function(input, output){
output$upload <- renderTable({
#assign uploaded file to a variable
File <- input$file1
#catches null exception
if (is.null(File))
return(NULL)
validate(
need(file_ext(File$name) %in% c(
'text/csv',
'text/comma-separated-values',
'text/tab-separated-values',
'text/plain',
'csv',
'tsv'
), "Wrong File Format try again!"))
read.csv(File$datapath)
})
}
))
这篇关于R上传错误文件时闪烁警告消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文