R Markdown: hide spoiler text (hover over text element)











up vote
11
down vote

favorite












Is it possible to hide blocks of text within .html files created with R Markdown? The elements of text should be hidden until the user hovers over the text preferentially (or clicks a button). The elements to be hidden do not concern code blocks. Currently I include text within < p> < /p>



It was suggested to hide text blocks by preceding each line with '>!', but R Studio does not recognize this 'markdown' approach. It just returns a text block that starts with '!'. I prefer this simple 'hover' approach though, above Javascript and buttons.



Any suggestions would be welcome. Thanks.










share|improve this question




























    up vote
    11
    down vote

    favorite












    Is it possible to hide blocks of text within .html files created with R Markdown? The elements of text should be hidden until the user hovers over the text preferentially (or clicks a button). The elements to be hidden do not concern code blocks. Currently I include text within < p> < /p>



    It was suggested to hide text blocks by preceding each line with '>!', but R Studio does not recognize this 'markdown' approach. It just returns a text block that starts with '!'. I prefer this simple 'hover' approach though, above Javascript and buttons.



    Any suggestions would be welcome. Thanks.










    share|improve this question


























      up vote
      11
      down vote

      favorite









      up vote
      11
      down vote

      favorite











      Is it possible to hide blocks of text within .html files created with R Markdown? The elements of text should be hidden until the user hovers over the text preferentially (or clicks a button). The elements to be hidden do not concern code blocks. Currently I include text within < p> < /p>



      It was suggested to hide text blocks by preceding each line with '>!', but R Studio does not recognize this 'markdown' approach. It just returns a text block that starts with '!'. I prefer this simple 'hover' approach though, above Javascript and buttons.



      Any suggestions would be welcome. Thanks.










      share|improve this question















      Is it possible to hide blocks of text within .html files created with R Markdown? The elements of text should be hidden until the user hovers over the text preferentially (or clicks a button). The elements to be hidden do not concern code blocks. Currently I include text within < p> < /p>



      It was suggested to hide text blocks by preceding each line with '>!', but R Studio does not recognize this 'markdown' approach. It just returns a text block that starts with '!'. I prefer this simple 'hover' approach though, above Javascript and buttons.



      Any suggestions would be welcome. Thanks.







      r r-markdown knitr






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 9 at 5:35









      jangorecki

      7,25123387




      7,25123387










      asked Jun 25 at 9:27









      Elyakim

      117118




      117118
























          3 Answers
          3






          active

          oldest

          votes

















          up vote
          10
          down vote













          My suggestion is to use CSS for this task.



          The following Rmd file includes some rules to hide the spoiler class elements. You may find other approaches with CSS:



          ---
          output: html_document
          ---

          ```{css, echo=FALSE}
          .spoiler {
          visibility: hidden;
          }

          .spoiler::before {
          visibility: visible;
          content: "Spoiler alert! Hover me to see the answer."
          }

          .spoiler:hover {
          visibility: visible;
          }

          .spoiler:hover::before {
          display: none;
          }
          ```

          You can insert a message in raw `HTML`:
          <p class="spoiler">Answer</p>

          A better approach is to use bracketed spans:
          [This is another answer]{.spoiler}





          share|improve this answer

















          • 1




            Also worth mentioning: fenced divs, which can eliminate the need for raw HTML in the input.
            – tarleb
            Nov 9 at 9:05


















          up vote
          5
          down vote













          This is of course possible. There are several possibilities including linear transformations (animated fading), buttons, no buttons and so on.



          To start with, here is a simple approach using CSS. When not hovered, the font and background colors match and therefore no text is visible. When hovered the background turns white and the text turns black.



          ---
          title: "Hide Code Blocks"
          author: "Martin Schmelzer"
          date: "June 25, 2018"
          output: html_document
          ---

          <style>
          hide {
          background-color: #d6d6d6;
          color: #d6d6d6;
          }
          hide:hover {
          background-color: white;
          color: black;
          }
          </style>

          ## R Markdown

          <hide>This is a hidden text block!</hide>





          share|improve this answer




























            up vote
            5
            down vote



            +50










            Based on the additional part about wanting to reveal a kable'd table, I took a look at some functions from the htmltools package for creating HTML elements. The way I came up with uses just the smallest amount of Javascript, via a js code block.



            Basically, I create a button using shiny::actionButton and attach to it a Javascript click handler by supplying a function name to the onclick attribute. The knit table is inside a HTML wrapper so that it will render properly, inside a <div> element as a container. This container has a hidden property initially set to true.



            The click handler is the only real Javascript to write, and that's a function that finds the <div> by ID, and sets its hidden property to false.



            The RMarkdown:



            ---
            output: html_document
            ---

            ```{r setup, include=FALSE}
            knitr::opts_chunk$set(echo = FALSE)
            ```

            ```{r}
            library(htmltools)

            shiny::actionButton("show_table_button",
            label = "Show table",
            onclick = "button_handler()")

            div(id = "tableContainer",
            hidden = "true",
            HTML(knitr::kable(head(iris), format = "html")))
            ```

            ```{js}
            function button_handler() {
            document.getElementById('tableContainer').hidden = false;
            }
            ```


            That creates:



            hidden = true



            After clicking the button:



            hidden = false



            Note that there are some other packages for writing more complex Javascript code in R—haven't used any to recommend one—but I tried to keep the packages limited to what you would already have from using anything Shiny / HTML Widgets related.






            share|improve this answer





















              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',
              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%2f51020181%2fr-markdown-hide-spoiler-text-hover-over-text-element%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              10
              down vote













              My suggestion is to use CSS for this task.



              The following Rmd file includes some rules to hide the spoiler class elements. You may find other approaches with CSS:



              ---
              output: html_document
              ---

              ```{css, echo=FALSE}
              .spoiler {
              visibility: hidden;
              }

              .spoiler::before {
              visibility: visible;
              content: "Spoiler alert! Hover me to see the answer."
              }

              .spoiler:hover {
              visibility: visible;
              }

              .spoiler:hover::before {
              display: none;
              }
              ```

              You can insert a message in raw `HTML`:
              <p class="spoiler">Answer</p>

              A better approach is to use bracketed spans:
              [This is another answer]{.spoiler}





              share|improve this answer

















              • 1




                Also worth mentioning: fenced divs, which can eliminate the need for raw HTML in the input.
                – tarleb
                Nov 9 at 9:05















              up vote
              10
              down vote













              My suggestion is to use CSS for this task.



              The following Rmd file includes some rules to hide the spoiler class elements. You may find other approaches with CSS:



              ---
              output: html_document
              ---

              ```{css, echo=FALSE}
              .spoiler {
              visibility: hidden;
              }

              .spoiler::before {
              visibility: visible;
              content: "Spoiler alert! Hover me to see the answer."
              }

              .spoiler:hover {
              visibility: visible;
              }

              .spoiler:hover::before {
              display: none;
              }
              ```

              You can insert a message in raw `HTML`:
              <p class="spoiler">Answer</p>

              A better approach is to use bracketed spans:
              [This is another answer]{.spoiler}





              share|improve this answer

















              • 1




                Also worth mentioning: fenced divs, which can eliminate the need for raw HTML in the input.
                – tarleb
                Nov 9 at 9:05













              up vote
              10
              down vote










              up vote
              10
              down vote









              My suggestion is to use CSS for this task.



              The following Rmd file includes some rules to hide the spoiler class elements. You may find other approaches with CSS:



              ---
              output: html_document
              ---

              ```{css, echo=FALSE}
              .spoiler {
              visibility: hidden;
              }

              .spoiler::before {
              visibility: visible;
              content: "Spoiler alert! Hover me to see the answer."
              }

              .spoiler:hover {
              visibility: visible;
              }

              .spoiler:hover::before {
              display: none;
              }
              ```

              You can insert a message in raw `HTML`:
              <p class="spoiler">Answer</p>

              A better approach is to use bracketed spans:
              [This is another answer]{.spoiler}





              share|improve this answer












              My suggestion is to use CSS for this task.



              The following Rmd file includes some rules to hide the spoiler class elements. You may find other approaches with CSS:



              ---
              output: html_document
              ---

              ```{css, echo=FALSE}
              .spoiler {
              visibility: hidden;
              }

              .spoiler::before {
              visibility: visible;
              content: "Spoiler alert! Hover me to see the answer."
              }

              .spoiler:hover {
              visibility: visible;
              }

              .spoiler:hover::before {
              display: none;
              }
              ```

              You can insert a message in raw `HTML`:
              <p class="spoiler">Answer</p>

              A better approach is to use bracketed spans:
              [This is another answer]{.spoiler}






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jun 25 at 11:46









              romles

              3,0661631




              3,0661631








              • 1




                Also worth mentioning: fenced divs, which can eliminate the need for raw HTML in the input.
                – tarleb
                Nov 9 at 9:05














              • 1




                Also worth mentioning: fenced divs, which can eliminate the need for raw HTML in the input.
                – tarleb
                Nov 9 at 9:05








              1




              1




              Also worth mentioning: fenced divs, which can eliminate the need for raw HTML in the input.
              – tarleb
              Nov 9 at 9:05




              Also worth mentioning: fenced divs, which can eliminate the need for raw HTML in the input.
              – tarleb
              Nov 9 at 9:05












              up vote
              5
              down vote













              This is of course possible. There are several possibilities including linear transformations (animated fading), buttons, no buttons and so on.



              To start with, here is a simple approach using CSS. When not hovered, the font and background colors match and therefore no text is visible. When hovered the background turns white and the text turns black.



              ---
              title: "Hide Code Blocks"
              author: "Martin Schmelzer"
              date: "June 25, 2018"
              output: html_document
              ---

              <style>
              hide {
              background-color: #d6d6d6;
              color: #d6d6d6;
              }
              hide:hover {
              background-color: white;
              color: black;
              }
              </style>

              ## R Markdown

              <hide>This is a hidden text block!</hide>





              share|improve this answer

























                up vote
                5
                down vote













                This is of course possible. There are several possibilities including linear transformations (animated fading), buttons, no buttons and so on.



                To start with, here is a simple approach using CSS. When not hovered, the font and background colors match and therefore no text is visible. When hovered the background turns white and the text turns black.



                ---
                title: "Hide Code Blocks"
                author: "Martin Schmelzer"
                date: "June 25, 2018"
                output: html_document
                ---

                <style>
                hide {
                background-color: #d6d6d6;
                color: #d6d6d6;
                }
                hide:hover {
                background-color: white;
                color: black;
                }
                </style>

                ## R Markdown

                <hide>This is a hidden text block!</hide>





                share|improve this answer























                  up vote
                  5
                  down vote










                  up vote
                  5
                  down vote









                  This is of course possible. There are several possibilities including linear transformations (animated fading), buttons, no buttons and so on.



                  To start with, here is a simple approach using CSS. When not hovered, the font and background colors match and therefore no text is visible. When hovered the background turns white and the text turns black.



                  ---
                  title: "Hide Code Blocks"
                  author: "Martin Schmelzer"
                  date: "June 25, 2018"
                  output: html_document
                  ---

                  <style>
                  hide {
                  background-color: #d6d6d6;
                  color: #d6d6d6;
                  }
                  hide:hover {
                  background-color: white;
                  color: black;
                  }
                  </style>

                  ## R Markdown

                  <hide>This is a hidden text block!</hide>





                  share|improve this answer












                  This is of course possible. There are several possibilities including linear transformations (animated fading), buttons, no buttons and so on.



                  To start with, here is a simple approach using CSS. When not hovered, the font and background colors match and therefore no text is visible. When hovered the background turns white and the text turns black.



                  ---
                  title: "Hide Code Blocks"
                  author: "Martin Schmelzer"
                  date: "June 25, 2018"
                  output: html_document
                  ---

                  <style>
                  hide {
                  background-color: #d6d6d6;
                  color: #d6d6d6;
                  }
                  hide:hover {
                  background-color: white;
                  color: black;
                  }
                  </style>

                  ## R Markdown

                  <hide>This is a hidden text block!</hide>






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jun 25 at 11:33









                  Martin Schmelzer

                  12.4k23561




                  12.4k23561






















                      up vote
                      5
                      down vote



                      +50










                      Based on the additional part about wanting to reveal a kable'd table, I took a look at some functions from the htmltools package for creating HTML elements. The way I came up with uses just the smallest amount of Javascript, via a js code block.



                      Basically, I create a button using shiny::actionButton and attach to it a Javascript click handler by supplying a function name to the onclick attribute. The knit table is inside a HTML wrapper so that it will render properly, inside a <div> element as a container. This container has a hidden property initially set to true.



                      The click handler is the only real Javascript to write, and that's a function that finds the <div> by ID, and sets its hidden property to false.



                      The RMarkdown:



                      ---
                      output: html_document
                      ---

                      ```{r setup, include=FALSE}
                      knitr::opts_chunk$set(echo = FALSE)
                      ```

                      ```{r}
                      library(htmltools)

                      shiny::actionButton("show_table_button",
                      label = "Show table",
                      onclick = "button_handler()")

                      div(id = "tableContainer",
                      hidden = "true",
                      HTML(knitr::kable(head(iris), format = "html")))
                      ```

                      ```{js}
                      function button_handler() {
                      document.getElementById('tableContainer').hidden = false;
                      }
                      ```


                      That creates:



                      hidden = true



                      After clicking the button:



                      hidden = false



                      Note that there are some other packages for writing more complex Javascript code in R—haven't used any to recommend one—but I tried to keep the packages limited to what you would already have from using anything Shiny / HTML Widgets related.






                      share|improve this answer

























                        up vote
                        5
                        down vote



                        +50










                        Based on the additional part about wanting to reveal a kable'd table, I took a look at some functions from the htmltools package for creating HTML elements. The way I came up with uses just the smallest amount of Javascript, via a js code block.



                        Basically, I create a button using shiny::actionButton and attach to it a Javascript click handler by supplying a function name to the onclick attribute. The knit table is inside a HTML wrapper so that it will render properly, inside a <div> element as a container. This container has a hidden property initially set to true.



                        The click handler is the only real Javascript to write, and that's a function that finds the <div> by ID, and sets its hidden property to false.



                        The RMarkdown:



                        ---
                        output: html_document
                        ---

                        ```{r setup, include=FALSE}
                        knitr::opts_chunk$set(echo = FALSE)
                        ```

                        ```{r}
                        library(htmltools)

                        shiny::actionButton("show_table_button",
                        label = "Show table",
                        onclick = "button_handler()")

                        div(id = "tableContainer",
                        hidden = "true",
                        HTML(knitr::kable(head(iris), format = "html")))
                        ```

                        ```{js}
                        function button_handler() {
                        document.getElementById('tableContainer').hidden = false;
                        }
                        ```


                        That creates:



                        hidden = true



                        After clicking the button:



                        hidden = false



                        Note that there are some other packages for writing more complex Javascript code in R—haven't used any to recommend one—but I tried to keep the packages limited to what you would already have from using anything Shiny / HTML Widgets related.






                        share|improve this answer























                          up vote
                          5
                          down vote



                          +50







                          up vote
                          5
                          down vote



                          +50




                          +50




                          Based on the additional part about wanting to reveal a kable'd table, I took a look at some functions from the htmltools package for creating HTML elements. The way I came up with uses just the smallest amount of Javascript, via a js code block.



                          Basically, I create a button using shiny::actionButton and attach to it a Javascript click handler by supplying a function name to the onclick attribute. The knit table is inside a HTML wrapper so that it will render properly, inside a <div> element as a container. This container has a hidden property initially set to true.



                          The click handler is the only real Javascript to write, and that's a function that finds the <div> by ID, and sets its hidden property to false.



                          The RMarkdown:



                          ---
                          output: html_document
                          ---

                          ```{r setup, include=FALSE}
                          knitr::opts_chunk$set(echo = FALSE)
                          ```

                          ```{r}
                          library(htmltools)

                          shiny::actionButton("show_table_button",
                          label = "Show table",
                          onclick = "button_handler()")

                          div(id = "tableContainer",
                          hidden = "true",
                          HTML(knitr::kable(head(iris), format = "html")))
                          ```

                          ```{js}
                          function button_handler() {
                          document.getElementById('tableContainer').hidden = false;
                          }
                          ```


                          That creates:



                          hidden = true



                          After clicking the button:



                          hidden = false



                          Note that there are some other packages for writing more complex Javascript code in R—haven't used any to recommend one—but I tried to keep the packages limited to what you would already have from using anything Shiny / HTML Widgets related.






                          share|improve this answer












                          Based on the additional part about wanting to reveal a kable'd table, I took a look at some functions from the htmltools package for creating HTML elements. The way I came up with uses just the smallest amount of Javascript, via a js code block.



                          Basically, I create a button using shiny::actionButton and attach to it a Javascript click handler by supplying a function name to the onclick attribute. The knit table is inside a HTML wrapper so that it will render properly, inside a <div> element as a container. This container has a hidden property initially set to true.



                          The click handler is the only real Javascript to write, and that's a function that finds the <div> by ID, and sets its hidden property to false.



                          The RMarkdown:



                          ---
                          output: html_document
                          ---

                          ```{r setup, include=FALSE}
                          knitr::opts_chunk$set(echo = FALSE)
                          ```

                          ```{r}
                          library(htmltools)

                          shiny::actionButton("show_table_button",
                          label = "Show table",
                          onclick = "button_handler()")

                          div(id = "tableContainer",
                          hidden = "true",
                          HTML(knitr::kable(head(iris), format = "html")))
                          ```

                          ```{js}
                          function button_handler() {
                          document.getElementById('tableContainer').hidden = false;
                          }
                          ```


                          That creates:



                          hidden = true



                          After clicking the button:



                          hidden = false



                          Note that there are some other packages for writing more complex Javascript code in R—haven't used any to recommend one—but I tried to keep the packages limited to what you would already have from using anything Shiny / HTML Widgets related.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 11 at 18:35









                          camille

                          6,76131327




                          6,76131327






























                              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.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • 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%2f51020181%2fr-markdown-hide-spoiler-text-hover-over-text-element%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

                              Florida Star v. B. J. F.

                              Error while running script in elastic search , gateway timeout

                              Adding quotations to stringified JSON object values