0

I am using a for loop to generate model results and printing them in r markdown. To keep it tidy it would be nice to print comments before printing the model result.

Is there a way to print commented text behind hashtags in R?

e.g.

for(i in c(1:10)){
#this is the number
print(i)}

vs

#this is the number
print(1)
#this is the number
print(2)
#this is the number
print(3)
#this is the number
print(4)
mluerig
  • 638
  • 10
  • 25

1 Answers1

0

No need to try to print comment in my opinion. You can use cat("this is the number", i, "\n") within your loop. Eg:

for (i in 1:5)
  cat("this is number", i, "\n")

this is number 1 
this is number 2 
this is number 3 
this is number 4 
this is number 5 

message instead of cat, tends to be preferred as we can easily get rid of them with suppressMessages or, better, message=FALSE in knitr/rmarkdown chunks.

Vincent Bonhomme
  • 7,235
  • 2
  • 27
  • 38