Using python Output in R for bar plots - r

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

Related

How do I change this line of code from R to Python

This is the line of code I need to change:
which(!is.na(match(tbl$col1,arr)))
I am a newbie to python, would really appreciate the help
There is a simpler way to achieve the same objective in R:
which(tbl$col1 %in% arr)
which can be translated to Python (I'm considering that tbl is a Pandas dataframe here):
import numpy as np
np.where(tbl[col1].isin(arr))[0]
You should remember that Python is 0-indexed, though.

from import from python in reticulate

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 ...

How can I use/call an R function in a python script?

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"])

RPy2: LookupError when calling R function 'DEoptim' in python

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")

Plot paraview data format file in mayavi

I am solving PDE numerically using FEniCS with its python interface. I am using the ipython notebook to code in python and I want to plot the result using mayavi. I don't want to use paraview to plot it.
The code which I used to save the result in the file is
file=File('grad_shafranov.pvd')
file<<w

Resources