2

I have a print statement in R as a macro in Markdown

```{r chunk_name, echo=FALSE}
if ( any(so.results$Duration <0.0))
{print("there are non-positive  durations")
} else
{print("all  durations are fine!")}
```

and I want to generate a PDF, not an XML! How can I print "all durations are fine!" in colour for example green or red?

maniA
  • 1,437
  • 2
  • 21
  • 42
  • 3
    [this](http://stackoverflow.com/questions/26819258/format-text-inside-r-code-chunk) and [this](http://stackoverflow.com/questions/29067541/rmarkdown-how-to-change-the-font-color) seem to have the answer. Change to `results='asis'` and then `print("\textcolor{red}{all durations are fine!}")` should work – user20650 Nov 18 '15 at 11:46
  • No unfortunately it dose not work, I guess it would work if I would be able to change the colour in R print-statement – maniA Nov 18 '15 at 13:08
  • 2
    Hi, can you explain what do you mean "it doesnt work". This prints red text when i render the pdf ... have i misunderstood? Or more generally, you could define new print function statement, that takes a colour argument ... `colprint <- function(x, col="red") print(paste0("\textcolor{", col, "}{", x, "}"))` – user20650 Nov 18 '15 at 13:48
  • @user20650 thank you that works naturally if I change ```{r, results='asis'} then I get not just a colour but the whole code too! Is it any possibility to get just the text in colour without to get the code – maniA Nov 18 '15 at 15:00
  • @user20650 every thing is now fine, I used the following code: ` ```{r, results='asis', echo=FALSE} if ( any(so.results$Duration <0.0)) {cat("\n") print("\textcolor{blue}{print("there are non-positive durations}") } else {cat("\n") print("\textcolor{red}{print("all durations are fine!}")} ``` ` and that works proper. Thanks a lot, would you give your comment as an answer please! – maniA Nov 18 '15 at 16:05

1 Answers1

1

Using HERE and HERE and the hints given by User I wrote the following:

 ```{r, results='asis', echo=FALSE}
 if ( any(so.results$Duration <0.0)) 
  {cat("\n") print("\textcolor{blue}
   {print("there are non-positive durations}") 
} else 
{cat("\n") print("\textcolor{red}{print("all durations are fine!}")}
 ```

which solves the problem.

Community
  • 1
  • 1
maniA
  • 1,437
  • 2
  • 21
  • 42