"read_excel" 在 Shiny 应用中
"read_excel" in a Shiny app
我有一个 Shiny 应用程序,它使用包 xlsx
中的 read.xlsx
函数。一切正常,但我想从 readxl
更改为 read_excel
,希望它会更快并且能够处理大文件。
ui 部分:
fileInput("inputFile","Upload file...")
服务器部分:
data <- reactive({
inFile <- input$inputFile
if (is.null(inFile)) { return(NULL) }
dataFile <- read_excel(inFile$datapath,sheet=1)
return(dataFile)
})
我收到 "Unknown format" 错误。
inFile$datapath is "/tmp/.../60974676c7287e913d1c0dc5/0"
inFile$type is "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
问题 1:有没有办法告诉 read_excel
这是一个 xlsx 类型的文件?
问题2:是否可以控制上传文件的存储位置?
这是一个带有 readxl 包的 open issue。当前提供的解决方法是复制文件数据路径并附加 .xlsx
。这是我机器上的一个工作示例,仅限于 .xlsx
文件编辑为使用 file.rename
而不是 file.copy
.
library(shiny)
library(readxl)
runApp(
list(
ui = fluidPage(
titlePanel("Use readxl"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose xlsx file',
accept = c(".xlsx")
)
),
mainPanel(
tableOutput('contents'))
)
),
server = function(input, output){
output$contents <- renderTable({
inFile <- input$file1
if(is.null(inFile))
return(NULL)
file.rename(inFile$datapath,
paste(inFile$datapath, ".xlsx", sep=""))
read_excel(paste(inFile$datapath, ".xlsx", sep=""), 1)
})
}
)
)
编辑
请注意,对于 readxl
的 1.1.0
版本,不再需要重命名文件。以下对我来说现在没有问题。
library(shiny)
library(readxl)
runApp(
list(
ui = fluidPage(
titlePanel("Use readxl"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose xlsx file',
accept = c(".xlsx")
)
),
mainPanel(
tableOutput('contents'))
)
),
server = function(input, output){
output$contents <- renderTable({
req(input$file1)
inFile <- input$file1
read_excel(inFile$datapath, 1)
})
}
)
)
确保用户确实上传了 .xlsx 文件,否则您需要检查自己的扩展名以在读取功能之间切换。您可以按如下方式提取扩展名:
library(shiny)
library(readxl)
runApp(
list(
ui = fluidPage(
titlePanel("Use readxl"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose xlsx file',
accept = c(".xlsx")
)
),
mainPanel(
tableOutput('contents'))
)
),
server = function(input, output){
output$contents <- renderTable({
inFile <- input$file1
if(is.null(inFile))
return(NULL)
ext <- tools::file_ext(inFile$name)
file.rename(inFile$datapath,
paste(inFile$datapath, ext, sep="."))
read_excel(paste(inFile$datapath, ext, sep="."), 1)
})
}
)
)
我有一个 Shiny 应用程序,它使用包 xlsx
中的 read.xlsx
函数。一切正常,但我想从 readxl
更改为 read_excel
,希望它会更快并且能够处理大文件。
ui 部分:
fileInput("inputFile","Upload file...")
服务器部分:
data <- reactive({
inFile <- input$inputFile
if (is.null(inFile)) { return(NULL) }
dataFile <- read_excel(inFile$datapath,sheet=1)
return(dataFile)
})
我收到 "Unknown format" 错误。
inFile$datapath is "/tmp/.../60974676c7287e913d1c0dc5/0"
inFile$type is "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
问题 1:有没有办法告诉 read_excel
这是一个 xlsx 类型的文件?
问题2:是否可以控制上传文件的存储位置?
这是一个带有 readxl 包的 open issue。当前提供的解决方法是复制文件数据路径并附加 .xlsx
。这是我机器上的一个工作示例,仅限于 .xlsx
文件编辑为使用 file.rename
而不是 file.copy
.
library(shiny)
library(readxl)
runApp(
list(
ui = fluidPage(
titlePanel("Use readxl"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose xlsx file',
accept = c(".xlsx")
)
),
mainPanel(
tableOutput('contents'))
)
),
server = function(input, output){
output$contents <- renderTable({
inFile <- input$file1
if(is.null(inFile))
return(NULL)
file.rename(inFile$datapath,
paste(inFile$datapath, ".xlsx", sep=""))
read_excel(paste(inFile$datapath, ".xlsx", sep=""), 1)
})
}
)
)
编辑
请注意,对于 readxl
的 1.1.0
版本,不再需要重命名文件。以下对我来说现在没有问题。
library(shiny)
library(readxl)
runApp(
list(
ui = fluidPage(
titlePanel("Use readxl"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose xlsx file',
accept = c(".xlsx")
)
),
mainPanel(
tableOutput('contents'))
)
),
server = function(input, output){
output$contents <- renderTable({
req(input$file1)
inFile <- input$file1
read_excel(inFile$datapath, 1)
})
}
)
)
确保用户确实上传了 .xlsx 文件,否则您需要检查自己的扩展名以在读取功能之间切换。您可以按如下方式提取扩展名:
library(shiny)
library(readxl)
runApp(
list(
ui = fluidPage(
titlePanel("Use readxl"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose xlsx file',
accept = c(".xlsx")
)
),
mainPanel(
tableOutput('contents'))
)
),
server = function(input, output){
output$contents <- renderTable({
inFile <- input$file1
if(is.null(inFile))
return(NULL)
ext <- tools::file_ext(inFile$name)
file.rename(inFile$datapath,
paste(inFile$datapath, ext, sep="."))
read_excel(paste(inFile$datapath, ext, sep="."), 1)
})
}
)
)