#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    6
    Rep Power
    0

    Rpy2 and user-defined functions


    Hi!
    I'm trying to learn rpy2 to access material in R.

    (I do know R).

    Anyhow, my question is: how do you access user-defined functions from rpy2, please?

    For instance, I have

    import rpy2.robjects as R

    R.r.rnorm(100)

    That works fine.
    But I have a test function called buzz and if I use:
    R.r.buzz(3)

    I get "R has no attribute buzz"

    Thank you in advance for any help.
    Sincerely,
    Erin
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    I've been unable to install rpy2. Have you any suggestions? I use a linux distribution. I don't know R, though it is installed and working.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    6
    Rep Power
    0
    Hi!

    I installed from source. I downloaded the rpy2 tarball and did

    unpacked it. Then I changed into that directory and did "python setup.py install"

    That did it!

    But you do have to have numpy installed too.


    Originally Posted by b49P23TIvg
    I've been unable to install rpy2. Have you any suggestions? I use a linux distribution. I don't know R, though it is installed and working.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    You'll need to show more before I can possibly run a function written in r from python. I think that's your intent. I'm up to here:

    $ python3
    >>> import rpy2.robjects as R
    >>> R.r.rnorm(100)
    <FloatVector - Python:0x7fd67de365a8 / R:0x37110a0>
    [0.280739, -0.477916, -1.211676, ..., -1.495234, -0.667136, -1.980195]
    >>>
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    6
    Rep Power
    0
    I got it to work in a .py file nicely:

    #!/usr/bin/python

    #

    from rpy2.robjects.packages import SignatureTranslatedAnonymousPackage as STAP
    with open("bz2.R","r") as f:
    string=''.join(f.readlines())
    etest = STAP(string,"etest")
    etest.etest(500)

    And here is the bz2.R file:
    etest <- function(n=100) {
    y <- rnorm(n)
    pdf(file="lap1.png")
    plot(y)
    dev.off()
    }

    All is well.

    Originally Posted by b49P23TIvg
    You'll need to show more before I can possibly run a function written in r from python. I think that's your intent. I'm up to here:

    $ python3
    >>> import rpy2.robjects as R
    >>> R.r.rnorm(100)
    <FloatVector - Python:0x7fd67de365a8 / R:0x37110a0>
    [0.280739, -0.477916, -1.211676, ..., -1.495234, -0.667136, -1.980195]
    >>>

IMN logo majestic logo threadwatch logo seochat tools logo