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!
Undeniably believe that which you stated. Your favorite reason appeared to be
on the internet the simplest thing to be
aware of. I say to you, I certainly get irked while people consider worries that they plainly don’t know about.
You managed to hit the nail upon the top as well as defined out
the whole thing without having side effect , people can take a signal.
Will probably be back to get more. Thanks
I do accept as true with all the concepts you have introduced in your post.
They’re very convincing and can certainly work.
Still, the posts are too quick for novices. Could you please extend them a bit from
subsequent time? Thank you for the post.
Thank you for sharing your info. I truly appreciate your efforts
and I will be waiting for your next write ups thank you once again.
Hi, i think that i saw you visited my site thus i came to “return the favor”.I am trying to
find things to enhance my site!I suppose its ok to use a few of your ideas!!
It’s hard to find experienced people for this topic, however,
you sound like you know what you’re talking about!
Thanks
Tremendous things here. I’m very satisfied to peer your article.
Thank you so much and I’m taking a look forward to contact you.
Will you please drop me a e-mail?
This paragraph is in fact a nice one it helps new net people, who are
wishing for blogging.
This post presents clear idea in favor of the new visitors of blogging, that actually how to do running a blog.
Its not my first time to visit this site,
i am browsing this website dailly and obtain nice data from here everyday.
Asking questions are genuinely good thing if you are not understanding anything fully, but this post provides nice understanding even.
Wow that was odd. I just wrote an incredibly long comment but after I clicked submit my comment didn’t show up.
Grrrr… well I’m not writing all that over again. Anyway, just wanted to
say great blog!
I am really enjoying the theme/design of your website.
Do you ever run into any web browser compatibility problems?
A small number of my blog audience have complained about my blog
not operating correctly in Explorer but looks great in Chrome.
Do you have any recommendations to help fix this issue?
Hi there! I know this is kind of off topic but
I was wondering if you knew where I could find a
captcha plugin for my comment form? I’m using the same blog platform as yours
and I’m having difficulty finding one? Thanks a lot!