Reputation: 41
I have a function in an Rscript (this is inside an r-package of my own) that is been called either by an .R
(Rscript) file or an .Rmd
(rmarkdown).
I need to create an if statement inside this function to do things depending if the function is called by either the Rscript or the Rmarkdown.
Upvotes: 1
Views: 85
Reputation: 41
In my case the following solves the issue
Getting the file path that also includes filename and extension:
file_type <- try(rstudioapi::getSourceEditorContext()$path, silent = T)
Getting the file extension.
file_type <- tools::file_ext(file_type)
Then iff file_type = 'R'
the functionalities are turned on and for any other cases they are off.
if(file_type == 'R'){
...
}
In this specific case I only care that the extension is a '.R' and don't want to risk using the functionalities with anything else.
For example if you are running the function in the console then file_type = ''
and the functionalities will be off.
Upvotes: 1