Closed slowkow closed 1 month ago
Colorout already checks whether the string is colorized by another package: https://github.com/jalvesaq/colorout/blob/master/src/colorout.c#L254
But it seems that R's output does not come in a single string. R breaks it in many pieces, and an ANSI color code might have been output in a previous string and still be active. So, I will not be surprised if colorout interferes in the colorizing made by other packages, but it should not mess with line breaks.
Perhaps the problem is here: https://github.com/jalvesaq/colorout/blob/master/src/colorout.c#L261
Maybe a workaround: define a hook for every call of the problematic function (in your case the pkg_install
).
I tried with the devtools::install_github
function. From my .Rprofile
:
# modify a function from a package as soon as the package is loaded
setHook(hookName=packageEvent(pkgname="devtools", event="attach"),
value=function(...) {
# save the function to be overwritten
original_install_github <- devtools::install_github
# define new function
my_install_github <- function(...) {
# first detach colorout if loaded
if (any(search() == "package:colorout")) {
message("setHook: detach colorout package ... ", appendLF=F)
detach(package:colorout, unload=T)
if (!any(search() == "package:colorout")) {
message("SUCCESS")
} else {
message("FAILED")
}
}
# run the original function
message("setHook: now run devtools::install_github ...")
original_install_github(...)
# reload colorout package
message("setHook: reload colorout package ... ", appendLF=F)
suppressMessages(suppressWarnings(require("colorout", character.only=T)))
if (any(search() == "package:colorout")) {
message("SUCCESS")
} else {
message("FAILED")
}
}
# overwrite the original function of the package
unlockBinding("install_github", as.environment("package:devtools"))
assign("install_github", my_install_github, "package:devtools")
} # hook function
) # setHook
Now, within R, there are 2 install_github
functions:
> getAnywhere(install_github)
2 differing objects matching ‘install_github’ were found
in the following places
package:devtools
namespace:remotes
Use [] to view one of them
The original function is unchanged and interferes with the colorout
package in an undesired way:
> any(search() == "package:colorout")
[1] TRUE
> devtools::install_github('rstudio/bookdown', force=T)
Downloading GitHub repo rstudio/bookdown@master
32m✔39m 38;5;247mchecking for file ‘/tmp/RtmpbU5ARO/remotes3da26e4a1da2/rstudio-bookdown-44af2c0/DESCRIPTION’39m36m39m
38;5;247m─39m38;5;247m 39m38;5;247mpreparing ‘bookdown’:39m36m39m
32m✔39m 38;5;247mchecking DESCRIPTION meta-information39m36m39m
38;5;247m─39m38;5;247m 39m38;5;247mchecking for LF line-endings in source and make files and shell scripts39m36m39m
38;5;247m─39m38;5;247m 39m38;5;247mchecking for empty or unneeded directories39m36m39m
38;5;247m─39m38;5;247m 39m38;5;247mbuilding ‘bookdown_0.9.1.tar.gz’39m36m39m
...
Through the hook, however, we can use our own function instead:
> any(search() == "package:colorout")
[1] TRUE
> install_github('rstudio/bookdown', force=T)
setHook: detach colorout package ... SUCCESS
setHook: now run devtools:install_github ...
Downloading GitHub repo rstudio/bookdown@master
✔ checking for file ‘/tmp/RtmpbU5ARO/remotes3da239ca9904/rstudio-bookdown-44af2c0/DESCRIPTION’ ...
─ preparing ‘bookdown’:
✔ checking DESCRIPTION meta-information ...
─ checking for LF line-endings in source and make files and shell scripts
─ checking for empty or unneeded directories
─ building ‘bookdown_0.9.1.tar.gz’
...
setHook: reload colorout package ... SUCCESS
Maybe this helps as a workaround? Good luck with your thesis ...
You may call the function noColorOut()
to disable the colorizing of R output, and ColorOut()
to enable it again. This will be faster than detaching colorout and attaching it again.
Good point ty!
I guess this bug is finally fixed on the branch fix_nl
.
When the output already had colors, it was always printed on stdout
.
colorout is interfering with the output from pkg, so line breaks are not appearing as they should.
I think I should stop using colorout now that crayon is gaining more usage.
With
library(colorout)
in my~/.Rprofile
:Without colorout:
Details
iTerm2 Build 3.2.7