VS Code IntelliSense inside R magic - python

Suppose that one is using an ipyn notebook, with R magic cells. Can IntelliSense work for the R code inside the R magic cells?
I have extensively searched the web but found nothing so far.

You can install NB package for jupyter-notebook: Is there any auto-completion for R in jupyterlab?
pip install jupyter_contrib_nbextensions

Related

How to use R and python in a Kaggle Notebook?

I would like to use both R and Python languages inside a Kaggle Kernel. Thus, when running
!pip install rpy2
inside a Kaggle Notebook I got the following error
Error: rpy2 in API mode cannot be built without R in the PATH or R_HOME defined. Correct this or force ABI mode-only by defining the environment variable RPY2_CFFI_MODE=ABI
I've found out a solution for users of Python within R, but a solution for calling R within Python in a Kaggle Kernel has not yet been provided.
One can notice that a Kaggle Kernel is using behind an anaconda environment. For example,
/opt/conda/bin/python3.7
Also, it is necessary to have R installed on this conda environment. Thus, we can use the subprocess library to run the following script for installing R
import subprocess
subprocess.run('conda install -c conda-forge r-base', shell=True)
and the corresponding rpy2 package
!pip install rpy2
I have provided a notebook on Kaggle with a complete explanation. I'll appreciate your comments.

R kernel crashes while loading R package using rpy2

First of all, I’m new to rpy2 / jupyter so please don’t judge me if this isn’t the correct place to ask my question.
I am trying to set up an integrated workflow for data analysis using R and Python and I encounter the following error:
I am on Ubuntu 19.04. running a conda environment using Jupyter 1.0.0, Python 3.7.4, R 3.5.1, r-irkernel 1.0.2 and rpy2 3.1.0 and I installed the R-package Seurat through R.
When I create a Jupyter notebook using the R-kernel, I can load Seurat with library(Seurat) just fine.
I can also use R code in python using rpy2 and the rmagic such as:
%load_ext rpy2.ipython
%%R
data(allen, package = 'scRNAseq')
adata_allen <- as(allen, 'SingleCellExperiment')
However when I try to load Seurat using rpy2 the kernel crashes:
%%R
library(Seurat)
And I get the following message:
Kernel Restarting
The kernel appears to have died. It will restart automatically
Jupyter gives the following message in the command line:
[I 16:39:01.388 NotebookApp] KernelRestarter: restarting kernel (1/5), keep random ports
kernel 23284ec0-63d5-4b61-9ffa-b52d19851eab restarted
Note that other libraries such as library(dplyr) load just fine using rpy2.
My complete conda environment can be found in the attached text file.
I just can’t seem to figure out what is causing the problem. Is there a way to get a more verbose error message from Jupyter?
Your help would be greatly appreciated!
Regards Felix
The R package Seurat is using an other R package called reticulate, providing a bridge to Python from R.
Unfortunately, whenever rpy2 and reticulate are involved R ends up being initialized twice, which results inevitably in a segfault. This is still an open bug at the time of writing. The issue tracking on the rpy2 side (a link to the reticulate side of the tracking can be found there) is here:
https://bitbucket.org/rpy2/rpy2/issues/456/reticulate-rpy2-sharing-r-process
I've got the same problem with you. But I downgrade to Seurat 3.0.2, your problem will be fixed. To use the user defined R kernel for rpy2 with conda, run the code before at the very beginning (before imoort rpy2)
# user defined R installation
import os
os.environ['R_HOME'] = '/path/to/miniconda/envs/seurat/lib/R' #path to your R installation
os.environ['R_USER'] = '/path/to/miniconda/lib/python3.7/site-packages/rpy2' #path depends on where you installed Python.
This worked for me, while facing issue of kernel getting dead during importing robjects from rpy2:
import os
os.environ['R_HOME'] = '/Users/<your user>/anaconda3/envs/<env name>/lib/R'
# import your desired module
from rpy2.robjects.packages import importr
I had the same problem and I am also using R and python with a Jupyter notebook in docker.
I solved the Kernel crash issue by starting my notebook or Python code with this:
import os \
os.environ['R_HOME'] = '/usr/lib/R'
/usr/lib/R is where I have my system's R installation and libraries, and should be an R version needed by rpy2. Hope this helps.
I tried to install rpy2 in the jupyter/r-notebook:hub-2.3.1 Docker image which comes with Python 3.10.5, IPython 8.4.0, R 4.1.3.
If I install rpy2 in a Terminal window with pip:
python3 -m pip install rpy2
and I start IPython in the Terminal, and type import rpy2,this first step works. But the next step, namely: import rpy2.robjects as robjects results in the following not-so-instructive error message:
Error in glue(.Internal(R.home()), "library", "base", "R", "base", sep = .Platform$file.sep) :
4 arguments passed to .Internal(paste) which requires 3
Error: could not find function "attach"
Error: object '.ArgsEnv' not found
Fatal error: unable to initialize the JIT
The reason is some subtle incompatibility between the rpy2 package on PyPI and the Python and R installations in the jupyter/r-notebook image. The incompatibility occurs because Python and R were installed using Conda in the r-notebook image.
If I install rpy2 also with Conda, like this:
conda install --yes rpy2
then everything works as advertised.
Lessons learned
If Python and R were installed with OS package installers, then you can probably install rpy2 with pip.
If Python and R were installed with Conda, then install rpy2 also with Conda.
(the most embarrassing bit): There is a jupyter/datascience-notebook which comes with rpy2 preinstalled (plus a lot of other goodies), no need to install anything:
jupyter/datascience-notebook includes libraries for data analysis from
the Julia, Python, and R communities.
Everything in the jupyter/scipy-notebook and jupyter/r-notebook
images, and their ancestor images rpy2 package The Julia compiler and
base environment IJulia to support Julia code in Jupyter notebooks
HDF5, Gadfly, RDatasets packages

