当 fread 键参数没有特定的列名称时,R Shiny 应用程序崩溃
R Shiny app crashes when fread key parameter doesn't have that specific column name
如何从 data.table::fread 函数传递 "key" 参数而不会在上传的数据集没有该特定列名时闪亮崩溃?换句话说,如果我上传的数据集没有 "key" 列,它不会崩溃,而且它应该没有输出或只有一条警告消息。
这里我有两个数据集。第一个,test1.csv, has the key column and the second one, test2.csv,没有。
我的代码如下所示:
library(shiny)
ui <- fluidPage(
titlePanel("Uploading Files"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose file to upload',
accept = c(
'text/csv',
'text/comma-separated-values',
'text/tab-separated-values',
'text/plain',
'.csv',
'.tsv'
)
),
tags$hr(),
checkboxInput('header', 'Header', TRUE),
radioButtons('sep', 'Separator',
c(Comma=',',
Semicolon=';',
Tab='\t'),
';'),
radioButtons('quote', 'Quote',
c(None='',
'Double Quote'='"',
'Single Quote'="'"),
''),
tags$hr(),
p('If you want a sample .csv or .tsv file to upload,',
'you can first download the sample',
a(href = 'mtcars.csv', 'mtcars.csv'), 'or',
a(href = 'pressure.tsv', 'pressure.tsv'),
'files, and then try uploading them.'
)
),
mainPanel(
DT::dataTableOutput('contents')
)
)
)
server <- function(input, output) {
arq <- reactiveValues(data = NULL)
observeEvent(input$file1,{
req(input$file1)
arq$data <- data.table::fread(input$file1$datapath,
header = input$header,
sep = input$sep,
quote = input$quote,
key="SPECIAL")
})
observeEvent(input$file1,{
output$contents = DT::renderDataTable(arq$data)
})
}
# Run the application
shinyApp(ui = ui, server = server)
你可以在读取数据时跳过key
参数,然后用data.table::setkey
设置密钥。
observeEvent(input$file1,{
req(input$file1)
dat <- data.table::fread(input$file1$datapath, header = input$header,
sep = input$sep, quote = input$quote)
if ("SPECIAL" %in% names(data)){
data.table::setkey(dat, "SPECIAL")
arq$data <- dat
} else {
showModal(modalDialog("Column 'SPEACIAL' is missing"))
arq$data <- NULL
}
})
如何从 data.table::fread 函数传递 "key" 参数而不会在上传的数据集没有该特定列名时闪亮崩溃?换句话说,如果我上传的数据集没有 "key" 列,它不会崩溃,而且它应该没有输出或只有一条警告消息。
这里我有两个数据集。第一个,test1.csv, has the key column and the second one, test2.csv,没有。
我的代码如下所示:
library(shiny)
ui <- fluidPage(
titlePanel("Uploading Files"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose file to upload',
accept = c(
'text/csv',
'text/comma-separated-values',
'text/tab-separated-values',
'text/plain',
'.csv',
'.tsv'
)
),
tags$hr(),
checkboxInput('header', 'Header', TRUE),
radioButtons('sep', 'Separator',
c(Comma=',',
Semicolon=';',
Tab='\t'),
';'),
radioButtons('quote', 'Quote',
c(None='',
'Double Quote'='"',
'Single Quote'="'"),
''),
tags$hr(),
p('If you want a sample .csv or .tsv file to upload,',
'you can first download the sample',
a(href = 'mtcars.csv', 'mtcars.csv'), 'or',
a(href = 'pressure.tsv', 'pressure.tsv'),
'files, and then try uploading them.'
)
),
mainPanel(
DT::dataTableOutput('contents')
)
)
)
server <- function(input, output) {
arq <- reactiveValues(data = NULL)
observeEvent(input$file1,{
req(input$file1)
arq$data <- data.table::fread(input$file1$datapath,
header = input$header,
sep = input$sep,
quote = input$quote,
key="SPECIAL")
})
observeEvent(input$file1,{
output$contents = DT::renderDataTable(arq$data)
})
}
# Run the application
shinyApp(ui = ui, server = server)
你可以在读取数据时跳过key
参数,然后用data.table::setkey
设置密钥。
observeEvent(input$file1,{
req(input$file1)
dat <- data.table::fread(input$file1$datapath, header = input$header,
sep = input$sep, quote = input$quote)
if ("SPECIAL" %in% names(data)){
data.table::setkey(dat, "SPECIAL")
arq$data <- dat
} else {
showModal(modalDialog("Column 'SPEACIAL' is missing"))
arq$data <- NULL
}
})