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"])
Related
Any way to do the from - import from Python in Reticulate without having to import the whole module?
example: from tensorflow import keras
I know about the solution below, but it imports the whole module anyway...
keras <- import('tensorflow')$keras
I am not sure, but if you are writing your r code in an rmd file, wouldn't you be able to do it directly in a python chunk like this:
python
from ... import ...
I am trying to use Bloomberg Excel built in functions (BCurve, BView,...) in Python. I am currently using xlwings and i have tried using some of the posts that explain how to run macros from Python using win32com, but I have not seen how to capture the range of cells that are output of the function and avoiding importing the results before the function has completed running.
Running an Excel macro via Python?
I am used to Python, and to a syntax like
import mypackage
import myotherpackage as mop
x = mypackage.calc(y)
z = mop.calc(y)
Am I right in understanding that R, like Matlab, does not have a proper namespace? I am a beginner in R.
How do you address conflict names?
What if two packages have the same function names?
If in R you do
library(mylib)
library(my_second_lib)
and both libraries have the function calc(), does calling calc() mean calling the function of the most recently loaded package?
How do you test two versions of a package? Eg if you want to test the output of your new package vs the output of the old one, and they both have the same function names?
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'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")