This is the core of the colorDF package – a print method for the colorful (and other) data frames.
a colorful data frame (object with class colorDF), a data.frame, a tibble, a data.table or any other object which can be coerced to data frame with as.data.frame function.
further arguments are ignored
Number of rows to show (default=20, use Inf to show all; this value can be set with options("colorDF_n"))
number of characters that the data frame should span on output
if TRUE (default), row names will be shown on output
whether to print with tibble style (overrides style setting)
a logical vector indicating which rows to highlight
column separator string (overrides style setting)
set default background for the table
set default foreground for the table
print_colorDF is the exported function, print.colorDF is the S3 method. Otherwise they are identical.
print_colorDF
is a function that can be applied to any data frame like
object. Using colorDF()
to change the class of the object is only
necessary if modifications to the style are required, such as specifying
column type. However, print_colorDF
applied to virtually any data frame,
tibble or data table will work. In such a case, the theme used to display the data frame
will either be taken from getOption("colorDF_theme")
or a default theme will be used.
Column types are basically the column classes (numeric, factor etc.) plus a few specialized
types (such as p-value) which are displayed slightly differently. For
example, an identifier will usually be shown in bold, and significant
p-values will be red (details depend on the given theme and style; see
col_type()
and df_style()
for more information).
It is possible to assign print_colorDF
to the default methods, thus
changing the way how tibbles, data frames or other data frame like objects
are displayed. This should be generally safe, but use it on your own peril
and preferably only in interactive sessions. I use the following code in my
.Rprofile file:
if(interactive()) {
print.data.frame <- colorDF::print_colorDF
print.tbl <- colorDF::print_colorDF
print.data.table <- colorDF::print_colorDF
}
df_style()
on how to modify colorful data frames styles;
col_type()
on how to change the column types; colorDF_themes_show()
to
demonstrate available themes; highlight()
and df_search()
functions on
how to use colorDF to highlight selected parts of a data frame.
colorDF(mtcars)
#> # Color data frame (class colorDF) 11 x 32:
#> # (Showing rows 1 - 20 out of 32)
#> │mpg │cyl │disp │hp │drat │wt │qsec │vs │am │gear
#> │<dbl>│<dbl>│<dbl>│<dbl>│<dbl>│<dbl>│<dbl>│<dbl>│<dbl>│<dbl>
#> Mazda RX4│ 21│ 6│ 160│ 110│ 3.9│ 2.6│ 16│ 0│ 1│ 4
#> Mazda RX4 Wag│ 21│ 6│ 160│ 110│ 3.9│ 2.9│ 17│ 0│ 1│ 4
#> Datsun 710│ 23│ 4│ 108│ 93│ 3.9│ 2.3│ 19│ 1│ 1│ 4
#> Hornet 4 Drive│ 21│ 6│ 258│ 110│ 3.1│ 3.2│ 19│ 1│ 0│ 3
#> Hornet Sportabout│ 19│ 8│ 360│ 175│ 3.1│ 3.4│ 17│ 0│ 0│ 3
#> Valiant│ 18│ 6│ 225│ 105│ 2.8│ 3.5│ 20│ 1│ 0│ 3
#> Duster 360│ 14│ 8│ 360│ 245│ 3.2│ 3.6│ 16│ 0│ 0│ 3
#> Merc 240D│ 24│ 4│ 147│ 62│ 3.7│ 3.2│ 20│ 1│ 0│ 4
#> Merc 230│ 23│ 4│ 141│ 95│ 3.9│ 3.1│ 23│ 1│ 0│ 4
#> Merc 280│ 19│ 6│ 168│ 123│ 3.9│ 3.4│ 18│ 1│ 0│ 4
#> Merc 280C│ 18│ 6│ 168│ 123│ 3.9│ 3.4│ 19│ 1│ 0│ 4
#> Merc 450SE│ 16│ 8│ 276│ 180│ 3.1│ 4.1│ 17│ 0│ 0│ 3
#> Merc 450SL│ 17│ 8│ 276│ 180│ 3.1│ 3.7│ 18│ 0│ 0│ 3
#> Merc 450SLC│ 15│ 8│ 276│ 180│ 3.1│ 3.8│ 18│ 0│ 0│ 3
#> Cadillac Fleetwood│ 10│ 8│ 472│ 205│ 2.9│ 5.2│ 18│ 0│ 0│ 3
#> Lincoln Continental│ 10│ 8│ 460│ 215│ 3.0│ 5.4│ 18│ 0│ 0│ 3
#> Chrysler Imperial│ 15│ 8│ 440│ 230│ 3.2│ 5.3│ 17│ 0│ 0│ 3
#> Fiat 128│ 32│ 4│ 79│ 66│ 4.1│ 2.2│ 19│ 1│ 1│ 4
#> Honda Civic│ 30│ 4│ 76│ 52│ 4.9│ 1.6│ 19│ 1│ 1│ 4
#> Toyota Corolla│ 34│ 4│ 71│ 65│ 4.2│ 1.8│ 20│ 1│ 1│ 4
#> # 1 more columns: carb (<dbl>)
print_colorDF(mtcars, row.names=FALSE)
#> # Data frame like object (class data.frame) 11 x 32:
#> # (Showing rows 1 - 20 out of 32)
#> │mpg │cyl │disp │hp │drat │wt │qsec │vs │am │gear │carb
#> │<dbl>│<dbl>│<dbl>│<dbl>│<dbl>│<dbl>│<dbl>│<dbl>│<dbl>│<dbl>│<dbl>
#> │ 21│ 6│ 160│ 110│ 3.9│ 2.6│ 16│ 0│ 1│ 4│ 4
#> │ 21│ 6│ 160│ 110│ 3.9│ 2.9│ 17│ 0│ 1│ 4│ 4
#> │ 23│ 4│ 108│ 93│ 3.9│ 2.3│ 19│ 1│ 1│ 4│ 1
#> │ 21│ 6│ 258│ 110│ 3.1│ 3.2│ 19│ 1│ 0│ 3│ 1
#> │ 19│ 8│ 360│ 175│ 3.1│ 3.4│ 17│ 0│ 0│ 3│ 2
#> │ 18│ 6│ 225│ 105│ 2.8│ 3.5│ 20│ 1│ 0│ 3│ 1
#> │ 14│ 8│ 360│ 245│ 3.2│ 3.6│ 16│ 0│ 0│ 3│ 4
#> │ 24│ 4│ 147│ 62│ 3.7│ 3.2│ 20│ 1│ 0│ 4│ 2
#> │ 23│ 4│ 141│ 95│ 3.9│ 3.1│ 23│ 1│ 0│ 4│ 2
#> │ 19│ 6│ 168│ 123│ 3.9│ 3.4│ 18│ 1│ 0│ 4│ 4
#> │ 18│ 6│ 168│ 123│ 3.9│ 3.4│ 19│ 1│ 0│ 4│ 4
#> │ 16│ 8│ 276│ 180│ 3.1│ 4.1│ 17│ 0│ 0│ 3│ 3
#> │ 17│ 8│ 276│ 180│ 3.1│ 3.7│ 18│ 0│ 0│ 3│ 3
#> │ 15│ 8│ 276│ 180│ 3.1│ 3.8│ 18│ 0│ 0│ 3│ 3
#> │ 10│ 8│ 472│ 205│ 2.9│ 5.2│ 18│ 0│ 0│ 3│ 4
#> │ 10│ 8│ 460│ 215│ 3.0│ 5.4│ 18│ 0│ 0│ 3│ 4
#> │ 15│ 8│ 440│ 230│ 3.2│ 5.3│ 17│ 0│ 0│ 3│ 4
#> │ 32│ 4│ 79│ 66│ 4.1│ 2.2│ 19│ 1│ 1│ 4│ 1
#> │ 30│ 4│ 76│ 52│ 4.9│ 1.6│ 19│ 1│ 1│ 4│ 2
#> │ 34│ 4│ 71│ 65│ 4.2│ 1.8│ 20│ 1│ 1│ 4│ 1
if(require(dplyr)) {
starwars %>% colorDF
starwars %>% print_colorDF(highlight=.$homeworld == "Tatooine")
## equivalently
starwars %>% highlight(.$homeworld == "Tatooine")
## with another style
options(colorDF_theme="bw")
starwars %>% print_colorDF(tibble_style=TRUE, sep=" |%%| ")
}
#> # Tibble (class tbl_df) 14 x 87:
#> # (Showing rows 1 - 20 out of 87)
#> │name │height│mass │hair_color │skin_color │eye_color
#> │<chr> │<int> │<dbl>│<chr> │<chr> │<chr>
#> 1│Luke Skywalker │ 172│ 77│blond │fair │blue
#> 2│C-3PO │ 167│ 75│NA │gold │yellow
#> 3│R2-D2 │ 96│ 32│NA │white, blue │red
#> 4│Darth Vader │ 202│ 136│none │white │yellow
#> 5│Leia Organa │ 150│ 49│brown │light │brown
#> 6│Owen Lars │ 178│ 120│brown, grey │light │blue
#> 7│Beru Whitesun lars │ 165│ 75│brown │light │blue
#> 8│R5-D4 │ 97│ 32│NA │white, red │red
#> 9│Biggs Darklighter │ 183│ 84│black │light │brown
#> 10│Obi-Wan Kenobi │ 182│ 77│auburn, white│fair │blue-gray
#> 11│Anakin Skywalker │ 188│ 84│blond │fair │blue
#> 12│Wilhuff Tarkin │ 180│ NA│auburn, grey │fair │blue
#> 13│Chewbacca │ 228│ 112│brown │unknown │blue
#> 14│Han Solo │ 180│ 80│brown │fair │brown
#> 15│Greedo │ 173│ 74│NA │green │black
#> 16│Jabba Desilijic Tiure│ 175│ 1358│NA │green-tan, brown│orange
#> 17│Wedge Antilles │ 170│ 77│brown │fair │hazel
#> 18│Jek Tono Porkins │ 180│ 110│brown │fair │blue
#> 19│Yoda │ 66│ 17│white │green │brown
#> 20│Palpatine │ 170│ 75│grey │pale │yellow
#> # 8 more columns: birth_year (<dbl>), sex (<chr>), gender (<chr>), homeworld (<chr>), species (<chr>), films (<lst>), vehicles (<lst>), starships (<lst>)
#> # Tibble (class tbl_df) 14 x 87:
#> # (Showing rows 1 - 20 out of 87)
#> │name │height│mass │hair_color │skin_color │eye_color
#> │<chr> │<int> │<dbl>│<chr> │<chr> │<chr>
#> 1│Luke Skywalker │ 172│ 77│blond │fair │blue
#> 2│C-3PO │ 167│ 75│NA │gold │yellow
#> 3│R2-D2 │ 96│ 32│NA │white, blue │red
#> 4│Darth Vader │ 202│ 136│none │white │yellow
#> 5│Leia Organa │ 150│ 49│brown │light │brown
#> 6│Owen Lars │ 178│ 120│brown, grey │light │blue
#> 7│Beru Whitesun lars │ 165│ 75│brown │light │blue
#> 8│R5-D4 │ 97│ 32│NA │white, red │red
#> 9│Biggs Darklighter │ 183│ 84│black │light │brown
#> 10│Obi-Wan Kenobi │ 182│ 77│auburn, white│fair │blue-gray
#> 11│Anakin Skywalker │ 188│ 84│blond │fair │blue
#> 12│Wilhuff Tarkin │ 180│ NA│auburn, grey │fair │blue
#> 13│Chewbacca │ 228│ 112│brown │unknown │blue
#> 14│Han Solo │ 180│ 80│brown │fair │brown
#> 15│Greedo │ 173│ 74│NA │green │black
#> 16│Jabba Desilijic Tiure│ 175│ 1358│NA │green-tan, brown│orange
#> 17│Wedge Antilles │ 170│ 77│brown │fair │hazel
#> 18│Jek Tono Porkins │ 180│ 110│brown │fair │blue
#> 19│Yoda │ 66│ 17│white │green │brown
#> 20│Palpatine │ 170│ 75│grey │pale │yellow
#> # 8 more columns: birth_year (<dbl>), sex (<chr>), gender (<chr>), homeworld (<chr>), species (<chr>), films (<lst>), vehicles (<lst>), starships (<lst>)
#> # Tibble (class tbl_df) 14 x 87:
#> # (Showing rows 1 - 20 out of 87)
#> |%%| name |%%| height |%%| mass |%%| hair_color
#> |%%| <chr> |%%| <int> |%%| <dbl> |%%| <chr>
#> 1 |%%| Luke Skywalker |%%| 172 |%%| 77 |%%| blond
#> 2 |%%| C-3PO |%%| 167 |%%| 75 |%%| NA
#> 3 |%%| R2-D2 |%%| 96 |%%| 32 |%%| NA
#> 4 |%%| Darth Vader |%%| 202 |%%| 136 |%%| none
#> 5 |%%| Leia Organa |%%| 150 |%%| 49 |%%| brown
#> 6 |%%| Owen Lars |%%| 178 |%%| 120 |%%| brown, grey
#> 7 |%%| Beru Whitesun lars |%%| 165 |%%| 75 |%%| brown
#> 8 |%%| R5-D4 |%%| 97 |%%| 32 |%%| NA
#> 9 |%%| Biggs Darklighter |%%| 183 |%%| 84 |%%| black
#> 10 |%%| Obi-Wan Kenobi |%%| 182 |%%| 77 |%%| auburn, white
#> 11 |%%| Anakin Skywalker |%%| 188 |%%| 84 |%%| blond
#> 12 |%%| Wilhuff Tarkin |%%| 180 |%%| NA |%%| auburn, grey
#> 13 |%%| Chewbacca |%%| 228 |%%| 112 |%%| brown
#> 14 |%%| Han Solo |%%| 180 |%%| 80 |%%| brown
#> 15 |%%| Greedo |%%| 173 |%%| 74 |%%| NA
#> 16 |%%| Jabba Desilijic Tiure |%%| 175 |%%| 1358 |%%| NA
#> 17 |%%| Wedge Antilles |%%| 170 |%%| 77 |%%| brown
#> 18 |%%| Jek Tono Porkins |%%| 180 |%%| 110 |%%| brown
#> 19 |%%| Yoda |%%| 66 |%%| 17 |%%| white
#> 20 |%%| Palpatine |%%| 170 |%%| 75 |%%| grey
#> # 10 more columns: skin_color (<chr>), eye_color (<chr>), birth_year (<dbl>), sex (<chr>), gender (<chr>), homeworld (<chr>), species (<chr>), films (<lst>), vehicles (<lst>), starships (<lst>)