Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tooltip / Popover for kable in Rmarkdown

I am using kableExtra for this. My dataset:

data = data.frame(REASON_CODE = c('V312A','UW32E','R312D'), REASON_DESCRIPTION = c('MISTAKE','ACCIDENT','INTENTIONAL'))

Since the reason description is much longer, I am trying to have it as a tooltip/popover message for each cell in reason code. My current code:

data$REASON_CODE = text_spec(x = data$REASON_CODE,'tooltip',format = 'html', tooltip = data$REASON_DESCRIPTION) 
kable(data[,1])

I am seeing the error as it prints full html on my output.

<span style=" NA    TRUE" data-toggle="tooltip" data-placement="right" title="MISTAKE">V312A</span>

I am not sure what is the mistake I am making and what are the possible options for something like this?

like image 787
Khiem Nguyen Avatar asked Nov 26 '25 12:11

Khiem Nguyen


1 Answers

Here's how I got it to work in my R Markdown. It uses knitr:

```{r}
library(magrittr)
library(knitr)
data <-data.frame(REASON_CODE = c('V312A','UW32E','R312D'), REASON_DESCRIPTION = c('MISTAKE','ACCIDENT','INTENTIONAL'))
data$TIP <- c("Tip 1","Tip 2","Tip 3")
data %>%
  mutate(REASON_DESCRIPTION=text_spec(REASON_DESCRIPTION, "html", tooltip=TIP)) %>% 
  select(REASON_CODE,REASON_DESCRIPTION) %>%
  kable("html", escape=F) %>% 
  kable_styling()
```

It produces this (I'm hovering on "intentional"):

enter image description here

like image 131
mysteRious Avatar answered Nov 28 '25 01:11

mysteRious



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!