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

[markdown] separate messages in the same chunk are printed in separate <pre><code> sections #534

Closed
renozao opened this issue May 5, 2013 · 10 comments
Labels
feature Feature requests
Milestone

Comments

@renozao
Copy link

renozao commented May 5, 2013

If you run the following in version 1.2:

knit2html(text="blabla\n```{r}\nmessage('a'); message('b')\n```")

you get:

<p>blabla</p>

<pre><code class="r">message(&quot;a&quot;)
</code></pre>

<pre><code>## a
</code></pre>

<pre><code class="r">message(&quot;b&quot;)
</code></pre>

<pre><code>## b
</code></pre>

which makes the displayed chunk very long if many messages are output (e.g., by some functions in verbose mode, whose output is informative and needs to be shown in the document).
Isn't the output supposed to be in a single <pre><code> section? Like in Rnw documents?

Thanks you

@ramnathv
Copy link
Contributor

ramnathv commented May 5, 2013

See here for a potential solution using hooks.

@renozao
Copy link
Author

renozao commented May 6, 2013

Thanks for the tip Ravi.
I believe my issue is however not about spacing between code and output,
and my first example was not very good in showing that.

Consider now:

knit2html(text="blabla\n```{r}\nmsg <- function(){message('a');
message('b')}; msg()\n```", fragment.only=TRUE)

which produces:

<p>blabla</p>

<pre><code class="r">msg &lt;- function() {
    message(&quot;a&quot;)
    message(&quot;b&quot;)
}
msg()
</code></pre>

<pre><code>## a
</code></pre>

<pre><code>## b
</code></pre>

The two messages are really part of the same call and should appear in a
single output chunk (in fact together with the code), like in Rnw documents:

blabla
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969,
0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
msg <- \hlfunctioncall{function}() \{
    \hlfunctioncall{message}(\hlstring{"a"})
    \hlfunctioncall{message}(\hlstring{"b"})
\}
\hlfunctioncall{msg}()
\end{alltt}


{\ttfamily\noindent\itshape\color{messagecolor}{\#\# a}}

{\ttfamily\noindent\itshape\color{messagecolor}{\#\# b}}\end{kframe}
\end{knitrout}

so something like this:

<p>blabla</p>

<pre><code class="r">msg &lt;- function() {
    message(&quot;a&quot;)
    message(&quot;b&quot;)
}
msg()
</code>

<code>## a
</code>

<code>## b
</code></pre>

On 5 May 2013 23:25, Ramnath Vaidyanathan notifications@github.com wrote:

See here ramnathv/slidify#189 for a potential
solution using hooks.


Reply to this email directly or view it on GitHubhttps://github.com//issues/534#issuecomment-17458445
.

@ramnathv
Copy link
Contributor

ramnathv commented May 6, 2013

Can you format your code? It is difficult to make things out currently.

@renozao
Copy link
Author

renozao commented May 6, 2013

Ah ugly indeed... the response by email must have messed things up. Here:

Thanks for the tip Ravi.
I believe my issue is however not about spacing between code and output, and my first example was not very good in showing that.

Consider now:

knit2html(text="blabla\n```{r}\nmsg <- function(){message('a'); message('b')}; msg()\n```", fragment.only=TRUE)

which produces:

<p>blabla</p>

<pre><code class="r">msg &lt;- function() {
    message(&quot;a&quot;)
    message(&quot;b&quot;)
}
msg()
</code></pre>

<pre><code>## a
</code></pre>

<pre><code>## b
</code></pre>

The two messages are really part of the same call and should appear in a single output chunk (in fact together with the code), like in Rnw documents:

blabla
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
msg <- \hlfunctioncall{function}() \{
    \hlfunctioncall{message}(\hlstring{"a"})
    \hlfunctioncall{message}(\hlstring{"b"})
\}
\hlfunctioncall{msg}()
\end{alltt}


{\ttfamily\noindent\itshape\color{messagecolor}{\#\# a}}

{\ttfamily\noindent\itshape\color{messagecolor}{\#\# b}}\end{kframe}
\end{knitrout}

so something like this:

<p>blabla</p>

<pre><code class="r">msg &lt;- function() {
    message(&quot;a&quot;)
    message(&quot;b&quot;)
}
msg()
</code>

<code>## a
</code>

<code>## b
</code></pre>

@ramnathv
Copy link
Contributor

ramnathv commented May 6, 2013

Hook2 mentioned in my link will do the trick. Here is an Rmd file that you can knit and produce output of the format you desire. Try it and let me know how it works.

@renozao
Copy link
Author

renozao commented May 6, 2013

Not working for me, I get:

<p>hook1 &lt;- function(x) {
    gsub(&ldquo;<code>\n*</code>r<em>\n</em>&rdquo;, &ldquo;&rdquo;, x)
}
hook2 &lt;- function(x) {
    print(x)
    gsub(&ldquo;<code>\\\\n+</code>\n&rdquo;, &ldquo;&rdquo;, x)
}
knit_hooks$set(document = hook2)</p>

<p>blabla</p>

<p>msg &lt;- function() {
    message(&ldquo;a&rdquo;)
    message(&ldquo;b&rdquo;)
}
msg()</p>

<h2>a</h2>

<h2>b</h2>

If I print x in hook2 I get:

[1] "hook1 <- function(x) {\n    gsub(\"```\\n*```r*\\n*\", \"\", x)\n}\nhook2 <- function(x) {\n    print(x)\n    gsub(\"```\\n+```\\n\", \"\", x)\n}\nknit_hooks$set(document = hook2)\n"
[2] "\n\nblabla\n"                                                                                                                                                                         
[3] "msg <- function() {\n    message(\"a\")\n    message(\"b\")\n}\nmsg()\n## a\n## b\n"                                                                                                  
[4] ""

so no md markup for code output, and the hook does not do anything.

I believe this should not require to redefine a hook though: these outputs should be gathered in the same output markup section.

@renozao
Copy link
Author

renozao commented May 6, 2013

Ok, not sure why but now your hook2-solution works nicely! (maybe the issue was coming from running it in an interactive R session and residual stuff)

Yihui, I still believe this should be default behaviour though :)
Thanks

@yihui
Copy link
Owner

yihui commented Jun 6, 2013

@renozao I agree with you. In fact this annoyed me from day one.

yihui added a commit to yihui/knitr-examples that referenced this issue Jun 7, 2013
@yihui yihui closed this as completed in 16d5836 Jun 7, 2013
@yihui
Copy link
Owner

yihui commented Jun 7, 2013

Done.

@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature Feature requests
Projects
None yet
Development

No branches or pull requests

3 participants