Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

renderText ejects header class into document #133

Closed
garrettgman opened this issue Jun 18, 2014 · 9 comments
Closed

renderText ejects header class into document #133

garrettgman opened this issue Jun 18, 2014 · 9 comments
Labels
bug an unexpected problem or unintended behavior
Milestone

Comments

@garrettgman
Copy link
Member

When I place reactive text into a header, it appears and reactivity works. However, part of the html also appears in the document, which looks bad.

For example, this .Rmd file


---
title: "Hurricanes"
runtime: shiny
output: html_document

---

Between 1995 and 2005, 79 "named" storms developed in the Atlantic Ocean.
These are storms that reached Tropical Depression status or greater.

```{r echo = FALSE}
selectInput("data", "Choose a state to explore:", choices = state.name)
```

## `r renderText(input$data)`

renders as

screen shot 2014-06-18 at 11 13 37 am

@jjallaire
Copy link
Member

I believe that this will be addressed by this shiny PR: rstudio/shiny#512

@yihui is that correct? If so I'll close this issue here.

@yihui
Copy link
Member

yihui commented Jun 18, 2014

That PR only addresses part of this issue. It uses <span> instead of <div> for renderText(), since div is not legitimate in <h2>.

I tried to debug this problem, but did not succeed. It seems Pandoc was confused by the HTML tag in ##, but when I tried to convert the markdown document below to HTML, Pandoc actually worked fine:

Hi.

## <span id="out966b648308c66c43" class="shiny-text-output shiny-bound-output"></span>

Whatever.

Note it works when renderText() is in a paragraph. I guess the problem is related to --section-divs, but I failed to find out which step it went wrong.

@elephantum
Copy link

+1 to the original problem, it would be nice to be able to have dynamic text in headers.

Please, fix :)

@yihui
Copy link
Member

yihui commented Sep 21, 2014

This problem turns out to be a little tricky. I have found out the reason but do not know how to fix it. We probably need @jcheng5's input here, since it is an issue caused by htmltools::htmlPreserve().

abc

## `r renderText('foo')`

def

The HTML output from shiny in Markdown is first "preserved" like this:

abc

## <!--html_preserve--><span id="a" class="b">foo</span><!--/html_preserve-->

def

Then the preserved chunk is replaced by a random ID (here preserveRandomID == '<!--html_preserve--><span id="a" class="b">foo</span><!--/html_preserve-->'):

abc

## preserveRandomID

def

The Markdown document is passed to Pandoc now, and turned to HTML:

<p>abc</p>
<div id="preserveRandomID" class="section level2">
<h2>preserveRandomID</h2>
<p>def</p>
</div>

Note the section is wrapped in a div with an id preserveRandomID. Then htmltools::restorePreserveChunks() will replace all preserveRandomID with the original HTML fragments. This is where the problem came from:

<p>abc</p>
<div id="<!--html_preserve--><span id="a" class="b">foo</span><!--/html_preserve-->" class="section level2">
<h2><!--html_preserve--><span id="a" class="b">foo</span><!--/html_preserve--></h2>
<p>def</p>
</div>

Only the second preserveRandomID should be restored, but there seems to be no way for htmltools to know which preserveRandomID is the original one.

One possible solution that I can think of is to use <span>preserveRandomID</span> as the random id, which leads to HTML from Pandoc like this:

<p>abc</p>
<div id="preserveRandomID" class="section level2">
<h2><span>preserveRandomID</span></h2>
<p>def</p>
</div>

Then we restore <span>preserveRandomID</span> to the original HTML.

@yihui yihui added the bug an unexpected problem or unintended behavior label Sep 21, 2014
@elephantum
Copy link

Meanwhile, I found a workaround. Instead of making pandoc generate value for div/@id you can provide one explicitly: {#foo}

abc

## `r renderText('foo')` {#foo}

def

This way preserveRandomID would not go into id= contents.

Relevant section in pandoc: http://johnmacfarlane.net/pandoc/README.html#header-identifiers-in-html-latex-and-context

@yihui
Copy link
Member

yihui commented Sep 21, 2014

@elephantum Oh nice trick. Thanks!

@elephantum
Copy link

Another way would be to turn off auto_identifiers and ascii_identifiers markdown extensions as described in http://johnmacfarlane.net/pandoc/README.html#pandocs-markdown

@yihui yihui added this to the v1.7 milestone Aug 30, 2017
@yihui
Copy link
Member

yihui commented Aug 30, 2017

This will be fixed by #1131.

jjallaire added a commit that referenced this issue Aug 31, 2017
Fix #133: remove the id of the preserved HTML chunk when it is in a section id attribute
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 3, 2020
@github-actions
Copy link

github-actions bot commented Nov 3, 2020

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

4 participants