With the crayon package, one can create colored strings:
library(crayon)
dat <- data.frame(X=c(yellow("foobar"), green("baz")), Y = cyan("qux"))
Here is the encoded dat
:
> dat
X Y
1 \033[33mfoobar\033[39m \033[36mqux\033[39m
2 \033[32mbaz\033[39m \033[36mqux\033[39m
With write.table
, one can get the table with the colors:
But the alignment is lost. How to get the colored dataframe with a nice alignment?
The colorDF package allows to set colors in a dataframe, but does not allow to do what I want. What I want is to be able to color one word say in red at every occurrence of this word in a column. The df_search
function of colorDF is close to what I want, but it colors the entire cells where the pattern is found, I want to color one word only.
For example, in this dataframe:
# file line code
# 1 folder/f.R 1 f <- function(x){
# 2 folder/subfolder/g.R 1 g <- function(y){
# 3 folder/subfolder/subsubfolder/h.R 1 h <- function(z){
I want the word function
in the code
column to be red.
It's possible to add color to data cells according to their values. The data_color() function colors all rows of any columns supplied. There are two ways to define how cells are colored: (1) through the use of a supplied color palette, and (2) through use of a color mapping function available from the scales package.
Every language provides some functions that can help you print the data on the console, and R is no different. To print the data on the console in R, use the print() function.
For using color in code, either its name is used or its order number is used. Though remembering color names is easier, at times colors are defined by the unique hexadecimal numbers which have been already predefined for each color in R. The six-digit length of the Hexadecimal number is in the format #RRGGBB.
Choosing the right color palette can often be difficult because it's both hard to discover suitable palettes and then obtain the vector of colors. To make this process easier we can elect to use the paletteer package, which makes a wide range of palettes from various R packages readily available.
It's possible to add color to data cells according to their values. The data_color () function colors all rows of any columns supplied. There are two ways to define how cells are colored: (1) through the use of a supplied color palette, and (2) through use of a color mapping function available from the scales package.
Using color palette for gradient fill in DataFrame: By importing the light palette of colors from the seaborn library, we can map the color gradient for the background of the data frame. Python3 import seaborn as sns cm = sns.light_palette ("green", as_cmap=True)
You could use a tibble
instead of a data.frame
as it allows to print vectors nicely, see vignette("pillar", package = "vctrs")
.
In particular :
You can get basic control over how a vector is printed in a tibble by providing a format() method
To answer your question, you could create a function_red
class extending character
, see vignette("s3-vector", package = "vctrs")
:
library(vctrs)
data <- read.table(text="file line code
'folder/f.R' 1 'f <- function(x){'
'folder/subfolder/g.R' 1 'g <- function(y){'
'folder/subfolder/subsubfolder/h.R' 1 'h <- function(z){'
",header=T)
function_red <- function(x = character()) {
vec_assert(x, character())
new_vctr(x, class = "vctrs_function_red")
}
format.vctrs_function_red <- function(x,...) {
gsub("function",crayon::red("function"),vec_data(x))
}
data$code <- function_red(data$code)
tibble::tibble(data)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With