Search code examples
rr-markdownknitrkablekableextra

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?


Solution

  • 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