6

What is the best way to write to a file some text followed by a data frame? The text is created by pasting variables into strings.

Example desired output:

Here is some text. This line has a variable: Hello World Data frame below the line ================= ID,val1,val2 1,2,3 2,4,6 3,6,9 4,8,12 5,10,15 6,12,18 7,14,21 8,16,24 9,18,27 10,20,30 

I can create a string with the initial text:

myvar <- "Hello World" out_string <- paste0("Here is some text.\n", "This line has a variable: ", myvar, "\n", "Data frame below the line\n", "=================\n") cat(out_string) 

And I can write a dataframe to file:

library(data.table) mydf <- data.frame(ID = 1:10, val1 = 1:10*2, val2 = 1:10*3) fwrite(x = mydf, file = "path/file.txt", sep = ",", col.names=T) 

But I'm not sure how best to combine these two.

I would think just pasting the data frame onto the end of the out_string then writing that to file would be best, but my attempts have failed, e.g.

cat(paste0(out_string, mydf, collapse='')) # Here is some text. # This line has a variable: Hello World # Data frame below the line # ================= # 1:10Here is some text. # This line has a variable: Hello World # Data frame below the line # ================= # c(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)Here is some text. # This line has a variable: Hello World # Data frame below the line # ================= # c(3, 6, 9, 12, 15, 18, 21, 24, 27, 30) 

3 Answers 3

7

Probably there are a few ways to do this. A simple one is

cat(out_string, file = '/tmp/test.txt') cat(paste0(colnames(mydf), collapse = ','), file = '/tmp/test.txt', append = T, sep = '\n') cat(apply(mydf,1,paste0, collapse=','), file = '/tmp/test.txt', append = T, sep = '\n') 

and of course, using fwrite:

cat(out_string, file = '/tmp/test.txt') fwrite(x = mydf, file = "/tmp/test.txt", sep = ",", col.names=T, append=T) 
Sign up to request clarification or add additional context in comments.

1 Comment

Both of these methods worked as requested. Thank you!
2

Still another way: sink() will open a connection to a file.

sink("<your_new_file_name>") out_string df sink() 

3 Comments

This should have cat(out_string) in order to render properly. Besides that it nearly works as requested, but the output of mydf has columns aligned with spaces, rather than csv. Still nice if one wants that formatting.
Sorry, I didn't pay attention to the details of the format of your answer. I thought you were asking how to simply write a file that combined the two outputs.
No worries. I wasn't aware of sink and I can see it being quite useful, so I am grateful for your answer.
2

One option is to make a connection, which you can write to with both writeLines and write.csv:

myvar <- "Hello World" out_string <- paste0("Here is some text.\n", "This line has a variable: ", myvar, "\n", "Data frame below the line\n", "=================\n") mydf <- data.frame(ID = 1:10, val1 = 1:10*2, val2 = 1:10*3) my_file <- file('file.csv', 'w') writeLines(out_string, my_file, sep = '') write.csv(mydf, my_file, quote = FALSE, row.names = FALSE) close(my_file) readLines('file.csv') #> [1] "Here is some text." #> [2] "This line has a variable: Hello World" #> [3] "Data frame below the line" #> [4] "=================" #> [5] "ID,val1,val2" #> [6] "1,2,3" #> [7] "2,4,6" #> [8] "3,6,9" #> [9] "4,8,12" #> [10] "5,10,15" #> [11] "6,12,18" #> [12] "7,14,21" #> [13] "8,16,24" #> [14] "9,18,27" #> [15] "10,20,30" 

3 Comments

This method adds a line between the out_string and the mydf writes, and also outputs the column names with quotation marks around them.
@conor Updated to remove the separators inserted by writeLines (out_string already has them) and add the quote = FALSE parameter to write.csv. I tend to use readr::write_csv for better defaults for such, but it is nice to keep it all in base R.
Thanks @alistaire, it fits what was requested now.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.