1

I am extremely new to R. This simple code prints two outputs:

ec_lineal = function(a,b){
  print(sprintf('%i x + %i = 0',a,b))
  paste(sprintf('x ='), -b / a)
}

ec_lineal(5,3)
[1] "5 x + 3 = 0"
[1] "x = -0.6"

When I knit the code to HTML, I get separate boxes for each of the outputs:

Is there any way to combine both outputs in a single white box? Maybe editing the code chunk header ```{r} ?

I am using R 3.6.3 and the latest version of RStudio in Windows 10.

Thank you.

Phil
  • 5,491
  • 3
  • 26
  • 61
pypau
  • 37
  • 9

1 Answers1

1

You may use cat.

ec_lineal = function(a,b) {
  cat(sprintf('%i x + %i = 0',a,b), 
      paste(sprintf('x ='), -b / a),
      sep="\n")
}

ec_lineal(5,3)
# 5 x + 3 = 0
# x = -0.6
jay.sf
  • 46,523
  • 6
  • 46
  • 87
  • thank you, this will do! just to make sure, what cat() allows me to do is to print things inside one print() but on different lines, right? – pypau Aug 08 '20 at 12:21
  • 1
    @pypau Yeah, it's actually the `sep=` parameter; when you define `"\n"` (i.e. new line) it separates each string defined in `cat` with that. For the difference between `cat` and `print` also see: https://stackoverflow.com/a/31843744/6574038 – jay.sf Aug 08 '20 at 12:28
  • 1
    You're welcome, consider to [accept answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235). – jay.sf Aug 09 '20 at 10:41