1 message()
函数
可以使用message()
函数进行终端屏幕输出.
message("ABC", "DEF")
## ABCDEF
suppressMessages(message("ABC"))
testit <- function() {
message("testing package startup messages")
packageStartupMessage("initializing ...", appendLF = FALSE)
Sys.sleep(1)
packageStartupMessage(" done")
}
testit()
## testing package startup messages
## initializing ... done
suppressPackageStartupMessages(testit())
## testing package startup messages
suppressMessages(testit())
2 改变输出字体颜色和格式
使用crayon
包可以修改输出字体的颜色.参考官方文档:https://github.com/r-lib/crayon.
library(crayon)
## Warning: package 'crayon' was built under R version 3.6.1
cat(blue("Hello", "world!\n"))
## Hello world!
cat("... to highlight the " %+% red("search term") %+% " in a block of text\n")
## ... to highlight the search term in a block of text
cat(yellow$bgMagenta$bold('Hello world!\n'))
## Hello world!
cat(green(
'I am a green line ' %+%
blue$underline$bold('with a blue substring') %+%
' that becomes green again!\n'
))
## I am a green line with a blue substring that becomes green again!
error <- red $ bold
warn <- magenta $ underline
note <- cyan
cat(error("Error: subscript out of bounds!\n"))
## Error: subscript out of bounds!
cat(warn("Warning: shorter argument was recycled.\n"))
## Warning: shorter argument was recycled.
cat(note("Note: no such directory.\n"))
## Note: no such directory.
3 一些常用的特殊字符
使用clisymbols
包可以给出一些常见的特殊字符.
官方文档:https://github.com/r-lib/clisymbols.
library(clisymbols)
cat(symbol$tick, "All good\n")
## <U+2714> All good
cat(symbol$cross, "Problem\n")
## <U+2716> Problem
for (i in seq_along(symbol)) {
cat(symbol[[i]], "\t", names(symbol)[i], "\n", sep = "")
}
## <U+2714> tick
## <U+2716> cross
## <U+2605> star
## <U+2587> square
## [ ] square_small
## [¦] square_small_filled
## <U+25EF> circle
## (*) circle_filled
## <U+25CC> circle_dotted
## (o) circle_double
## <U+24DE> circle_circle
## <U+24E7> circle_cross
## <U+24BE> circle_pipe
## (?) circle_question_mark
## <U+25CF> bullet
## . dot
## - line
## - double_line
## … ellipsis
## > pointer
## i info
## <U+203C> warning
## <U+2630> menu
## <U+263A> smiley
## <U+0DF4> mustache
## <U+2665> heart
## <U+2191> arrow_up
## <U+2193> arrow_down
## <U+2190> arrow_left
## <U+2192> arrow_right
## (*) radio_on
## ( ) radio_off
## <U+2612> checkbox_on
## <U+2610> checkbox_off
## <U+24E7> checkbox_circle_on
## <U+24BE> checkbox_circle_off
## (?) fancy_question_mark
## != neq
## >= geq
## <= leq
## <U+2594> upper_block_1
## ¯ upper_block_4
## <U+2581> lower_block_1
## <U+2582> lower_block_2
## <U+2583> lower_block_3
## _ lower_block_4
## <U+2585> lower_block_5
## <U+2586> lower_block_6
## <U+2587> lower_block_7
## ¦ lower_block_8
## ¦ full_block