In this blog post, I describe to ways to deal with code you do not want to run when it is sourced from an external file in R.
When Python sources code it does a particular thing, it assigns the string ‘__main__’ to the __name__ variable in that code. However, if Python sources code from a module it will assign the filename of the code to the __name__ variable and strip the ‘.py’ from it. This makes it easy for you if you want to run code within a file, but don’t want it to run when your code is sourced from another file.
In this example, foo() will only run when the code isn’t sourced from an external file (e.g. with import foo).
if __name__ == '__main__':
...your code...
This is extremely useful if your script is a library, and running the script an sich is only for doing unit tests or for tinkering.It’s somewhat of a pitty that this doesn’t exist in R. However, there are some workarounds that offer similar functionality.
This chunk of code will check if the source code is actually the environment (or frame) with level 0 — e.g. the .GlobalEnv. If this is new to you, that’s fine, since this is some hard-core under the hood level stuff. If you want to know more: Hadley Wickham has written an in-depth guide to environments.
if (sys.nframe() == 0) {
...your code...
}
Another way you could approach this, is by using options. This way, you could set an option in an external script, source your code and only run certain chunks when the options meets the criteria. This should be in your main.R file.
main <- function() {
...your code...
}
if (getOption('run.main', default=TRUE)) {
main()
}
And this should be in the file where you’re calling main.R from, if you don’t want …your code… to run.
options(run.main=FALSE)
source('main.R')
By the way, if you’re having trouble understanding some of the code and concepts, I can highly recommend “An Introduction to Statistical Learning: with Applications in R”, which is the must-have data science bible. If you simply need an introduction into R, and less into the Data Science part, I can absolutely recommend this book by Richard Cotton. Hope it helps!
Great success!
Your point of view caught my eye and was very interesting. Thanks. I have a question for you. https://accounts.binance.com/zh-TC/register-person?ref=IJFGOAID
Your article helped me a lot, is there any more related content? Thanks! https://accounts.binance.com/de-CH/register-person?ref=53551167
Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me? https://www.binance.info/pt-PT/join?ref=DB40ITMB
Tremendous things here. I’m very satisfied to look your post.
Thank you a lot and I’m taking a look forward to touch you.
Will you please drop me a e-mail?
Hello my family member! I wish to say that this post is awesome, great written and include approximately all vital infos.
I would like to look more posts like this .
Great work! This is the kind of information that
should be shared around the web. Disgrace on the seek engines for not positioning this submit upper!
Come on over and visit my website . Thank you =)
I absolutely love your blog.. Very nice colors & theme. Did you make this website yourself?
Please reply back as I’m looking to create my very own blog and would like to find out where you got this from or just what the theme is called.
Kudos!