print colorized output to R console
print colorized output to R console, inverted
print colorized output to HTML
Usage
printDebug(
...,
fgText = NULL,
fgDefault = getOption("jam.fgDefault", c("darkorange1", "dodgerblue")),
bgText = NULL,
fgTime = getOption("jam.fgTime", "cyan2"),
timeStamp = getOption("jam.timeStamp", TRUE),
comment = getOption("jam.comment", !htmlOut),
formatNumbers = getOption("jam.formatNumbers", TRUE),
trim = getOption("jam.trim", TRUE),
digits = getOption("jam.digits"),
nsmall = getOption("jam.nsmall", 0L),
justify = "left",
big.mark = getOption("jam.big.mark", ","),
small.mark = getOption("jam.small.mark", "."),
zero.print = NULL,
width = NULL,
doColor = getOption("jam.doColor"),
splitComments = FALSE,
collapse = getOption("jam.collapse", ""),
sep = getOption("jam.sep", ","),
doReset = NULL,
detectColors = TRUE,
dex = 2,
darkFactor = c(1, 1.5),
sFactor = c(1, 1.5),
lightMode = checkLightMode(),
Crange = getOption("jam.Crange"),
Lrange = getOption("jam.Lrange"),
removeNA = FALSE,
replaceNULL = NULL,
adjustRgb = getOption("jam.adjustRgb"),
byLine = FALSE,
verbose = FALSE,
indent = "",
keepNA = TRUE,
file = getOption("jam.file", ""),
append = getOption("jam.append", TRUE),
invert = getOption("jam.invert", FALSE),
htmlOut = getOption("jam.htmlOut", FALSE)
)
printDebugI(..., invert = TRUE)
printDebugHtml(..., htmlOut = TRUE, comment = FALSE)Arguments
- ...
character,factor,numericor compatible atomic vectors to be printed to the R console. These arguments are recognized as any un-named argument, or any argument whose name does not match the named arguments below.- fgText
one of two formats to define the foreground color for elements in
...being printed. Each element is colored in order, and when multiple vector values are contained in one...element, the color defined infgTextis extended. The input types recognized:NULLwhen no color is defined, one of two outputs:When all values in
...represent colors, these colors are used to colorize the output text. Whennames()are present they are used as the text labels in place of the vector value.When not all values in
...represent colors, the default color set is used:c("darkorange1", "dodgerblue").To disable option 1 above, define a specific value for
fgText, such asfgText=c("darkorange1", "dodgerblue").
vectorof R compatible colors, recycled to the length of.... When any element of...is a vector with multiple values, the corresponding color infgTextis shaded slightly lighter and darker, then recycled to the vector length, so that adjacent values have slightly different color. This behavior is controlled by default argumentsplitComments=TRUE.listof vectors of R compatible colors, recycled to the length of..., then applied to each element in...in order. When only one color is defined, and multiple values are present in the correspondinglistelement, the color is shaded slightly lighter and darker, then recycled to the vector length, as described above. This behavior is controlled by default argumentsplitComments=TRUE. When multiple colors are defined for thelistelement, these values are recycled to the vector length.Note: When
invert=TRUEthe values forfgTextandbgTextare reversed, and if the resultingfgTextisNULLthen its color is defined bysetTextContrastColor()in order to define a contrasting text color.
- fgDefault
characterdefaults togetOption("jam.fgDefault", c("darkorange1", "dodgerblue")), and is used when colors are not defined byfgTextor by the input...values.- bgText
vectorof R colors, orlistof vectors, used to define the background color, using the same approach described forfgText. Note thatNULLorNAdefines the absence of any background color, which is default. Wheninvert=TRUE, which is default forprintDebugI(), the values forfgTextandbgTextare reversed.- fgTime
characterR color to colorize the time- timeStamp
logicalwhether to include a time stamp in output- comment
logicalwhether to prefix output with '## ' as a comment, orcharacterstring used as a prefix.- formatNumbers
logicalwhether to format numbers usingformat()which controls the number of digits displayed, and is default. WhenformatNumbers=FALSEsometimesnumericvalues that containintegersmay be represented as14.0000000001.- trim, digits, nsmall, justify, big.mark, small.mark, zero.print, width
arguments passed to
format().- doColor
logicalorNULLindicating whether to colorize output. WhendoColorisNULL, if the"crayon"package is available, and if crayon detects color is permitted, color is enabled.- splitComments
logicalwhether to color each element independently without light-dark alternating pattern. The intensity of the adjustment is controlled bydexpassed tocolor2gradient().- collapse
charactercollapse string used to separate list items, by default "" so text separation is expected in the input data.- sep
characterseparator used to separate vector elements, when a list items contains a vector.- doReset
logicalorNULL, indicating whether to applycrayon::reset()to the delimitersep. WhendoReset=TRUEthe style on the delimiter is forced to reset, usingcrayon::reset(), or to remove pre-existing style withcrayon::strip_style(). WhendoReset=NULLandsepcontains ANSI escape characters, they are left as-is; whendoReset=NULLandsepdoes not contain ANSI escape characters,sepbecomescrayon::reset(sep)which forces the style to be reset between printed values.- detectColors
logicalwhether to detect and potentially try to correct console color capabilities.- dex
numericpassed tocolor2gradient()to split a color into a lighter,darker alternating pattern. Until version 0.0.83.900, this process usedgradientWtFactor=1and was not adjustable. Note that whensplitComments=TRUEthe input values in...are flattened to a single vector, and colors infgTextare applied directly without adjustment.- darkFactor, sFactor
numericarguments deprecated.- lightMode
logicalor NULL, indicating whether the text background color is light, wherelightMode=TRUEindicates the background is white or light enough to require darker text, imposing a maximum brightness for colors displayed. WhenNULLit callscheckLightMode(), which uses:getOption("jam.lightMode")if definedotherwise attempts to detect whether the session is running inside RStudio, by checking for environmental variable
"RSTUDIO", under the assumption that default RStudio uses a light background, thereforelightMode=TRUE.if steps above fail, it uses
lightMode=FALSE.to force a specific lightMode for all uses, use options:
options(jam.lightMode=TRUE)oroptions(jam.lightMode=FALSE).
- Crange, Lrange
numericrange of chroma and luminance values between 0 and 100. When NULL, default values are assigned bysetCLranges(). The intent is to restrict the range relative to the console background color, also controlled bylightMode.- removeNA
logicalwhether to remove NA values and not print to the console.- replaceNULL
characteror NULL, optionally replace NULL elements with non-NULL character value, otherwise NULL elements are ignored.- adjustRgb
numericvalue adjustment used during the conversion of RGB colors to ANSI colors, which is inherently lossy. If not defined, it uses the default returned bysetCLranges()which itself usesgetOption("jam.adjustRgb")with default=0. In order to boost color contrast, an alternate value of -0.1 is suggested.- byLine
logicalwhether to delimit lists by line instead of using collapse to combine them onto one line.- verbose
logicalwhether to print verbose output- indent
characteroptional characters used as a prefix to indent output. Whennumericit is rounded to integer, then this many character spaces" "are concatenated together to define the indent width. Note that theindenttext is not colorized.- keepNA
logical, default TRUE, whether to keep and print NA values.- file
argument passed to
cat()to send output to a file or compatible output ofcat().- append
logicalwhether to append output, passed tocat()whenfileis defined.- invert
logicalindicating whether foreground and background colors should be switched, as is default forprintDebugI(). Note when the resultingfgTextisNULL, its color is defined bysetTextContrastColor()to define a contrasting text color relative to the background color inbgText.- htmlOut
logicalindicating whether to print HTML span output, using format<span style="color:fg;background-color:bg">text</span>. This argument is not yet implemented, more testing is required to determine the best mechanism to use for things like 'Rmarkdown' rendering, and R-shiny app rendering.
Value
NULL invisibly, this function is called for the side effect
of printing output using cat().
NULL invisibly, this function is called for the side effect
of printing output using cat().
NULL invisibly, this function is called for the side effect
of printing output using cat().
Details
This function prints colorized output to the R console, with some rules for colorizing the output to help visually distinguish items.
The main intent is to use this function to print pretty debug messages, because color helps identify.
By default, output has the following configurable properties:
each line begins with a comment, controlled by default
comment=getOption("jam.comment", TRUE)which by default uses"##", but which can be defined to use a different prefix, orFALSEfor no prefix at all.each line includes time and date stamp controlled by
timeStamp=getOption("jam.timeStamp", TRUE)which by default includes the current time and date.each line formats
numericvalues, controlled byformatNumbers=getOption("jam.formatNumbers", TRUE), which determines whether to apply argumentsbig.markandsmall.markto make numeric values more readable.each entry in
...is printed with its own foreground colorfgText, background colorbgText, with a slight lighter/darker dithering effect to add minor visual distinction for multiple values.Values in each
vectorare concatenated bysep=","by default.Each
listis concatenated bycollapse=""by default.
Additional convenience rules:
For convenience, when the last
...argument is acharactervector of colors, it is assumed to befgText.When the only entry in
...is acharactervector of R colors, the names are printed using the color vector forfgText, or if no names exist the colors are printed using the color vector forfgText.For
printDebugI()orinvert=TRUE, colors typically assigned tofgTextare instead assigned tobgText.For very specific color assignments,
fgTextand/orbgTextcan be defined as alistofcharactervectors of R colors, in which case thelistoverall is recycled to the length...to be printed, and within each vector of...printed the corresponding color vector is recycled to the length of that vector.
For use inside 'Rmarkdown' .Rmd documents, current recommendation is
to define the R output with results='asis' like this:
Then define a global option to turn off the comment prefix in
printDebug(): options("jam.comment"=FALSE)
For colorized text, it may require "html_output" rendering of the
.Rmd 'Rmarkdown' file, as well as this option to enable HTML formatting
by printDebug(): options("jam.htmlOut"=TRUE).
This function prints colorized output to the R console, using the
same logic as printDebug except by default the color is inverted
so the default fgText colors are applied to the background.
This function prints colorized output in HTML form, using the
same logic as printDebug() except by default the output is HTML.
The intended use is for 'Rmarkdown' with chunk option results='asis',
which causes the HTML code to be interpreted directly as HTML.
This function internally calls printDebug() which then calls
make_html_styles(). The text is surrounded by <span color='#FFFFFF'>
HTML formatting.
See also
Other jam practical functions:
breakDensity(),
call_fn_ellipsis(),
checkLightMode(),
check_pkg_installed(),
colNum2excelName(),
color_dither(),
exp2signed(),
getAxisLabel(),
isFALSEV(),
isTRUEV(),
jargs(),
kable_coloring(),
lldf(),
log2signed(),
middle(),
minorLogTicks(),
newestFile(),
reload_rmarkdown_cache(),
renameColumn(),
rmInfinite(),
rmNA(),
rmNAs(),
rmNULL(),
setPrompt()
Other jam practical functions:
breakDensity(),
call_fn_ellipsis(),
checkLightMode(),
check_pkg_installed(),
colNum2excelName(),
color_dither(),
exp2signed(),
getAxisLabel(),
isFALSEV(),
isTRUEV(),
jargs(),
kable_coloring(),
lldf(),
log2signed(),
middle(),
minorLogTicks(),
newestFile(),
reload_rmarkdown_cache(),
renameColumn(),
rmInfinite(),
rmNA(),
rmNAs(),
rmNULL(),
setPrompt()
Other jam practical functions:
breakDensity(),
call_fn_ellipsis(),
checkLightMode(),
check_pkg_installed(),
colNum2excelName(),
color_dither(),
exp2signed(),
getAxisLabel(),
isFALSEV(),
isTRUEV(),
jargs(),
kable_coloring(),
lldf(),
log2signed(),
middle(),
minorLogTicks(),
newestFile(),
reload_rmarkdown_cache(),
renameColumn(),
rmInfinite(),
rmNA(),
rmNAs(),
rmNULL(),
setPrompt()
Examples
printDebug("Testing ", "default ", "printDebug().");
#> ## (14:38:26) 22Mar2025: Testing default printDebug().
printDebug("List of vectors:", c("one", "two", "three"));
#> ## (14:38:26) 22Mar2025: List of vectors:one,two,three
# By default, there is no space between separate elements in `...`
printDebug("List of vectors:", c("one", "two", "three"),
c("four", "five", "six"));
#> ## (14:38:26) 22Mar2025: List of vectors:one,two,threefour,five,six
# To add a space " " between elements, use collapse
printDebug("List of vectors:", c("one", "two", "three"),
c("four", "five", "six"), collapse=" ");
#> ## (14:38:26) 22Mar2025: List of vectors: one, two, three four, five, six
# slightly different style, one entry per line, indented:
printDebug("List of vectors:", c("one", "two", "three"),
c("four", "five", "six"), collapse="\n ");
#> ## (14:38:26) 22Mar2025: List of vectors:
#> one,
#> two,
#> three
#> four,
#> five,
#> six
# when a vector entirely contains recognized colors,
# the colors are used in the output
printDebug(c("red", "blue", "yellow"));
#> ## (14:38:26) 22Mar2025: red,blue,yellow
# When the vector contains colors, the names are used as the label
color_vector <- jamba::nameVector(c("red", "blue", "green","orange"),
c("group_A", "group_B", "group_C", "group_D"));
printDebug(color_vector);
#> ## (14:38:26) 22Mar2025: group_A,group_B,group_C,group_D
# Remember the sister function that inverses the colors
printDebugI(color_vector);
#> ## (14:38:26) 22Mar2025: group_A,group_B,group_C,group_D
printDebug(1:10, fgText="blue", dex=2);
#> ## (14:38:26) 22Mar2025: 1,2,3,4,5,6,7,8,9,10
printDebug(1:10, bgText="blue", dex=2);
#> ## (14:38:26) 22Mar2025: 1,2,3,4,5,6,7,8,9,10
printDebug(1:10, fgText="orange", dex=2);
#> ## (14:38:26) 22Mar2025: 1,2,3,4,5,6,7,8,9,10