0

I am trying to output some anova results from R into a textfile using the sink() command, but I am getting some weird symbols. This is the code:

sink(file)
print(title)
print("SUMMARY STATISTICS")
print("")
print("Summary grouped by factor ' roi'")
data %>%
  group_by(roi) %>%
  get_summary_stats(value, type = "mean_sd") #value cannot be a factor
print("")
print("Summary grouped by factor 'mA'")
data %>%
  group_by(mA) %>%
  get_summary_stats(value, type = "mean_sd") #value cannot be a factor
print("")
print("Summary grouped by both factors")
data%>%
  group_by(roi,mA) %>%
  get_summary_stats(value, show = c("mean", "sd", "se", "median"))

sink()

... and this the result :(

example of the printed file by sink()

Software details: R studio is Version 1.4.1717 R Version 1.4.1717 OS: Ubuntu 18.04.5 LTS

user438383
  • 5,716
  • 8
  • 28
  • 43
Tamara
  • 21
  • 2

1 Answers1

0

This is how tibble formats data for printing. It uses styled console output with the packages cli and crayon, which allow coloring and other styling with ANSI strings. Those ANSI strings are not compatible with sink().

The easiest solution would be to convert the data into a more simple format like a data.frame for printing. Or you could also render the table into a text format using knitr::kable().

sink(file)
# ...
data %>%
  group_by(roi,mA) %>%
  get_summary_stats(value, show = c("mean", "sd", "se", "median")) %>%
  print.data.frame()  ## or knitr::kable()
sink()

The fact that title is printed as a function is because the symbol was undefined and therefore, the function body of graphics::title() gets displayed.

Gregor de Cillia
  • 7,397
  • 1
  • 26
  • 43
  • Oh, wow, it works perfectly with 'knitr::kable'! Thank you very much! ♥ – Tamara Jul 30 '21 at 12:11
  • Still, I wonder why in my windows version, I don't have this problem with the very same code... – Tamara Jul 30 '21 at 12:12
  • Please accept this answer so the question gets marked as "solved". Or is there something I missed? – Gregor de Cillia Jul 30 '21 at 13:26
  • It is possible that `sink()` or some parts of the `cli` codebase behave differently on Linux and Windows. I personally don't think that it is a good idea to use `sink()` for any production code, but this is an intersting observation. – Gregor de Cillia Jul 30 '21 at 13:30