Reputation: 1565
Suppose I have some analysis results in data frame/table. I wish to embed them in text document.
In case of LaTeX, I can use miscFuncs package:
library('miscFuncs')
latextable(myDataFrame)
and simply copy output displayed in R console and paste it to LaTeX document.
What if I wish to copy from R! output and paste to LibreOffice Writer or MS Word documents?
I think that most (?) convenient way is to generate output as tab-delimited text. After pasting text, just convert them to table.
I know that there are functions which writes output to tab-delimited files: http://www.instantr.com/2012/12/11/exporting-a-dataset-from-r/
so I guess there must be function/package which simply generate tab-delimited content?
Note - there was similar question: https://stats.stackexchange.com/questions/90184/can-i-copy-output-from-r-to-word-or-excel
but answers suggest to export content to delimited file. I wish to avoid this step.
Upvotes: 1
Views: 3248
Reputation: 149
If you want to copy an R data frame to Writer or Word, you can follow these steps:
Convert the data frame to a data.
data_df <- as.data.frame(data)
Save the data as a CSV file:
write.csv(data_df[,6:7], "clipboard", row.names = FALSE, quote = FALSE)
This here selected column 6 and 7.
Upvotes: 0
Reputation: 2977
If you do not mind using a intermediate file to save your table, you could use functions from the package flextable
(https://davidgohel.github.io/flextable/index.html).
Repex:
library(flextable)
# create a flextable object
ft <- flextable::flextable(data = head(iris))
# save it as a .docx file on your disk
flextable::save_as_docx(ft, path = "./test.docx")
# or pop-up a word window with your table
print(ft, preview ="docx") # from https://github.com/davidgohel/flextable/issues/117#issuecomment-487126438
Output:
Now you can copy/paste this table inside your main document. Note that you can do a lot of customization with flextable
and save your table in different output format like HTML or even .pptx.
Upvotes: 0
Reputation: 120
If it is a data.frame, to me the easiest way is using kable()
in knitr
package, then copy-paste from console to spreadsheet
library(dplyr) # just to use the handy %>%
matrix(c(1:20), 5) %>% # some data
as.data.frame() %>% # as it is a matrix, I need it as data.frame
knitr::kable() # It is now tabulated, you can use in your Office software "convert text to table"
I you don't like the %>%,
knitr::kable(
as.data.frame(
matrix(c(1:20), 5)
))
Upvotes: 1
Reputation: 8252
One very simple way is to simply use a monospace font, such a Courier New or Lucida Console (for a couple of examples) in your word processor -- if it looks okay on screen in your R session, it will look very similar in Libre Office's Writer or Word. A straight copy-paste from the R console to the document works fine once it's in a suitable font.
If you're trying to "reproduce the look" of the R session, this works quite well; if you're trying to format output for a paper or some-such it may be less suitable, depending on your preferences.
Upvotes: 0