R上传错误文件时发出警告消息
我想在我的闪亮应用程序中有一个功能,当有人上传一个不是.csv格式的文件时,他们会收到警告,当他们上传.csv格式的文件时,它会打印表格。这是我的UI代码R上传错误文件时发出警告消息,r,csv,file-upload,shiny,rstudio,R,Csv,File Upload,Shiny,Rstudio,我想在我的闪亮应用程序中有一个功能,当有人上传一个不是.csv格式的文件时,他们会收到警告,当他们上传.csv格式的文件时,它会打印表格。这是我的UI代码 shinyUI( tabPanel("File Upload", h4("File Upload"), sidebarLayout( sidebarPanel(
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!")
})
}
shinyServer(功能(输入、输出){
输出$upload您真正需要的只是一个验证
语句。此外,您还需要使用%in%
函数检查值是否在向量中,仅供参考。以下是警告/错误的简单实现。它利用工具
包中的文件扩展
函数
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)
})
}
))
库(闪亮)
图书馆(工具)
runApp(
名单(
ui=tabPanel(“文件上传”,
h4(“文件上传”),
侧边栏布局(
侧栏面板(
fileInput('file1','选择要上载的CSV文件',
接受=c(
“文本/csv”,
“文本/逗号分隔值”,
“文本/制表符分隔值”,
“文本/纯文本”,
“csv”,
“tsv”
)
)
),
主面板(
tableOutput('上载')
)
)
),
服务器=功能(输入、输出){
输出$upload那么问题出在哪里?出现了什么错误?我的问题是,它似乎永远无法捕获正确的文件类型。它打印每种格式,即使它不是csv格式。$type似乎不起作用,并发送一个空值