Suppose we have the following data frame called data, produced by the code immediately beneath:
> data
ID Period Values
1 1 1 5
2 1 2 10
3 1 3 15
4 2 1 12
5 2 2 0
6 2 3 2
7 3 1 4
8 3 2 -8
9 3 3 3
data <-
data.frame(
ID = (c(1,1,1,2,2,2,3,3,3)),
Period = as.numeric(c(1, 2, 3, 1, 2, 3, 1, 2, 3)),
Values = as.numeric(c(5, 10, 15, 12, 0, 2, 4, -8, 3))
)
Next, we use the below simple code in base R to copy and paste data into an Excel sheet:
write.table(x = data,
file = "clipboard",
sep = "\t",
row.names = FALSE,
col.names = TRUE
)
When copy/pasting data into Excel, I'd like a row of text describing the table inserted immediately above the table (such as: Table name is "Data"), as shown in the image below.
How can the above code be modified to insert a text row above the table? In base R preferably?

Clipboard alone
... and then paste into Excel. I've run into issues in the past when the data has embedding issues (embedded tabs, etc) and perhaps something in the chain (including "me") did not handle all things correctly.
On windows, one could replace
writeLines(.., "clipboard")withwriteClipboard, but that function is windows only. On other OSes, one can install thecliprpackage for clipboard reading/writing.Using files
(One cannot use
write.csv, since it does not tolerateappend=TRUE, complainingattempt to set 'append' ignored.)Resulting file:
It opens in Excel as