R Shiny - 上传多个 csv 在本地有效,但在 shinyapps.io 上无效
R Shiny - uploading multiple csv works locally but not on shinyapps.io
我制作了一个允许用户上传多个 csv 文件的应用程序。
然后将这些 csvs 'rbind' 放在一起,'read.csv' 并在 df 中添加一列,即文件名。
然后处理 df 以生成可下载的各种图。这在本地完美运行,但在部署时却不行。我用下面的代码复制了错误:
文件中的警告(文件,"rt"):无法打开文件“*.csv”:没有这样的文件或目录
警告:文件错误:无法打开连接
UI:
dashboardPage( skin = "black",
dashboardHeader(title = "myApp"),
dashboardSidebar(collapsed = TRUE,
sidebarMenu(
menuItem("Home", tabName = "dashboard1", icon = icon("home", lib =
"glyphicon"))
)
),
dashboardBody(
tags$head(tags$style(HTML('
.main-header .logo {
font-family: "Times New Roman", serif;
font-weight: bold;
font-size: 24px;
}
'))),
tabItems(
tabItem(tabName = "dashboard1",
fileInput("file1",
label="Input files:",
multiple = TRUE),
downloadButton('plot.pdf', 'Download Data')
)
)
)
)
服务器:
library(shiny)
library(shinydashboard)
#server start
function(input, output) {
testinput<- reactive({
if(is.null(input$file1))
return ()
else
{
nfiles = nrow(input$file1)
csv = list()
for (i in 1 : nfiles)
{
csv[[i]] = read.csv(input$file1$datapath[i])
}
csv_names <- input$file1[['name']]
mydata <- do.call(rbind, lapply(csv_names, function(x) cbind(read.csv(x), name=strsplit(x,'\.')[[1]][1])))
View(mydata)
}
})
output$plot.pdf <- downloadHandler(
filename = function() {
"plot.pdf"
},
content = function(file) {
withProgress(message = 'Your file is downloading',
detail = 'This may take a minute or two...', value = 0, {
for (i in 1:10) {
incProgress(1/10)
Sys.sleep(0.5)}
pdf(file)
print(testinput())
dev.off()
})
}
)
}
任何帮助将不胜感激。我搜索了大量的 SO 和其他论坛,但我真的被困住了。
请帮忙
你不应该在你的 server
中使用 csv_names <- input$file1[['name']]
,这只是 return 文件名,而不是文件路径,所以当你使用 read.csv
读取 csv文件,发生错误。
以下应该可以正常工作。
ui
library(shiny)
library(shinydashboard)
dashboardPage( skin = "black",
dashboardHeader(title = "myApp"),
dashboardSidebar(collapsed = TRUE,
sidebarMenu(
menuItem("Home", tabName = "dashboard1", icon = icon("home", lib =
"glyphicon"))
)
),
dashboardBody(
tags$head(tags$style(HTML('
.main-header .logo {
font-family: "Times New Roman", serif;
font-weight: bold;
font-size: 24px;
}
'))),
tabItems(
tabItem(tabName = "dashboard1",
fileInput("file1",
label="Input files:",
multiple = TRUE),
downloadButton('plot.pdf', 'Download Data')
)
)
)
)
服务器
library(shiny)
library(shinydashboard)
#server start
function(input, output) {
testinput<- reactive({
if(is.null(input$file1))
return ()
else
{
nfiles = nrow(input$file1)
csv = list()
for (i in 1 : nfiles)
{
csv[[i]] = read.csv(input$file1$datapath[i])
}
csv_names <- input$file1$datapath
mydata <- do.call(rbind, lapply(csv_names, function(x) cbind(read.csv(x), name=strsplit(basename(x),'\.')[[1]][1])))
mydata
}
})
output$plot.pdf <- downloadHandler(
filename = function() {
"plot.pdf"
},
content = function(file) {
withProgress(message = 'Your file is downloading',
detail = 'This may take a minute or two...', value = 0, {
for (i in 1:10) {
incProgress(1/10)
Sys.sleep(0.5)}
pdf(file)
plot(testinput()[,1])
dev.off()
})
}
)
}
我制作了一个允许用户上传多个 csv 文件的应用程序。
然后将这些 csvs 'rbind' 放在一起,'read.csv' 并在 df 中添加一列,即文件名。
然后处理 df 以生成可下载的各种图。这在本地完美运行,但在部署时却不行。我用下面的代码复制了错误:
文件中的警告(文件,"rt"):无法打开文件“*.csv”:没有这样的文件或目录
警告:文件错误:无法打开连接
UI:
dashboardPage( skin = "black",
dashboardHeader(title = "myApp"),
dashboardSidebar(collapsed = TRUE,
sidebarMenu(
menuItem("Home", tabName = "dashboard1", icon = icon("home", lib =
"glyphicon"))
)
),
dashboardBody(
tags$head(tags$style(HTML('
.main-header .logo {
font-family: "Times New Roman", serif;
font-weight: bold;
font-size: 24px;
}
'))),
tabItems(
tabItem(tabName = "dashboard1",
fileInput("file1",
label="Input files:",
multiple = TRUE),
downloadButton('plot.pdf', 'Download Data')
)
)
)
)
服务器:
library(shiny)
library(shinydashboard)
#server start
function(input, output) {
testinput<- reactive({
if(is.null(input$file1))
return ()
else
{
nfiles = nrow(input$file1)
csv = list()
for (i in 1 : nfiles)
{
csv[[i]] = read.csv(input$file1$datapath[i])
}
csv_names <- input$file1[['name']]
mydata <- do.call(rbind, lapply(csv_names, function(x) cbind(read.csv(x), name=strsplit(x,'\.')[[1]][1])))
View(mydata)
}
})
output$plot.pdf <- downloadHandler(
filename = function() {
"plot.pdf"
},
content = function(file) {
withProgress(message = 'Your file is downloading',
detail = 'This may take a minute or two...', value = 0, {
for (i in 1:10) {
incProgress(1/10)
Sys.sleep(0.5)}
pdf(file)
print(testinput())
dev.off()
})
}
)
}
任何帮助将不胜感激。我搜索了大量的 SO 和其他论坛,但我真的被困住了。
请帮忙
你不应该在你的 server
中使用 csv_names <- input$file1[['name']]
,这只是 return 文件名,而不是文件路径,所以当你使用 read.csv
读取 csv文件,发生错误。
以下应该可以正常工作。
ui
library(shiny)
library(shinydashboard)
dashboardPage( skin = "black",
dashboardHeader(title = "myApp"),
dashboardSidebar(collapsed = TRUE,
sidebarMenu(
menuItem("Home", tabName = "dashboard1", icon = icon("home", lib =
"glyphicon"))
)
),
dashboardBody(
tags$head(tags$style(HTML('
.main-header .logo {
font-family: "Times New Roman", serif;
font-weight: bold;
font-size: 24px;
}
'))),
tabItems(
tabItem(tabName = "dashboard1",
fileInput("file1",
label="Input files:",
multiple = TRUE),
downloadButton('plot.pdf', 'Download Data')
)
)
)
)
服务器
library(shiny)
library(shinydashboard)
#server start
function(input, output) {
testinput<- reactive({
if(is.null(input$file1))
return ()
else
{
nfiles = nrow(input$file1)
csv = list()
for (i in 1 : nfiles)
{
csv[[i]] = read.csv(input$file1$datapath[i])
}
csv_names <- input$file1$datapath
mydata <- do.call(rbind, lapply(csv_names, function(x) cbind(read.csv(x), name=strsplit(basename(x),'\.')[[1]][1])))
mydata
}
})
output$plot.pdf <- downloadHandler(
filename = function() {
"plot.pdf"
},
content = function(file) {
withProgress(message = 'Your file is downloading',
detail = 'This may take a minute or two...', value = 0, {
for (i in 1:10) {
incProgress(1/10)
Sys.sleep(0.5)}
pdf(file)
plot(testinput()[,1])
dev.off()
})
}
)
}