Reputation: 2659
If I print a tibble with the console in the RStudio IDE the NA values appear in red. When I print the table on a xaringan slide the table is black and white and the NA_character_ values display as <NA>
. Is there a way to get the output on a xaringan slide to look like the console output (without using a screen shot)?
Here is an example:
---
title: "x"
output:
xaringan::moon_reader
---
```{r}
library(tibble)
example <- tribble(
~name, ~age,
"Fran", 2,
"Bob", NA,
NA, NA
)
example
```
Upvotes: 5
Views: 752
Reputation: 34441
This is available via the fansi
package which provides knitr
hooks for rendering crayon
output into HTML in rmarkdown. More info in the vignette here.
```{r setup, echo = FALSE, results='asis'}
options(crayon.enabled = TRUE)
old.hooks <- fansi::set_knit_hooks(knitr::knit_hooks)
```
```{r}
tibble::tribble(
~name, ~age,
"Fran", 2,
"Bob", NA,
NA, NA
)
waldo::compare(1:5, 3:7)
```
Upvotes: 5
Reputation: 18714
I tried to account for 'I didn't know what I didn't know' in regards to where your R Markdown script goes down the line (as you add more content). This works for what you have here, in addition to looking for coded negative numbers.
I have the styling called out individually. If you want to update/change these styles, you should be able to see what and where to change the styles from how this is written. I added comments to the Javascript, as well.
I used the same YAML as you have above.
I updated example
so that it would contain a negative number. That way you can see that negative numbers will change color, as well.
library(tibble)
example <- tribble(
~name, ~age,
"Fran", 2,
"Bob", NA,
NA, NA,
"George", -1
)
example
Add this chunk anywhere inside your R Markdown script file.
```{r doAsYouAreTold,results="asis",echo=FALSE,engine="js"}
// var str = text.innerHTML,
// reg = /red|blue|green|orange/ig; //g is to replace all occurrences
text = document.querySelectorAll("pre>code.remark-code:not(.hljs)>div.remark-code-line");
for (i = 0; i < text.length; ++i) {
str = text[i].innerHTML,
reg = /NA|##|-[0-9]+|<chr>|<dbl>|<int>|<fct>|<num>/ig;
//g is to replace all occurrences
// fix the structure
toStr = String(reg);
color = (toStr.replace('\/g', '|')).substring(1);
//split the list
colors = color.split("|");
//remove the <> around NA
str = str.replace(/<NA>/g, 'NA '); // added whitespace to keep spacing even
//remove the ## preceding each code line
str = str.replace(/##/g, '');
// look for negative numbers - make them dark red
if(colors.indexOf("-[0-9]+") > -1) {
str = str.replace(/(-[0-9]+)/g, '<span style="color:darkred;">$1</span>');
}
// look for NA - make them dark red
if (colors.indexOf("NA") > -1) {
str = str.replace(/NA/g, '<span style="color:darkred;">NA</span>');
}
// look for field types - make them italicized and the color gray
if (colors.indexOf("<chr>") > -1) {
str = str.replace(/<chr>/g, '<i style="color:gray;"><chr></i>');
}
if (colors.indexOf("<dbl>") > -1) {
str = str.replace(/<dbl>/g, '<i style="color:gray;"><dbl></i>');
}
if (colors.indexOf("<int>") > -1) {
str = str.replace(/<int>/g, '<i style="color:gray;"><int></i>');
}
if (colors.indexOf("<fct>") > -1) {
str = str.replace(/<fct>/g, '<i style="color:gray;"><fct></i>');
}
if (colors.indexOf("<num>") > -1) {
str = str.replace(/<num>/g, '<i style="color:gray;"><num></i>');
}
// replace the updated content of str
text[i].innerHTML = str;
}
// look for A tibble (A tsibble.. A ... anything else that could land here)
// make the text gray
text2 = document.querySelectorAll("pre>code.remark-code>div.remark-code-line:First-child");
for(j = 0; j < text2.length; ++j) {
if(text2[j].innerText.includes("# A")) {
str2 = text2[j].innerHTML
text2[j].innerHTML = '<span style="color:gray;">'+str2+'</span>';
}
}
```
Upvotes: 2