Based on some simple tests, interactive() is true when running code within rmarkdown::render() or knitr::knit2html(). That is, a simp
interactive()
rmarkdown::render()
knitr::knit2html()
As Yihui suggested on github isTRUE(getOption('knitr.in.progress')) can be used to detect whether code is being knitted or executed interactively.
isTRUE(getOption('knitr.in.progress'))