Solve atom error message

I received an error on atom, it asked to install ipkernal using pip.
Not sure what to do. I have Anaconda on my system and not pip. Can someone explains whats the error about and how can I solve it in using anaconda.
I was running a python code and saved the file as .py.
import pandas as pd
wd = pd.read_csv("winequality-red", sep = ";")
five = wd.head()
print ("five")
Error message:
No kernel for grammar Python found <br>
Check that the language for this file is set in Atom and that you have a Jupyter kernel installed for it.<br>
To detect your current Python install you will need to run:<br>
python -m pip install ipykernel<br>
python -m ipykernel install --user
This isn't really an answer, but you might have better luck on the dedicated Atom forums.
In your case though, it looks like you haven't installed the proper kernels Hydrogen needs to run Python with. (Of course, I'm just assuming you're using Hydrogen. You haven't actually provided any details about how you are trying to run it).
From the Hydrogen documentation, it takes you to this page for Python kernels.
https://nteract.io/kernels/python
In particular, I think you want to run the command conda install ipykernel

Installing vpython

I have installed vpython on the anaconda IDE. However when I import it and use a simple command
import vpython as v
v.sphere()
Nothing appears. How do I rectify this?
Are you on RHEL or Ubuntu? You might want to try reinstalling and making sure that the installation is correct. You can install from http://vpython.org/contents/download_linux.html
The person was using the newer vpython module, not the old visual module. See the first page of vpython.org. Perhaps you were not running in a Jupyter notebook? At the time the question was posted, the vpython module only worked when run in a Jupyter notebook, though now it can be used outside a Jupyter notebook.

IPython notebook: auto-completion doesn't work after dot operators

I'm novice at IPython and I've got a problem with autocompletion.
Tab autocompletion doesn't work properly.
It works after imports:
import<tab>
But tab completion doesn't work after dot operators. As example:
import numpy as np
np.<tab>
Meanwhile this example works well for IPython at console.
Package pyreadline is up-to-date. I tried different browsers. I reinstalled IPython and pyreadlines. But it didn't helped.
If you write things in the same cell, it's perfectly normal. Until you've run the import line, np is not defined.
Run the cell once, or run the import statement in a previous cell and autocomplete should work.
Found the solution in an answer I found on the web:
"I've just installed the latest JEDI then helped me to solve that issue, here's the command when using Anaconda":
conda install - c anaconda jedi
"Or you might need to try this, copy this line on the top of your jupyter notebook":
% config Completer.use_jedi = False
"In my case I installed miniconda on Debian Linux and most likely had a problem with the previous Python that was included with Debian. The following lines solved my problem":
conda update conda
conda update--all

Categories