1

In R, using knitr, is there a way to prevent line breaks in the HTML when results='hide' and echo=FALSE?

In this case:

First I do this, 
```{r results='hide', echo=FALSE}
x=4;x
```
then I do that.

I get:

First I do this,

then I do that.

with both a break and an extra line between.

I'd like to get:

First I do this, then I do that.

instead.

Generally speaking, I'd like for code chunks to not insert new lines so that markdown is free to eat the one after the first line of text.

Thanks,

softboyled
  • 51
  • 4

2 Answers2

2

I assume you're creating an HTML document from an R Markdown document. In that case, you can use the inline R code capability offered by knitr by using the ` characters starting with the letter r.

Example:

In your R Markdown, write:

First I do this,`r x=4` then I do that. I can call x by doing `r x`.

And as output, you get:

First I do this, then I do that. I can call x by doing 4.

Note that in my example, I evaluated the variable x, but if you do not want to evaluate it, you do not have to. The variable x should still be assigned a value of 4 from the

`r x=4`

part of the R Markdown.

This is Inline R Code, and is documented here under the section "Inline R Code".

EDIT:

Note that Inline R Code has properties that are analogous to "echo=FALSE". And if you want to hide the results from inline R code, you can use base R functions to hide the output. See this question.

Community
  • 1
  • 1
ialm
  • 8,510
  • 4
  • 36
  • 48
  • x=4 is not actually the code that I want to place there. Assuming the insertion of arbitrary code that will be echoed and have its own output that I need to suppress, I believe that I need to use the three-backtick approach so that I have access to the echo and results chunk options. – softboyled Nov 20 '13 at 00:05
  • 1
    @softboyled Your example was not very clear on that point. I believe my answer gives the result you requested in your question. Can you clarify your question with a better example that outlines what you want to achieve? – ialm Nov 20 '13 at 00:12
  • 1
    Agreed. Inline code is the only way to go, as far as I see. At the moment, there is no easy way to suppress the empty line if you use code chunks (three backticks). – Yihui Xie Nov 20 '13 at 02:47
  • @ialm: The question and text both state: "when results='hide' and echo=FALSE" which cannot be done in your scenario. – softboyled Nov 20 '13 at 02:49
  • 1
    @softboyled I took the example provided in your question and created the desired output, albeit using a different construct than you originally proposed to arrive at the result. I apologize if the solution does not work for you, but if you have a better example of what result you want, then there may be better solutions offered by others. BTW, @Yihui is the creator of `knitr`, maybe you can bug him now that he's here :) – ialm Nov 20 '13 at 06:21
  • @iaim Thanks for the tip, but that's not quite what I'm after. I think that it's a 'bug' in the rstudio code. Looking into it... – softboyled Nov 20 '13 at 23:22
0

Try something like:

``` {r , results="asis", echo=F, eval=T} 
if(showMe){
  cat("printed")
} else {
  cat("<!-- no break line -->")
}
```
Ferroao
  • 3,042
  • 28
  • 53