Bold text in R - lmmx/devnotes GitHub Wiki

I can write bold text by setting a variable from tput as described in this question, and was wondering if how to incorporate this into an R script run from the command line (with Rscript script.r).

Turns out this is how to do so:

cat(system('bold=`tput bold`; normal=`tput sgr0`;echo "Hello, may I be ${bold}bold${normal}?"', intern=TRUE))
cat('\n\n!\n\n')
cat(system('echo "Note that the effect does ${bold}not persist${normal} after the initial command.\n'))

Giving:

Hello, may I be bold?

!

Note that the effect does not persist after the initial command.

Removing the cat() function from the first line will show the character strings that are interpreted to give bold text — often seemingly these are \033[1m to give bold and \033(B\033[m to return to normal font weight.

This means it's possible to take these system variables from tput into R variables and use them to manipulate bold text in strings instead without repeatedly asking the system for this string, e.g.

boldcode <- system('bold=`tput bold`; echo "${bold}"', intern=TRUE))
normalcode <- system('normal=`tput sgr0`;echo "${normal}"', intern=TRUE))
⚠️ **GitHub.com Fallback** ⚠️