Python in R

0 views
Skip to first unread message

Ian McCandliss

unread,
Nov 11, 2018, 1:34:55 PM11/11/18
to PSYC892
So I've spent the past 3 hours banging my head against a wall trying to get "Pystan" to work in R.  

I'm using a MacBook atm, again latest version of R and Rstudio.

So far I've installed Anaconda, and then opened up R in it, and have managed to figure out how to open an R notebook with a python heading, but I am unsure as to how to import specific python modules into the whole thing.  "pandas" seemed to work fine, but from what I've online "pystan" seems to require stan to be compiled in R?  And at that point I've just become very confused.  

Anyone else having issues figuring out how to implement this or am I just the slow one in class?

Andrew Stets

unread,
Nov 11, 2018, 3:08:52 PM11/11/18
to Ian McCandliss, psy...@googlegroups.com
Hi Ian,

So I have a windows 10 OS, but I installed the package "reticulate". It's been just that simple for me. This might work for you too. Also, if you're the slow one in the class than I don't know what that makes me. I'm still working with Stan trying to estimate mean differences. I may have just figured it out, but I don't have much confidence in saying that. 

Cheers,
Andy 

--
Check out the Course Website: https://sites.google.com/site/psyc757/
---
You received this message because you are subscribed to the Google Groups "PSYC892" group.
To unsubscribe from this group and stop receiving emails from it, send an email to psyc892+u...@googlegroups.com.
To post to this group, send email to psy...@googlegroups.com.
Visit this group at https://groups.google.com/group/psyc892.
For more options, visit https://groups.google.com/d/optout.

Kristin Carpenter

unread,
Nov 11, 2018, 4:58:56 PM11/11/18
to Andrew Stets, Ian McCandliss, psy...@googlegroups.com
Hi everyone,

Reticulate was a failure for me. It installs fine, but I continue to get FALSE when I run py_available() . I have done literally all the trouble shooting options for the first 4-5 pages of google results. It seems to be a working directory issue, but I have tried every permutation of solutions to no avail.

I then did some googling and decided to download Anaconda Navigator. So, I did all that jazz, created a new environment with both R Studio and JupyterLab (for Python) and can now use both. But, I don't know that this solves my problem of Pat wanting us to execute Python code within R. In theory it should, as this is a common recommendation for balancing use of both programs. *shrug* This is the best I have got!

K
--
Founder
Linder Educational Coaching 
Winner Best of Arlington 2018
Winner Best of Northern Virginia 2017
Winner Best of Arlington 2016

Patrick McKnight

unread,
Nov 11, 2018, 5:11:37 PM11/11/18
to Kristin Carpenter, Andrew Stets, Ian McCandliss, psy...@googlegroups.com
Hey all,

I am en route back to VA.  Enjoying a pint in DFW airport.  Just catching up on emails.  I struggled with getting python setup for Rstudio and here is what I learned.  All my problems were from the path - at least on my mac.  In Linux, python is an integral part of the operating system.  So, what is my recommendation?  I suggest you use anaconda.  The approach implemented with anaconda is to create a closed environment whereby you execute a command to make python available to Rstudio.  Once Rstudio (and reticulate) learn that command, the path problems go away.  

Here are a few good links to get you all sorted out:



Kristin Carpenter

unread,
Nov 11, 2018, 6:00:54 PM11/11/18
to Patrick McKnight, Andrew Stets, Ian McCandliss, psy...@googlegroups.com
Hi everyone,

Yah, I had already done all the links Pat sent. I just redid them start to finish, and I 100% know that the working directories are in the same environment for R and Python. However, I still get a FALSE when I run py_available() after installing reticulate . 

I am out of links and options. The only other thing that might be a factor is that at various points in all these installs I get an error on my Javascript being outdated. I have downloaded the newest JavaScript, but apparently Chrome doesn't support the newest one. This shouldn't be a factor in running things in R Studio, but it is literally the only thing I can think of that could explain why python isn't successfully being called in R when I have confirmed every path, and you can see all the things in the same folder...which is the path.....sigh.

