生活需要色彩,工作也一样,而且我最近迷上了生信分析流程中的各种颜色的代码。上期,我分享的让我们的脚本带点色儿 - 知乎 (zhihu.com),是面向CLI(Command-Line Interface)的用户的,今天分享的内容,CLI和GUI(Graphical User Interface)都适用。
R语言现在已经不是小众语言了,学校教育和各行各业的工作中都可以使用R语言中便捷的统计学函数和强悍的图形可视化能力,人们对于数据可视化有着执念式的追求。
当然,做生信分析工作也离不开R语言,下面我将展示一个R包,可以使我们的R代码或者程序输出信息不那么单调。
这个包就是:crayon(Colored Terminal Output),作者有两位:这是他们的邮箱地址csardi.gabor@gmail.com/brodie.gaslam@yahoo.com。
我直接分步进行代码和输出信息展示:相信大家一看就明白如何使用了,非常nice and easy.
step1: 安装R包:crayon,只需要安装一次就可以了,后面使用时,每次导入即可
install.packages("crayon")
为了展示方便,我们这里再安装一个R包:praise
step2: 导入R包,R包安装成功后就可以导入R环境
library("crayon")
library("praise")
step3:查看版本信息
packageVersion("crayon")
# [1] ‘1.4.1’
step4:获取R包中的函数列表
ls(package:crayon)
[1] "%+%" "bgBlack" "bgBlue" "bgCyan"
[5] "bgGreen" "bgMagenta" "bgRed" "bgWhite"
[9] "bgYellow" "black" "blue" "blurred"
[13] "bold" "chr" "col_align" "col_nchar"
[17] "col_strsplit" "col_substr" "col_substring" "combine_styles"
[21] "cyan" "drop_style" "finish" "green"
[25] "has_color" "has_hyperlink" "has_style" "hidden"
[29] "hyperlink" "inverse" "italic" "magenta"
[33] "make_style" "num_ansi_colors" "num_colors" "red"
[37] "reset" "show_ansi_colors" "silver" "strikethrough"
[41] "strip_style" "style" "styles" "underline"
[45] "white" "yellow"
高能预警!!!
step5:Genaral styles:8个函数
5.1 reset,这个额。。。
?reset
cat(reset(blue(praise(), "\n")))
就没什么感觉
5.2 bold,字体加粗函数
> praise()
[1] "You are breathtaking!"
> cat(bold(praise(),"\n"))
You are amazing!
5.3 blurred (usually called ‘dim’, renamed to avoid name clash)
> cat(blurred(praise(), "\n"))
You are wicked!
5.4 italic (not widely supported),斜体
> cat(italic(praise(), "\n"))
You are delightful!
5.5 underline,下划线
> cat(underline(praise(), "\n"))
You are impressive!
5.6 inverse,高对比度
> cat(inverse(praise(), "\n"))
You are superior!
> a = "Colored terminal output"
> cat(inverse(a, "\n"))
Colored terminal output
5.7 hidden,隐藏输出字体
> cat(hidden(praise(),"\n"))
5.8 strikethrough (not widely supported)
> cat(strikethrough(praise(), "\n"))
You are classy!
step6:Text colors:9个函数
cat(black(praise(),"\n"))
cat(red(praise(),"\n"))
cat(green(praise(),"\n"))
cat(yellow(praise(),"\n"))
cat(blue(praise(),"\n"))
cat(magenta(praise(),"\n"))
cat(cyan(praise(),"\n"))
cat(white(praise(),"\n"))
cat(silver(praise(),"\n"))
linux服务器展示:
step7:Background colors:8个函数
cat(bgBlack(praise(), "\n"))
cat(bgRed(praise(), "\n"))
cat(bgGreen(praise(), "\n"))
cat(bgYellow(praise(), "\n"))
cat(bgBlue(praise(), "\n"))
cat(bgMagenta(praise(), "\n"))
cat(bgCyan(praise(), "\n"))
cat(bgWhite(praise(), "\n"))
linux服务器展示:
step8:总共有8x8x9=576种搭配用法
搭配举例1:bold + bgWhite + blue
cat(bold(bgWhite(blue(praise(), "\n"))))
搭配举例2:bold + bgRed + black + bgWhite + blue
cat(bold(bgRed(black("Today, ")),bgWhite(blue(praise(), "\n"))))
step9:R程序中的用法举例:
> exprmat_file <- list.files(path = "./", pattern = "ExprData_raw.xlsx$")
> cat(blue(paste0("原始表达矩阵文件名为: ",exprmat_file)))
原始表达矩阵文件名为: ExprData_raw.xlsx
> cat(blue("原始表达矩阵文件名为: "), red(exprmat_file))
原始表达矩阵文件名为: ExprData_raw.xlsx
> cat(bgYellow(blue("原始表达矩阵文件名为: "), red(exprmat_file)))
原始表达矩阵文件名为: ExprData_raw.xlsx
> cat(bgWhite(blue("原始表达矩阵文件名为: "), red(exprmat_file)))
原始表达矩阵文件名为: ExprData_raw.xlsx
step10:收尾彩蛋
%+%用法,字符串拼接
> letters[1:10] %+% chr(1:10)
[1] "a1" "b2" "c3" "d4" "e5" "f6" "g7" "h8" "i9" "j10"
> letters[1:10] %+% "-" %+% chr(1:10)
[1] "a-1" "b-2" "c-3" "d-4" "e-5" "f-6" "g-7" "h-8" "i-9" "j-10"
非常方便哦,不要只记得paste()和paste0(),也可以试试%+%
> date()
[1] "Thu Jul 22 16:07:58 2021"
> sessionInfo()
R version 4.1.0 (2021-05-18)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur 10.16
Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib
locale:
[1] zh_CN.UTF-8/zh_CN.UTF-8/zh_CN.UTF-8/C/zh_CN.UTF-8/zh_CN.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] praise_1.0.0 crayon_1.4.1
loaded via a namespace (and not attached):
[1] zip_2.2.0 Rcpp_1.0.7 cellranger_1.1.0
[4] pillar_1.6.1 compiler_4.1.0 BiocManager_1.30.16
[7] remotes_2.4.0 forcats_0.5.1 prettyunits_1.1.1
[10] tools_4.1.0 testthat_3.0.4 pkgbuild_1.2.0
[13] pkgload_1.2.1 memoise_2.0.0 lifecycle_1.0.0
[16] tibble_3.1.2 pkgconfig_2.0.3 rlang_0.4.11
[19] openxlsx_4.2.4 rstudioapi_0.13 cli_3.0.1
[22] curl_4.3.2 haven_2.4.1 rio_0.5.27
[25] fastmap_1.1.0 withr_2.4.2 desc_1.3.0
[28] fs_1.5.0 vctrs_0.3.8 devtools_2.4.2
[31] hms_1.1.0 rprojroot_2.0.2 glue_1.4.2
[34] data.table_1.14.0 R6_2.5.0 processx_3.5.2
[37] fansi_0.5.0 readxl_1.3.1 foreign_0.8-81
[40] sessioninfo_1.1.1 purrr_0.3.4 callr_3.7.0
[43] magrittr_2.0.1 usethis_2.0.1 ps_1.6.0
[46] ellipsis_0.3.2 utf8_1.2.1 stringi_1.7.3
[49] cachem_1.0.5