Check if code is running in RStudio, R Markdown file, R Jupyter Notebook or other contexts. And if is in R Markdown file, check the output format.
where() is_rmd() is_rstudio() is_jupyter()
For where():
If executed in R Markdown file, it returns the output format.
If output format is not specified, it returns "rmd".
If executed in RStudio, it returns "rstudio".
If executed in R Jupyter Notebook, it returns "jupyter".
If executed in other contexts, it returns "other".
is_rmd(), is_rstudio() and is_jupyter() return TRUE if executed
in their corresponding contexts, or FALSE if not.