如何在R中导出/读取空数据帧?

如何在R中导出/读取空数据帧?,r,csv,shiny,read.csv,R,Csv,Shiny,Read.csv,我正在尝试使用read.csv和write.csv将数据帧导出/导入为csv文件,但当data.frame为空时,我似乎无法处理这种情况。通常,如果我的data.frame是空的(即它有列但没有行名),write.csv会创建一个空的*.csv文件,当我尝试使用read.csv读取它时,会出现错误。有解决办法吗 编辑: 我收到的错误消息是: 我收到以下错误消息: Error in read.table(file = file, header = header, sep = sep, quote

我正在尝试使用
read.csv
write.csv
将数据帧导出/导入为csv文件,但当
data.frame
为空时,我似乎无法处理这种情况。通常,如果我的
data.frame
是空的(即它有列但没有行名),
write.csv
会创建一个空的*.csv文件,当我尝试使用
read.csv
读取它时,会出现错误。有解决办法吗

编辑:

我收到的错误消息是:

我收到以下错误消息:

Error in read.table(file = file, header = header, sep = sep, quote = 
quote,  : 
les premières cinq lignes sont vides : abandon
也就是说:前5行是空的:放弃

我的代码行是:

write.csv(DF.old,file="DF_old.csv",row.names=FALSE)
DF.old<-read.csv(file="DF_old.csv")
write.csv(DF.old,file=“DF_old.csv”,row.names=FALSE)

DF.old
dat
dat这至少适用于
数据表

> library(data.table)
> 
> #generate a data table with column name and no rows
> DT <- data.table(column1="value")[column1!="value"]
> 
> #print the data table
> DT
Empty data.table (0 rows) of 1 col: column1
> 
> #write the data table
> fwrite(DT, "test.csv")
> 
> #read the data table
> fread("test.csv")
Empty data.table (0 rows) of 1 col: column1
>库(data.table)
> 
>#生成一个列名为且没有行的数据表
>DT
>#打印数据表
>DT
空data.table(0行),共1列:column1
> 
>#编写数据表
>fwrite(DT,“test.csv”)
> 
>#读取数据表
>fread(“test.csv”)
空data.table(0行),共1列:column1

因此,当使用
fwrite
写入并使用
fread
读回时,不会出现错误,列名称也会保留下来。

这至少适用于
数据。table

> library(data.table)
> 
> #generate a data table with column name and no rows
> DT <- data.table(column1="value")[column1!="value"]
> 
> #print the data table
> DT
Empty data.table (0 rows) of 1 col: column1
> 
> #write the data table
> fwrite(DT, "test.csv")
> 
> #read the data table
> fread("test.csv")
Empty data.table (0 rows) of 1 col: column1
>库(data.table)
> 
>#生成一个列名为且没有行的数据表
>DT
>#打印数据表
>DT
空data.table(0行),共1列:column1
> 
>#编写数据表
>fwrite(DT,“test.csv”)
> 
>#读取数据表
>fread(“test.csv”)
空data.table(0行),共1列:column1

因此,当使用
fwrite
写入并使用
fread
读回时,不会出现错误,列名称也会保留。

当处理0列和0行的空数据帧时,
fwrite
来自数据。表失败。但是,
write_csv
fromreadr成功创建了一个空的csv文件

library(readr) # or library(tidyverse)
df <- data.frame()
write_csv(df, "~/empty_df.csv")
# fwrite does not work
library(readr)#或library(tidyverse)

df处理0列和0行的空数据帧时,
fwrite
fromdata.table失败。但是,
write_csv
fromreadr成功创建了一个空的csv文件

library(readr) # or library(tidyverse)
df <- data.frame()
write_csv(df, "~/empty_df.csv")
# fwrite does not work
library(readr)#或library(tidyverse)

df这真的是一个错误还是一个警告?请发布发生的消息。为什么要尝试写入空数据帧?我无法复制此消息。投票结束作为离题。这真的是一个错误还是一个警告?请发布发生的消息。为什么要尝试写入空数据帧?我无法复制此消息。投票结束作为离题。你能发布生成DF.old的脚本吗?或者提供更多信息,如类类型、维度等。我已经更新了代码。我无法重现你的问题。您是否尝试过
rm(list=ls())
、重新启动R会话或其他操作?你试过运行我和“anotherfred”发布的代码并找到任何东西吗?你的两个代码都很好。。。也许区别在于我的是一个闪亮的应用程序的一部分?你能发布生成DF.old的脚本吗?或者提供更多信息,如类类型、维度等。我已经更新了代码。我无法重现你的问题。您是否尝试过
rm(list=ls())
、重新启动R会话或其他操作?你试过运行我和“anotherfred”发布的代码并找到任何东西吗?你的两个代码都很好。。。也许区别在于我的是一个闪亮应用的一部分?