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 ...
Related
Consider you do npm i numjs then you want to do something like
import * as nj from 'numjs'
but it does not quite work like other libraries. Is this the correct import statement or is there some trick?
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 have multiple Notebook cells where im importing the same libraries.
Is there a way to import these libraries once (like in the very 1st cell) and allow all other cells to use them?
Since my notebook has several cells, each importing the same libraries, i'm starting to see alot of redundancies.
Of course! You can add all your libraries at the very top of your notebook. All of them will be available for use.
import pandas as pd
import numpy as np
import matolotlib.pyplot as plt
import seaborn as sns
I use iPython Notebook for scientific applications and I'm always plotting experimental data sets. The default image rendering and color cycling behavior of the combination of iPython Notebook and Matplotlib is pretty bad, but it's great after the following tweaks.
# ipython notebook specific
%pylab inline
# imports
from matplotlib import pyplot as plt
import matplotlib as mpl
from seaborn.apionly import set_palette
from IPython.display import set_matplotlib_formats
# configure plotting
set_matplotlib_formats('pdf', 'svg')
set_palette('Set1', n_colors=15, desat=None)
mpl.rcParams['figure.figsize']=(12.0,2.0)
I don't want to have to enter these manually in every notebook. How can I have these executed for all notebooks I'll ever create?
You can create Python files in $HOME/.ipython/profile_default/startup/ that contains the code you want executed at startup.
For example, you can create $HOME/.ipython/profile_default/startup/00-init.py:
# imports
from matplotlib import pyplot as plt
import matplotlib as mpl
from seaborn.apionly import set_palette
from IPython.display import set_matplotlib_formats
# configure plotting
set_matplotlib_formats('pdf', 'svg')
set_palette('Set1', n_colors=15, desat=None)
mpl.rcParams['figure.figsize']=(12.0,2.0)
Note that IPython magics are not supported here, so %matplotlib inline won't work. This question deals with making %matplotlib inline the default.
You should be aware that if you change the defaults for your IPython environment, your notebooks may not work on other people's IPython installations.
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