Open changcw83 opened 7 years ago
A workaround:
from rpy2.robjects import r
from IPython import get_ipython
def rpy2_competer(ipython, event):
query = event.line.strip().split()[-1]
suggestions = []
all_r_symbols = r('sapply(search(), ls)')
for environment, symbols in all_r_symbols.items():
for _, symbol in symbols.items():
if symbol.startswith(query):
suggestions.append(symbol)
return suggestions
ipython = get_ipython()
ipython.set_hook('complete_command', rpy2_competer, re_key='.*')
For me that's a great help when using R ggplot in Python projects. Unfortunately, it is not R-cells specific, but does the job for me. Tested in JupyterLab only, though I am not aware why it would not work in Notebook. If anyone would be interested in building upon my snippet, I consider it CC0.
For cell specific completion, custom completer API would need to be extended to provide full text of the cell (in the event
object), but I guess this discussion belongs more to IPython. I've also looked into passing type='R'
information (this may be actually better, as we sometimes refer to the R-variables from python cells using the rpy2 objects - so it would allow user to decide if the completion is relevant there) but it seems that Completer
class is experimental and reserved for Jedi completion.
CC @Carreau - I've seen that you've recently updated the IPython's completer.py file, just wanted to let you know that there is a use-case for passing more information for custom completers. Not sure if I should discuss this there or open an issue in IPython repository.
@krassowski, thanks for the code. It works when running this on a cell in Jupyter Notebook. Like you, I also use R ggplot in python projects, but the completion is only for the main functions. For instance, in the following cell, I show where completion is and is not allowed:
%%R library [COMPLETION] (ggplot2) ggplot [COMPLETION] (data [NO COMPLETION] = df1 [NO COMPLETION] , mapping [NO COMPLETION] = aes [NO COMPLETION] (...) + geom_point [COMPLETION] ()
Basically, partial completion is allowed, and since this issue has more than one year, I am interested in any solution that has been raised over time.
Thanks.
I think that the GitHub editor might have changed how the code that you intended to show is displayed; I think that if you use the code formatting option it might become clearer.
Anyways: yes, there was a development in this space indeed: jupyterlab-lsp allows to autocomplete, show diagnostics and many more in the %%R
and %R
cells (and other %% cells too!). Now, it was initially developed for JupyterLab, but with the advent of jupyterlab-classic
that closely resembles the Jupyter Notebook interface it might be good enough for you. Here is a demo:
Some notes on the demo:
continiousHinting
enabled which is disabled by default (so you can decide if you want the hints to show up after every keystroke or just after Tab and other trigger characters such as dot; the trigger character depend on the language)@krassowski Thanks a lot! It works really well with jupyter Lab. In addition to install the extension, I also uninstalled jedi since it causes a problem with autocompletion in Python Language (this is an issue I read on Github a couple of weeks ago). There was no change when working with jupyter notebook and the same virtual environment, but with Lab it is working.
Great to hear it works well for you! If you have any improvement ideas please let us know on in the extension repository.
uninstalled jedi since it causes a problem with autocompletion in Python Language
If you pin jedi to 0.17.2 it should work fine. Just do not install jedi 0.18 just yet.
There was no change when working with jupyter notebook
The LSP extension does not suport the old Jupyter Notebook, but you can get a similar experience when using the proof-of-concept jupyterlab-classic
that closely resembles the Jupyter Notebook interface; jupyterlab-classic is supported by the LSP extension.
@krassowski I have been working with the extension and the packages I needed for a while. However, I was not able to obtain the full benefits of autocompletion (probably because I uninstalled jedi completely). To improve that, I tried jedi=0.17.1 and 0.17.2, but the environments turn inconsistent.
After that experience, I think that lots of warnings and not fully autocomplete is an uncomfortable way to work, but since you have an environment with rpy2, pandas, jupyterlab-lsp, and jedi working together providing you a complete functionality, would you be nice enough to share the list of packages in a requirements.txt format (via pip freeze or conda create ) to be able to set up an environment from it which also works as it is supposed to do?
I think that it would be really helpful. Otherwise, is there any platform where environments are shared in a similar way by the users?
A clean installation using the installation instructions from the lsp extension readme should provide you with a working environment. I do not have one magic requirements.txt as we do tests it across several Python versions and operating systems where exact packages and versions vary; we are confident that it works across that many configurations.
If it does not work for you in a clean environment please do open a bug report on the jupyterlab-lsp extension repository providing your environment details as detailed in the template.
I frequently need to switch back and forth between python and R and would like to have autocomplete feature for the R code i write using %%R cell magic. Is there a workaround that I can accomplish this?