dynamic tabsets with multiple plots r markdown












1














I managed to create a html document that creates dynamic tabsets based on a list of items. Adding one plot works fine on one tabset. How can I add now multiple plots on one tabset?



Hereby the code I started from but it only shows 1 plot per tabset when you knit the document to html output. obviously there is still something missing.



---
title: "R Notebook"
output:
html_document:
df_print: paged
editor_options:
chunk_output_type: inline
---


### header 1
```{r}
library(ggplot2)
df <- mtcars

pl_list <- list()

pl1 <- qplot(cyl, disp, data = df[1:12,])
pl2 <- qplot(mpg, cyl, data = df[13:20,])
pl3 <- qplot(mpg, cyl, data = df[21:30,])
pl4 <- qplot(mpg, cyl, data = df[1:12,])

pl_list[[1]] <- list(pl1, pl3, "one")
pl_list[[2]] <- list(pl2, pl4, "two")
```


### header {.tabset}
```{r, results = 'asis', echo = FALSE}

for (i in seq_along(pl_list)){
tmp <- pl_list[[i]]
cat("####", tmp[[3]], " n")
print(tmp[1])
cat(" nn")
}
```









share|improve this question





























    1














    I managed to create a html document that creates dynamic tabsets based on a list of items. Adding one plot works fine on one tabset. How can I add now multiple plots on one tabset?



    Hereby the code I started from but it only shows 1 plot per tabset when you knit the document to html output. obviously there is still something missing.



    ---
    title: "R Notebook"
    output:
    html_document:
    df_print: paged
    editor_options:
    chunk_output_type: inline
    ---


    ### header 1
    ```{r}
    library(ggplot2)
    df <- mtcars

    pl_list <- list()

    pl1 <- qplot(cyl, disp, data = df[1:12,])
    pl2 <- qplot(mpg, cyl, data = df[13:20,])
    pl3 <- qplot(mpg, cyl, data = df[21:30,])
    pl4 <- qplot(mpg, cyl, data = df[1:12,])

    pl_list[[1]] <- list(pl1, pl3, "one")
    pl_list[[2]] <- list(pl2, pl4, "two")
    ```


    ### header {.tabset}
    ```{r, results = 'asis', echo = FALSE}

    for (i in seq_along(pl_list)){
    tmp <- pl_list[[i]]
    cat("####", tmp[[3]], " n")
    print(tmp[1])
    cat(" nn")
    }
    ```









    share|improve this question



























      1












      1








      1







      I managed to create a html document that creates dynamic tabsets based on a list of items. Adding one plot works fine on one tabset. How can I add now multiple plots on one tabset?



      Hereby the code I started from but it only shows 1 plot per tabset when you knit the document to html output. obviously there is still something missing.



      ---
      title: "R Notebook"
      output:
      html_document:
      df_print: paged
      editor_options:
      chunk_output_type: inline
      ---


      ### header 1
      ```{r}
      library(ggplot2)
      df <- mtcars

      pl_list <- list()

      pl1 <- qplot(cyl, disp, data = df[1:12,])
      pl2 <- qplot(mpg, cyl, data = df[13:20,])
      pl3 <- qplot(mpg, cyl, data = df[21:30,])
      pl4 <- qplot(mpg, cyl, data = df[1:12,])

      pl_list[[1]] <- list(pl1, pl3, "one")
      pl_list[[2]] <- list(pl2, pl4, "two")
      ```


      ### header {.tabset}
      ```{r, results = 'asis', echo = FALSE}

      for (i in seq_along(pl_list)){
      tmp <- pl_list[[i]]
      cat("####", tmp[[3]], " n")
      print(tmp[1])
      cat(" nn")
      }
      ```









      share|improve this question















      I managed to create a html document that creates dynamic tabsets based on a list of items. Adding one plot works fine on one tabset. How can I add now multiple plots on one tabset?



      Hereby the code I started from but it only shows 1 plot per tabset when you knit the document to html output. obviously there is still something missing.



      ---
      title: "R Notebook"
      output:
      html_document:
      df_print: paged
      editor_options:
      chunk_output_type: inline
      ---


      ### header 1
      ```{r}
      library(ggplot2)
      df <- mtcars

      pl_list <- list()

      pl1 <- qplot(cyl, disp, data = df[1:12,])
      pl2 <- qplot(mpg, cyl, data = df[13:20,])
      pl3 <- qplot(mpg, cyl, data = df[21:30,])
      pl4 <- qplot(mpg, cyl, data = df[1:12,])

      pl_list[[1]] <- list(pl1, pl3, "one")
      pl_list[[2]] <- list(pl2, pl4, "two")
      ```


      ### header {.tabset}
      ```{r, results = 'asis', echo = FALSE}

      for (i in seq_along(pl_list)){
      tmp <- pl_list[[i]]
      cat("####", tmp[[3]], " n")
      print(tmp[1])
      cat(" nn")
      }
      ```






      r r-markdown knitr






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 10:13









      PoGibas

      15.8k144175




      15.8k144175










      asked Nov 23 '18 at 9:55









      JL BJL B

      394




      394
























          1 Answer
          1






          active

          oldest

          votes


















          0














          There are a couple of improvements you can do.




          1. Create cat header function with arguments for text and level.


          With it you don't need to call cat multiple times and it creates wanted number of # automatically.



          catHeader <- function(text = "", level = 3) {
          cat(paste0("nn",
          paste(rep("#", level), collapse = ""),
          " ", text, "n"))
          }




          1. print plots using lapply.


          Full code looks like this:



          ---
          title: "R Notebook"
          output:
          html_document:
          df_print: paged
          editor_options:
          chunk_output_type: inline
          ---


          ```{r, functions}
          catHeader <- function(text = "", level = 3) {
          cat(paste0("nn",
          paste(rep("#", level), collapse = ""),
          " ", text, "n"))
          }
          ```

          ### header 1

          ```{r}
          library(ggplot2)
          df <- mtcars

          pl_list <- list()

          pl1 <- qplot(cyl, disp, data = df[1:12,])
          pl2 <- qplot(mpg, cyl, data = df[13:20,])
          pl3 <- qplot(mpg, cyl, data = df[21:30,])
          pl4 <- qplot(mpg, cyl, data = df[1:12,])

          pl_list[[1]] <- list(pl1, pl3, "one")
          pl_list[[2]] <- list(pl2, pl4, "two")
          ```


          ## header {.tabset}

          ```{r, results = "asis", echo = FALSE}

          for(i in seq_along(pl_list)){
          tmp <- pl_list[[i]]
          # As you want to use tabset level here has to be lower than
          # parent level (ie, parent is 2, so here you have to use 3)
          catHeader(tmp[[3]], 3)
          lapply(tmp[1:2], print)
          }
          ```





          share|improve this answer





















          • Hi thank you very much for your time and reply. It works great and solved my problem! much appreciated!
            – JL B
            Nov 23 '18 at 10:32










          • @JLB happy to help!
            – PoGibas
            Nov 23 '18 at 10:32











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53444333%2fdynamic-tabsets-with-multiple-plots-r-markdown%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          There are a couple of improvements you can do.




          1. Create cat header function with arguments for text and level.


          With it you don't need to call cat multiple times and it creates wanted number of # automatically.



          catHeader <- function(text = "", level = 3) {
          cat(paste0("nn",
          paste(rep("#", level), collapse = ""),
          " ", text, "n"))
          }




          1. print plots using lapply.


          Full code looks like this:



          ---
          title: "R Notebook"
          output:
          html_document:
          df_print: paged
          editor_options:
          chunk_output_type: inline
          ---


          ```{r, functions}
          catHeader <- function(text = "", level = 3) {
          cat(paste0("nn",
          paste(rep("#", level), collapse = ""),
          " ", text, "n"))
          }
          ```

          ### header 1

          ```{r}
          library(ggplot2)
          df <- mtcars

          pl_list <- list()

          pl1 <- qplot(cyl, disp, data = df[1:12,])
          pl2 <- qplot(mpg, cyl, data = df[13:20,])
          pl3 <- qplot(mpg, cyl, data = df[21:30,])
          pl4 <- qplot(mpg, cyl, data = df[1:12,])

          pl_list[[1]] <- list(pl1, pl3, "one")
          pl_list[[2]] <- list(pl2, pl4, "two")
          ```


          ## header {.tabset}

          ```{r, results = "asis", echo = FALSE}

          for(i in seq_along(pl_list)){
          tmp <- pl_list[[i]]
          # As you want to use tabset level here has to be lower than
          # parent level (ie, parent is 2, so here you have to use 3)
          catHeader(tmp[[3]], 3)
          lapply(tmp[1:2], print)
          }
          ```





          share|improve this answer





















          • Hi thank you very much for your time and reply. It works great and solved my problem! much appreciated!
            – JL B
            Nov 23 '18 at 10:32










          • @JLB happy to help!
            – PoGibas
            Nov 23 '18 at 10:32
















          0














          There are a couple of improvements you can do.




          1. Create cat header function with arguments for text and level.


          With it you don't need to call cat multiple times and it creates wanted number of # automatically.



          catHeader <- function(text = "", level = 3) {
          cat(paste0("nn",
          paste(rep("#", level), collapse = ""),
          " ", text, "n"))
          }




          1. print plots using lapply.


          Full code looks like this:



          ---
          title: "R Notebook"
          output:
          html_document:
          df_print: paged
          editor_options:
          chunk_output_type: inline
          ---


          ```{r, functions}
          catHeader <- function(text = "", level = 3) {
          cat(paste0("nn",
          paste(rep("#", level), collapse = ""),
          " ", text, "n"))
          }
          ```

          ### header 1

          ```{r}
          library(ggplot2)
          df <- mtcars

          pl_list <- list()

          pl1 <- qplot(cyl, disp, data = df[1:12,])
          pl2 <- qplot(mpg, cyl, data = df[13:20,])
          pl3 <- qplot(mpg, cyl, data = df[21:30,])
          pl4 <- qplot(mpg, cyl, data = df[1:12,])

          pl_list[[1]] <- list(pl1, pl3, "one")
          pl_list[[2]] <- list(pl2, pl4, "two")
          ```


          ## header {.tabset}

          ```{r, results = "asis", echo = FALSE}

          for(i in seq_along(pl_list)){
          tmp <- pl_list[[i]]
          # As you want to use tabset level here has to be lower than
          # parent level (ie, parent is 2, so here you have to use 3)
          catHeader(tmp[[3]], 3)
          lapply(tmp[1:2], print)
          }
          ```





          share|improve this answer





















          • Hi thank you very much for your time and reply. It works great and solved my problem! much appreciated!
            – JL B
            Nov 23 '18 at 10:32










          • @JLB happy to help!
            – PoGibas
            Nov 23 '18 at 10:32














          0












          0








          0






          There are a couple of improvements you can do.




          1. Create cat header function with arguments for text and level.


          With it you don't need to call cat multiple times and it creates wanted number of # automatically.



          catHeader <- function(text = "", level = 3) {
          cat(paste0("nn",
          paste(rep("#", level), collapse = ""),
          " ", text, "n"))
          }




          1. print plots using lapply.


          Full code looks like this:



          ---
          title: "R Notebook"
          output:
          html_document:
          df_print: paged
          editor_options:
          chunk_output_type: inline
          ---


          ```{r, functions}
          catHeader <- function(text = "", level = 3) {
          cat(paste0("nn",
          paste(rep("#", level), collapse = ""),
          " ", text, "n"))
          }
          ```

          ### header 1

          ```{r}
          library(ggplot2)
          df <- mtcars

          pl_list <- list()

          pl1 <- qplot(cyl, disp, data = df[1:12,])
          pl2 <- qplot(mpg, cyl, data = df[13:20,])
          pl3 <- qplot(mpg, cyl, data = df[21:30,])
          pl4 <- qplot(mpg, cyl, data = df[1:12,])

          pl_list[[1]] <- list(pl1, pl3, "one")
          pl_list[[2]] <- list(pl2, pl4, "two")
          ```


          ## header {.tabset}

          ```{r, results = "asis", echo = FALSE}

          for(i in seq_along(pl_list)){
          tmp <- pl_list[[i]]
          # As you want to use tabset level here has to be lower than
          # parent level (ie, parent is 2, so here you have to use 3)
          catHeader(tmp[[3]], 3)
          lapply(tmp[1:2], print)
          }
          ```





          share|improve this answer












          There are a couple of improvements you can do.




          1. Create cat header function with arguments for text and level.


          With it you don't need to call cat multiple times and it creates wanted number of # automatically.



          catHeader <- function(text = "", level = 3) {
          cat(paste0("nn",
          paste(rep("#", level), collapse = ""),
          " ", text, "n"))
          }




          1. print plots using lapply.


          Full code looks like this:



          ---
          title: "R Notebook"
          output:
          html_document:
          df_print: paged
          editor_options:
          chunk_output_type: inline
          ---


          ```{r, functions}
          catHeader <- function(text = "", level = 3) {
          cat(paste0("nn",
          paste(rep("#", level), collapse = ""),
          " ", text, "n"))
          }
          ```

          ### header 1

          ```{r}
          library(ggplot2)
          df <- mtcars

          pl_list <- list()

          pl1 <- qplot(cyl, disp, data = df[1:12,])
          pl2 <- qplot(mpg, cyl, data = df[13:20,])
          pl3 <- qplot(mpg, cyl, data = df[21:30,])
          pl4 <- qplot(mpg, cyl, data = df[1:12,])

          pl_list[[1]] <- list(pl1, pl3, "one")
          pl_list[[2]] <- list(pl2, pl4, "two")
          ```


          ## header {.tabset}

          ```{r, results = "asis", echo = FALSE}

          for(i in seq_along(pl_list)){
          tmp <- pl_list[[i]]
          # As you want to use tabset level here has to be lower than
          # parent level (ie, parent is 2, so here you have to use 3)
          catHeader(tmp[[3]], 3)
          lapply(tmp[1:2], print)
          }
          ```






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 10:11









          PoGibasPoGibas

          15.8k144175




          15.8k144175












          • Hi thank you very much for your time and reply. It works great and solved my problem! much appreciated!
            – JL B
            Nov 23 '18 at 10:32










          • @JLB happy to help!
            – PoGibas
            Nov 23 '18 at 10:32


















          • Hi thank you very much for your time and reply. It works great and solved my problem! much appreciated!
            – JL B
            Nov 23 '18 at 10:32










          • @JLB happy to help!
            – PoGibas
            Nov 23 '18 at 10:32
















          Hi thank you very much for your time and reply. It works great and solved my problem! much appreciated!
          – JL B
          Nov 23 '18 at 10:32




          Hi thank you very much for your time and reply. It works great and solved my problem! much appreciated!
          – JL B
          Nov 23 '18 at 10:32












          @JLB happy to help!
          – PoGibas
          Nov 23 '18 at 10:32




          @JLB happy to help!
          – PoGibas
          Nov 23 '18 at 10:32


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53444333%2fdynamic-tabsets-with-multiple-plots-r-markdown%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Berounka

          Sphinx de Gizeh

          Different font size/position of beamer's navigation symbols template's content depending on regular/plain...