1

If I have an R script called arscript.r with the following content:

#!/usr/bin/R
newvar = 10

then in the R interactive shell I can do

> source("./arscript.r")
> newvar
[1] 10

I would like to do the equivalent in Python. How would it be possible? Obviously in Python I would write print(newvar)...

TMOTTM
  • 3,286
  • 6
  • 32
  • 63
  • 1
    `"./test.r"` or `"./arscript.r"`? – M-- Mar 28 '19 at 21:52
  • https://stackoverflow.com/questions/35360008/equivalent-of-source-of-r-in-python or https://stackoverflow.com/questions/436198/what-is-an-alternative-to-execfile-in-python-3?noredirect=1&lq=1 – M-- Mar 28 '19 at 22:01

1 Answers1

0

In python you can import a script and then call variables from it:

for example, file blah.py looks like this

text = 'abc'

and another file run.py looks like this:

import blah
print (blah.text)

which prints

> abc

(in this case, the files have to be in the same folder)

philshem
  • 24,761
  • 8
  • 61
  • 127
  • As I said, I would like to be able to continue working on the variables from the script in the interactive shell. – TMOTTM Mar 28 '19 at 21:59
  • so then you need to use something like Jupyter Notebook – philshem Mar 28 '19 at 22:02
  • @TMOTTM and you *can* do that. How, exactly, does this not work for you? – juanpa.arrivillaga Mar 28 '19 at 22:17
  • @juanpa.arrivillaga What do you mean by 'that'? I find it convenient to work with an interactive shell next to a script so I can define a complicated expression that stores a value/array/.. in a variable. Then I can "import" the script in the shell and interactively explore what I can do with this variable. That's what I like about the R interpreter and what I would wish for to do in Python shell. – TMOTTM Mar 29 '19 at 15:44
  • Do you mean Rstudio? – philshem Mar 29 '19 at 15:47