I can run code in Python alone, and run code in R alone, but no luck syncing them yet. In JupyterLab I can create a notebook that holds sections of R and sections of Python, among other things, in the same doc, but I don't know that they "speak" to each other.

K

Xue Lei

unread,
Nov 11, 2018, 6:49:17 PM11/11/18
to PSYC892
Similar to others, I've also had troubles with making the pystan package work and especially under reticulate. I have windows 10 and installed Anaconda first as well as the other packages required for pystan to work. Because I have windows, following the instructions on the pystan website, I had to install an additional C++ compiler to make it really work in Python. And I could run the stan code in Python alone. However, then, when I tried to use the reticulate package in R to call Python, it seems that it can recognize the python version under Anaconda, but it doesn't recognize all the Python packages I've installed there and I had to install those again under the reticulate environment. (BTW, I found the py_discover_config() command really useful to check which version of Python will be used in reticulate. Kristin, maybe this can help you.) 
But because I don't really know how to make the C++ compiler work under reticulate, I still can't use the pystan function in reticulate. Not sure whether I'm doing it correctly in this trial and error process. I think more explanations on the coda environment will be really helpful!

Xue

Patrick McKnight

unread,
Nov 12, 2018, 2:46:01 PM11/12/18
to PSYC892
Hey all,

I just ran down some more tips and tricks to get Python running.  Should we go through all the hassle?  Yes, I think we ought to for several reasons.  First, Python is a much faster programming language for Bayesian analyses.  There are more resources to run these models compared to R (or any other language).  Second, while Python may be difficult to integrate (as of today) with RStudio, it is the future for data analysis and it would be a shame to miss the opportunity to learn how to use it.  Third, and this might be the kicker....Python is simpler.  You can run the same models in Python that you run in R with far fewer lines of code (and it is faster).  So, from a data analysis and "data scientist" perspective, it makes all the sense in the world for us to push through the pain and learn how to run Python.  With that being said, I realize that the reticulate package is in its infancy.  There are some glitches that will be worked out over the next few months.  Bear with me.  Learn to execute Python from within R and vice versa.

The reticulate online help has actually improved over the past few weeks.  Here (below) is one thing to help you get Python located with R.  Be sure to go through the steps and don't just copy and paste the code into your interpreter.  Read what they say.  Most of the problems you are likely facing stem from the location of Python (i.e., the actual binary interpreter command).  So peruse this link material:


If you don't find that helpful, start diving into the other articles listed on the website.  Many of these articles are focused on certain operating systems.  I use Linux.  Haven't had a problem on my desktop at all running Python with RStudio but, then again, Linux relies heavily on Python.  

Kristin Carpenter

unread,
Nov 12, 2018, 5:55:37 PM11/12/18
to Patrick McKnight, psy...@googlegroups.com
Hi,

OK so perhaps I am hanging too much on the "py_available()" = FALSE. Because if I just install reticulate, and make sure it is set to my wd, then I can run this from the example:

os <- import("os")
os$listdir(".")

This was failing earlier when the wd() path was not correct. SO, if I can run the above, and that os shows up in my environment, am I now successfully pulling Python into R? If not, can you send a sample code that should work for us to test? Maybe the issue in the end was the stupid py_available() call not assessing correctly.

K

--
Check out the Course Website: https://sites.google.com/site/psyc757/
---
You received this message because you are subscribed to the Google Groups "PSYC892" group.
To unsubscribe from this group and stop receiving emails from it, send an email to psyc892+u...@googlegroups.com.
To post to this group, send email to psy...@googlegroups.com.
Visit this group at https://groups.google.com/group/psyc892.
For more options, visit https://groups.google.com/d/optout.

Patrick McKnight

unread,
Nov 12, 2018, 9:20:24 PM11/12/18
to Kristin Carpenter, psy...@googlegroups.com
Ah, you are probably not using projects!!!!!  I think I know the problem.  Permit me to walk you all through the problem tomorrow.
Reply all
Reply to author
Forward
0 new messages