Monday, December 11, 2017

Executing Python Script in RStudio - R + Python in one IDE

Hello there,

Without any doubt what-so-ever, the two most powerful data science programming tools are Python and R. How about if you could run these two in on Integrated Development Environment (IDE)?

That is what this post is all about. I will show you how you could run Python scripts right inside of a popular R environment.

R + Python in one IDE

A quick solution to this lies in RStudio. With the latest version of RStudio, you can use RStudio as a Python REPL. But first you need to download and install Python on your machine just the way you would do R.

RStudio is a free and open-source integrated development environment for R, a programming language for statistical computing and graphics. RStudio was founded by JJ Allaire, creator of the programming language ColdFusion (Source: WikiPedia).



To run R script in RStudio:
Because RStudio was originally made for R, it is fairly striaght forward to run R scripts. Just got to the "Comprehensive R Archive Network" (CRAN) web page to download R. Then also download and install RStudio.


To run Python script in RStudio:
To run Python in the same RStudio environment, go to the official Python web page and download it. During the installation, make sure that it is added to your system "Environment Variable" so that RStudio terminal could recognize it without you calling the full PATH all the time.

With the above in place, you can now write your python script in the RStudio "Script Pane" and save it with .py extension. Then run it and see the output on the "Console Pane".


As seen above, I import 'pandas' module and created a dataframe in the "Script Pane" and the result is displayed on the "Console Pane".

You can also use the interactive terminal to run python scripts by going to: Tools >> Terminal >> New Terminal. From the new terminal window that open, you can call the python interpreter and enter any python command just as you would do on cmd or python IDLE.



That is it!

Note: Apart fro R and Python, RStudio supports other programming languages such as JavaScript, SQL, etc.

No comments:

Post a Comment