3

Typing python -i file.py at the command line runs file.py and then drops into the python terminal preserving the run environment.

https://docs.python.org/3/using/cmdline.html

Is there an equivalent in R?

abalter
  • 9,663
  • 17
  • 90
  • 145

1 Answers1

2

I may be misinterpreting what python -i file.py does, but try:

From inside R, at the terminal, you can do:

source('file.R')

and it will run file.R, with the global environment reflecting what was done in file.R

If you're trying to run from the command line, review this post

Community
  • 1
  • 1
Andrew Taylor
  • 3,438
  • 1
  • 26
  • 47
  • Yes, the idea is from the command line. Thanks for the link. Very useful to see the variety of ways to run an R script at the terminal. Still haven't found one that drops you into the R terminal afterwards, at least I don't see it in the man pages. If you haven't used it, do try `python -i`. I find it indispensable for debugging scripts. – abalter May 31 '16 at 18:21
  • What advantage is there to running the script in such a way that it opens python and puts you into the environment (via `python -i`), as compared to opening R and `source()`ing the file? Aside from the 2 seconds lost due to having to manually open R ;) I use Rmarkdown to do all my reports/analyses/etc so I knit my documents from inside RStudio – Andrew Taylor May 31 '16 at 18:52
  • That is a good point. There is no simple equivalent to `source()` in Python, which is what makes `-i` very handy. Can't off the top of my head think of an example where `source` wouldn't fit the bill. – abalter May 31 '16 at 20:46