-
-
Notifications
You must be signed in to change notification settings - Fork 872
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
Comments
See here for a potential solution using hooks. |
Thanks for the tip Ravi. 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 <- function() {
message("a")
message("b")
}
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 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 <- function() {
message("a")
message("b")
}
msg()
</code>
<code>## a
</code>
<code>## b
</code></pre> On 5 May 2013 23:25, Ramnath Vaidyanathan notifications@github.com wrote:
|
Can you format your code? It is difficult to make things out currently. |
Ah ugly indeed... the response by email must have messed things up. Here: Thanks for the tip Ravi. 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 <- function() {
message("a")
message("b")
}
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 <- function() {
message("a")
message("b")
}
msg()
</code>
<code>## a
</code>
<code>## b
</code></pre> |
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. |
Not working for me, I get: <p>hook1 <- function(x) {
gsub(“<code>\n*</code>r<em>\n</em>”, “”, x)
}
hook2 <- function(x) {
print(x)
gsub(“<code>\\\\n+</code>\n”, “”, x)
}
knit_hooks$set(document = hook2)</p>
<p>blabla</p>
<p>msg <- function() {
message(“a”)
message(“b”)
}
msg()</p>
<h2>a</h2>
<h2>b</h2> If I print [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. |
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 :) |
@renozao I agree with you. In fact this annoyed me from day one. |
Done. |
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. |
If you run the following in version 1.2:
you get:
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
The text was updated successfully, but these errors were encountered: