I am using R studio.
Is there a way to know if an R script is running directly either by the source command in the console) or within another script. ie. another script is sourced and this has the call to the first script.
This can be useful to prompt for some values in some cases.
What I am doing now is to set a variable to true or false and within the script I check for that variable. This works but an automatic way is better.
Thanks for your time.
Let's say I have an independent script that runs fine as is, but this script is part of a process to run after another script finished. If I have to run both, I can run the first, then the second; but also I have the chance just to run the second.
What I am asking is if there is a way to (in the second script) to verify if this second was called from the first or no.
Take a look at his simple examples (inspired by the answer from Greg Snow). First the file 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 the file being sourced
# scriptb.R
writeLines("script B")
if (interactive()) writeLines("interactive: true") else writeLines("interactive false")
writeLines("end B")
The result in Rstudio is
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 now is more clear.
Thanks
In RStudio, select "New Script" from the "File" menu. You'll now see a "Script" panel appear. Try typing a command into this panel and then press the "Run" button shown below. You should see the results of running the script appear in the console panel.
You can execute R script as you would normally do by using the Windows command line. If your R version is different, then change the path to Rscript.exe. Use double quotes if the file path contains space.
RStudio Workbench (previously RStudio Server Pro) gives you the ability to open multiple concurrent sessions. Multiple concurrent sessions can be useful when you want to: Run multiple analyses in parallel.
The R console is interactive, we can directly enter and execute commands here, or we write our script in the code editor and “Run” the script which will then be executed “line-by-line” in the R console (shown later).
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 a person is available to answer questions, it will return FALSE
if running in BATCH mode and it is fairly certain that there are no humans (or aliens, intelligent animals, etc.) to answer questions.
Not exactly what you were asking, but it may be helpful for deciding whether to prompt for information.
If I understand correctly, a simple message()
command should do what (I think) you need. Since you are calling one of several scripts based on logical checks, then having a message echoed at the start of each script like:
message("R has now entered script_1.R \n")
should do it. If a script is never called because some variable is set to FALSE
, then you will never see this message.
If you need to prompt for, and read values from the console, then inserting a line like:
new_input <- readline("Enter a value for x: ")
would also be of use to you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With