I've finally got RPy2 working on my Windows 7 computer with Python 2.7.8 and R 3.10.1. I want to call the R function 'DEoptim' which did not come with my R installation however the package has been downloaded via the R Repository and is working in R.
When I do this:
import rpy2.robjects as robjects
dea = robjects.r['DEoptim']
I get the following error:
LookupError Traceback (most recent call last)
<ipython-input-3-a882c24e8623> in <module>()
----> 1 dea = robjects.r['DEoptim']
C:\Users\Patrick\Anaconda\lib\site-packages\rpy2\robjects\__init__.pyc in __getitem__(self, item)
224
225 def __getitem__(self, item):
--> 226 res = _globalenv.get(item)
227 res = conversion.ri2ro(res)
228 res.__rname__ = item
LookupError: 'DEoptim' not found
Which seems to make sense. Python is trying to find this package however it is not there. When importing this package in R I can see this that it is located at: C:/Users/Patrick/Documents/R/win-library/3.1.
Is there any way that I can call this function from python? I've looked around for a good DE optimization package in python and found insyred however using the DEoptim package from R is much easier. Also, there are a lot of other R packages not in the standard library that would be great to have around once in a while
You will likely need to load the relevant R package. The rpy2 function importr provide a simple interface to R packages.
This is the code that solved the problem for me in case anyone gets stuck or doesn't take enough time to read the documentation (like me..)
from rpy2.robjects.packages import importr
dea = importr('DEoptim', lib_loc="C:/Users/Patrick/Documents/R/win-library/3.1")
Related
I have a simple script that creates a ggplot2 scatter plot using rpy2. However, the figure is not being displayed. The returned plot object is NoneType. Here is the script:
from rpy2 import robjects
import rpy2.robjects as ro
from rpy2.robjects.lib import grid
from rpy2.robjects.packages import importr, data
import rpy2.robjects.lib.ggplot2 as ggplot2
rprint = robjects.globalenv.find("print")
stats = importr('stats')
grdevices = importr('grDevices')
base = importr('base')
datasets = importr('datasets')
grid.activate()
mtcars = data(datasets).fetch('mtcars')['mtcars']
gp = ggplot2.ggplot(mtcars)
pp = gp + ggplot2.aes_string(x='wt', y='mpg') + \
ggplot2.geom_point()
print('type(pp):',type(pp))
fig = pp.plot()
print('type(fig):',type(fig))
pp.plot()
I am able to save the figure to a png file, so most of the code is working. While troubleshooting this, I've tried using several versions of rpy2 with various results:
Versions 3.0.1 and 3.0.2 -- Figure flashes, program crashes and plot object is NoneType
Version 3.0.3 -- Empty canvas flashes, program crashes and plot object is NoneType
Versions 3.0.4 and 3.0.5 -- Figure flashes, program crashes and plot object is NoneType
Versions 3.1.0 and 3.2.0 -- The figure is not displayed and plot object is NoneType
Versions 3.2.1 to 3.2.4 -- rpy2 fails to install with pip
I have also read the thread here and followed the links suggested there but I have not been able to adapt the suggested actions to my problem.
For completeness, here are details of the environment I am using: Python 3.7.5, R version 3.6.0, MacOS Catalina Version 10.15.2.
More details to describe "fails to install" would help to guess what is happening with Catalina. On Github Actions, the latest OSX is High Sierra (10.13) and both installs and tests work there: https://github.com/rpy2/rpy2/runs/378570566
Otherwise, your code does not seem to make use of grdevices (or any of the other R pacakges imported through importr()). R may use "static" graphical devices (e.g., PNG file) or "interactive" graphical devices (e.g., X11, Quartz on OSX).
See https://rpy2.github.io/doc/v3.2.x/html/graphics.html#graphical-devices for more info.
Interactive devices may require to process the event loop to update (see
https://rpy2.github.io/doc/v3.2.x/html/interactive.html#r-event-loop), although a program crash is not expected when not doing so.
You could try identify what is the graphical device opened by default on your system (unless there is already an open device R will open a device of the default type) as a first step toward identifying what the issue is.
i have a piece of code where i'm using JuMPand Mambaand both of them export Model.
When i run the code first i get a warning : both Mamba and JuMP export "Model"; uses of it in module QuantumRelay must be qualified, therefore en Error is raised which is :
ERROR: UndefVarError: Model not defined
i need both of the packages Mamba for the MCMC simulation for simulationg draws from a probability distribution and the other for Linear programming.
you can find the package or the code on this link:
https://github.com/marouanehanhasse/Quantum_Relay
check the QuantumRelay module .
Apologizes in advance, because i couldn't post the code here since i'm still new on this community.
In Julia, the using and import keywords are used to bring bindings from another module into the current scope.
using M brings all exported bindings from M directly into scope. If M defines and exports a function my_function, you can use my_function directly in your code after the using statement.
import M imports only the binding M, so you will use M.my_function.
If you want to avoid name clashes as you have with Mamba and JuMP, import at least one of them, and then specify the qualified name, Mamba.Model, JuMP.Model. Subjectively, this also makes your code clearer to read for someone not familiar with both packages and what they export.
Details and other ways to use using and import can be found in the Julia documentation.
My colleague gave me a big R function and I have to use it in the pipeline which will be developed in python3. In theory I have two options:
convert R into python
directly use R function in python script (for example by importing like a module)
What do you suggest? and how can I do each of these options?
Using rpy2.robjects might help ('function.R' is the r code)
import rpy2.robjects as robjects
robjects.r.source("function.R")
result refers to the output of the function
print(robjects.globalenv["result"])
I want to use output from python program and barplot it using R.
I tried using rPy2
import rpy2.robjects as robjects
A= 2
B=4
plot = robjects.IntVector([A,D])
now to call barplot(plot) function. Please guide, even help using any other python package is appreciated
I am trying to read SAS dataset in R 3.0.1.
I have downloaded Hmisc package required to use sas.get function. But I am getting note as below:
Hmisc library by Frank E Harrell Jr
Type library(help='Hmisc'), ?Overview, or ?Hmisc.Overview')
to see overall documentation.
NOTE:Hmisc no longer redefines [.factor to drop unused levels when
subsetting. To get the old behavior of Hmisc type dropUnusedLevels().
Attaching package: ‘Hmisc’
Then I am using the following command:
sas.get(library = "C:\\SAS_dataset", member = "test", formats = FALSE, sasprog = sasprog)
Then the R goes in infinite loop and does not give output. Finally when i press "Esc", it terminates by giving an warning message saying
Warning message:
running command '"C:/program files/SAS/SAS 9.1/sas.exe" "C:\Users\TEJASW~1.ABH\AppData\Local\Temp\RtmpML87zC\SaS13c41642d38.3.sas" -log "_temp_.log"' had status 10708
I tried to find the reason for the same, but all in vain.
I don't understand the reason for this. Is it due to some note given by Hmisc package or something else?
Also I noted that I am facing this problem for latest version i.e. 3.0.1 only. Whereas I was able to read the SAS dataset with the same commands in version 2.15.1.
Can any one help me to solve this problem.
Thanks in advance.
Regards,
Tejasweeni
If you have SAS, you can always export your data to a CSV file and read in R using read.table() or read.csv(). I think this is often the best solution.