I solved this with a bash script and completely removing this dream piece from the R script.
Rscript -e 'knitr::knit("markdown_file.Rmd", "source_file.R", tangle = T)'
Thus, with this approach, I could use a similar bash script to embed my .Rmd into the desired documentation output (for example, pdf, code not shown), then this line of code to create an R file, which can be used by other R scripts. In fact, I remove the need to call knitin the Rmd file (or inside RStudio).
source
share