I wish to write a data.frame
from one R
file to another R
file using base R
. So far I have tried cat
, capture.output
, write.table
and sink
. I found suggested solutions for capture.output
and write.table
here:
writing a data.frame using cat
However, I have not been able to obtain an ideal solution. write.table
comes the closest but returns an unwanted warning
message.
Here is the data.frame
in the source R
file:
my.df <- data.frame(scenario = 3333,
AA = 200,
BB = 999,
CC = 444,
DD = 7)
Here is the desired appearance in the recipient R
file except I want the name to be my.df
, not desired.format
:
desired.format <- read.table(text = '
scenario AA BB CC DD
3333 200 999 444 7
', header = TRUE, stringsAsFactors = FALSE)
Here is the full code for the source R
file except for the setwd()
statement:
R.file <- 'my_R_file.R'
cat(' ' , file = R.file, sep=c("\n") )
cat('This is my stuff' , file = R.file, sep=c("\n"), append = TRUE)
cat('#' , file = R.file, sep=c("\n"), append = TRUE)
cat(' ' , file = R.file, sep=c("\n"), append = TRUE)
my.df <- data.frame(scenario = 3333,
AA = 200,
BB = 999,
CC = 444,
DD = 7)
str(my.df)
# Desired format in my_R_file.R
desired.format <- read.table(text = '
scenario AA BB CC DD
3333 200 999 444 7
', header = TRUE, stringsAsFactors = FALSE)
str(desired.format)
# capture.output includes an unwanted row number
cat(' ' , file = R.file, sep=c("\n"), append = TRUE)
cat('capture.output' , file = R.file, sep=c("\n"), append = TRUE)
capture.output(my.df, file = R.file, append = TRUE)
cat(' ' , file = R.file, sep=c("\n"), append = TRUE)
# write.table returns an unwanted warning message
cat('write.table' , file = R.file, sep=c("\n"), append = TRUE)
cat('my.df <- read.table(text = \'' , file = R.file, sep=c("\n"), append = TRUE)
write.table(my.df, file = R.file, col.names = TRUE, row.names = FALSE, quote = FALSE, append=TRUE)
cat('\', header = TRUE, stringsAsFactors = FALSE)' , file = R.file, sep=c("\n"), append = TRUE)
cat(' ' , file = R.file, sep=c("\n"), append = TRUE)
# sink does not return any useful output
#cat('sink' , file = R.file, sep=c("\n"), append = TRUE)
#sink(R.file)
#sink()
#my.df
#sink()
#cat(' ' , file = R.file, sep=c("\n"), append = TRUE)
cat('This is the end' , file = R.file, sep=c("\n"), append = TRUE)
cat(' ' , file = R.file, sep=c("\n"), append = TRUE)
Here are the full contents of the recipient R
file my_R_file.R
:
This is my stuff
#
capture.output
scenario AA BB CC DD
1 3333 200 999 444 7
write.table
my.df <- read.table(text = '
scenario AA BB CC DD
3333 200 999 444 7
', header = TRUE, stringsAsFactors = FALSE)
This is the end
Here is the warning
message returned by write.table
:
Warning message:
In write.table(my.df, file = R.file, col.names = TRUE, row.names = FALSE, :
appending column names to file
Thank you for any suggestions on eliminating this warning
message or arriving at a better solution. I would rather not suppress all warning
messages.
CodePudding user response:
Notice that there is base:::print.data.frame
method involved when evaluating my.df
which is of class
"data.frame"
. It has arguments such as row.names=
. Accordingly you may specify:
capture.output(print(my.df, row.names=FALSE), file=R.file, append=TRUE)