Cutting results from R - formatting Output for Word/Excel

Im a long time SPSS user learning R. I am becoming familiar with different packages & am having some success running different tests.

One problem I am having is when I try and take results from R (either by cut and pasting from console or output.rft functions) - the output is not formatted meaning it is not possible to create a table in either word or excel. Output from several row tends to merge into one cell & I am struggling to know how to reformat it (all standard reformatting options in word & excel do not worked).

Hope this makes sense & any help in getting my results into programmes like excel& word so I can then create tables would be much appreciated.

Jeff

Can you give some examples of the kind of output you want to transfer to Word or Excel? Is it a data frame/tibble or the summary of a model or something else?

As noted above, an example would be good.

Otherwise, have you tried knitting the output straight to .docx via RMarkdown? Also the officer or officedown packages?

Depending on the type of tests you're trying to do, I'd suggest that you use broom in combination with openxlsx. Then, you would be able to convert tests into a tidy format which could be easily passed to a function that passes a tibble into a .csv or xslx.

Some code:

library(tidyverse)
library(broom)
library(openxlsx)

set.seed(133) # this garantees the example is reproducible

# generating normally distributed vectors

data <- map(1:10, ~rnorm(1000)) # 10 vectors with a 1000 values each

# testing if generated data are normally distributed

tests <- data %>% 
  map(~ks.test(.x, dnorm))

# generating tidy rows for each test

tidy_tests <- map(tests, tidy) %>% 
  bind_rows()

# saving tests into an excel file
write.xlsx(tidy_tests, 'tidy_tests.xlsx')

Your .xlsx file would have an output like this one (I'm coping and pasting from the excel file we generated):

statistic p.value method alternative
0,999746018 0 One-sample Kolmogorov-Smirnov test two-sided
0,998703017 0 One-sample Kolmogorov-Smirnov test two-sided
0,997744841 0 One-sample Kolmogorov-Smirnov test two-sided
0,999707866 0 One-sample Kolmogorov-Smirnov test two-sided
0,999755772 0 One-sample Kolmogorov-Smirnov test two-sided
0,992688239 0 One-sample Kolmogorov-Smirnov test two-sided
0,998981303 0 One-sample Kolmogorov-Smirnov test two-sided
0,998411468 0 One-sample Kolmogorov-Smirnov test two-sided
0,999128399 0 One-sample Kolmogorov-Smirnov test two-sided
0,99744755 0 One-sample Kolmogorov-Smirnov test two-sided

Of couse, it could be the case your test cannot be easily converted into a tidy format. Take a look at broom's documentation to be sure.

There are myriad HTML table building (& formatting) packages in R.

https://github.com/rstudio/gt for one example

You could then copy and paste these outputs.

Thank you so much - all answers have been v helpful for a newbie and opened up lots of new approaches.

@FJCC - I am running logistf (initial reasons for starting to is R). I'm finding alot of functions are not available with logistf.

"Error: No tidy method for objects of class logistf"

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.