Search code examples
rsearchplotlabelmarkdown

R Markdown html: main and axis labels of plots as text suitable for search-function


I want to generate a R markdown html document with plots and it should be possible to jump to a certain plot by search-function (in my example there are 3 plots and I want to jump in the html-doc to the plot, where the main is "rivers"). I think, the problem is, that main and axis labels of a plot are grafical elements, like the plot itself, and not text. So the search-function doesn't work.

Of course it would be possible to add manually text before each plot, but as all my plots are generated with a for-loop, I don_t know how to do it.

is there a possibilty to include text-output in this kind of for-loop or are there other ideas, how the main or axis labels of a plot can be suitable for search-function?

thanks in advance!

---
title: "search function test"
author: "Michel Grün"
date: "last edited `r format(Sys.Date(),'%d.%m.%Y')`"
output: 
  html_document:
    df_print: paged
---


knitr::opts_chunk$set(echo = TRUE,warning = FALSE)




df<-data.frame(x=seq(1,20),
               trees=rnorm(20,4,3),
               mountains=rnorm(20,6,3),
               rivers=rnorm(20,4,4))



for(i in 2:length(colnames(df))){
  plot(df$x,df[,i],
       main=colnames(df)[i],
       xlab=colnames(df)[1],
       ylab=colnames(df)[i])
}

solved in another issue: https://stackoverflow.com/a/57034752/16578253

in this issue, the question is slightly different, but a solution shown there was also the solution for my problem. The idea is to create headings + outputs within a loop. As result, in the output dokument every header is followed by a plot and the header is of course suitable for search-function. It's important to use the argument results='asis' in the chunk konfiguration to allow that cat() is interpreted as Markdown syntax. Furthermore the cat()ing tshould be surrounded by some newlines to make sure it's interpreted properly.


Solution

  • You can combine a svg device with a knitr hook:

    ---
    title: "search function test"
    author: "Michel Grün"
    date: "last edited `r format(Sys.Date(),'%d.%m.%Y')`"
    output: 
      html_document:
      df_print: paged
    ---
    
    ```{r setup}
    library(ggplot2)
    library(knitr)
    
    # see https://github.com/yihui/knitr/issues/754
    local({
      hook_plot <- knit_hooks$get("plot")
      knit_hooks$set(plot = function(x, options) {
        x <- paste(x, collapse = ".")
        if (!grepl("\\.svg", x)) {
          return(hook_plot(x, options))
        }
        # read the content of the svg image and write it out without <?xml ... ?>
        paste(readLines(x)[-1], collapse = "\n")
      })
    })
    
    opts_chunk$set(echo = TRUE, warning = FALSE, dev = "svglite")
    
    
    df <- data.frame(
      x = seq(1, 20),
      trees = rnorm(20, 4, 3),
      mountains = rnorm(20, 6, 3),
      rivers = rnorm(20, 4, 4)
    )
    ```
    
    ```{r}
    
    for (i in 2:length(colnames(df))) {
      plot(df$x, df[, i],
        main =paste0(colnames(df)[i], " äöα😋"),
        xlab = colnames(df)[1],
        ylab = colnames(df)[i]
      )
    }
    ```
    

    enter image description here