Is there a way to find out if an R script is working directly or inside another script?

I am using R studio.

Is there a way to find out if the R script is executed directly either by the source command in the console), or inside another script. i.e. another script is received, and this is a call to the first script.

This may be useful for querying some values โ€‹โ€‹in some cases.

What I am doing now is setting the variable to true or false, and in the script I check this variable. It works, but the automatic way is better.

Thank you for your time.

EDIT: More info

Suppose I have an independent script that works just as it is, but this script is part of the process that runs after the completion of another script. If I need to run both, I can run the first, then the second; but also I have a chance to just launch a second one.

I ask if there is a way (in the second scenario) to check if this second was called from the first or not.

Take a look at his simple examples (inspired by Greg Snow 's answer). First the file that I call in Rstudio

# scripta.R writeLines("script A") if (interactive()) writeLines("interactive: true") else writeLines("interactive false") source("scriptb.r") writelines("after B") 

Then file received

 # scriptb.R writeLines("script B") if (interactive()) writeLines("interactive: true") else writeLines("interactive false") writeLines("end B") 

Result in Rstudio

 script A interactive: true script B interactive: true end B after B 

I like to have something like

 script A interactive: true script B interactive: false end B after B 

I hope itโ€™s now clearer.

thanks

+8
source share
3 answers

Not a direct answer to your question, but a related one is to look at the interactive function. This function will return TRUE if R believes that you are in an interactive session, and it is reasonable to assume that a person can answer questions, he will return FALSE if he works in BATCH mode, and is quite sure that there are no people (or aliens, smart animals, etc.) to answer questions.

Not quite what you requested, but it can be helpful to decide whether to request information.

+5
source

If I understand correctly, a simple message() command should do what (I think) you need. Since you call one of several scripts based on logical checks, the message that echoes is repeated at the beginning of each script, for example:

message("R has now entered script_1.R \n")

must do it. If the script is never called because some variable is set to FALSE , you will never see this message.

If you need to request and read values โ€‹โ€‹from the console by adding a line, for example:

new_input <- readline("Enter a value for x: ")

will also be useful to you.

+1
source

It can do it. First, the script that you run

 # script-AR cat("script A\n") if (interactive()) cat("interactive: true\n") else cat("interactive: false\n") source_func <- function(file){ # check that the variable does not exist already pa <- .GlobalEnv has_old <- if(exists("is_sourced", where = pa)) stop(sQuote("is_sourced"), " exists already") pa$is_sourced <- TRUE # make sure to remove the variable again on.exit(rm(is_sourced, envir = pa)) source( file, # .GlobalEnv is used anyway but now it is easy to change to another environment local = pa) invisible() } source_func("script-BR") cat("after B\n") 

Then the script that you source

 # script-BR if(!exists("is_sourced")) if(!interactive()) # throw an error if interactive and 'is_sourced' is not set is_sourced <- FALSE else stop("Set ", sQuote("is_sourced")) if (interactive() && !is_sourced) cat("interactive: true\n") else cat("interactive: false\n") cat("end B\n") 

The result of running script-AR :

 script A interactive: true script B interactive: false end B after B 
0
source

All Articles