I have two Windows machines. Both Windows 10. When I run the following code on each, I get two very different outputs:
library(tibble)
sink(file.path(tempdir(), "test.log"), append = FALSE)
print(as_tibble(mtcars))
sink()
One machine gives me the expected output:
The other machine gives me the same tibble data, but appears to be printing all the crayon color codes:
At first I figured it had to be a difference in the version of R or the version of crayon. But I've now synchronized both on R 4.0.3 and crayon 1.3.4. Tibble version is 3.0.4 on both. Not sure what to check next. The second machine initially had later versions of everything. But I reverted back as much as possible, and I'm still getting the color codes.
Anyone have any suggestions on how to diagnose this?
The colors are controlled by the crayon package. You can query that package for how many colors are supported, e.g.
There are different ways to configure the number of colors. I'd say, setting an environment variable is the best because it will also used in parallel processing, if you even end up using that. So,
If you set it to one color, then you'll get:
Now, key here is when there's only one color, there is no ANSI color escape codes produced. Thus, depending on whether or not the current R environment supports colors or not (
crayon::has_color()
), you'll get ANSI escape codes.Since you probably only want to limit the number of colors when you're
sink()
:ing, you don't want to set permanently. The easiest is to use withr for this, e.g.That should do it for you.