LoginSignup
5
5

More than 3 years have passed since last update.

Jupyter labでRおよびRstudioのパイプ演算子ショートカットキーを使う

Posted at

R notebookで作業する機会が増えてから、R markdownの使い勝手悪いよなあと感じ始め、
notebook的な使い方であればJupyterlabに乗り換えた方が良いのではと思ってトライ。

乗り換えるにあたり、パイプ演算子のショートカットは譲れないポイントだったので設定方法を調べた。

環境

macOS 10.14.6
R 3.5.2
Python 3.7 (Anaconda)

R側の準備

Rを起動してIRkernelを入れる
依存関係にあるパッケージも自動的に入る

install.packages('IRkernel')
IRkernel::installspec()
q()
Jupyter labでRを起動する

new > notebook > kernel選択でRの選択肢が増えていれ成功。
非常に簡単。

Jupyter labの拡張機能

日本語の詳しい解説記事(リンク1リンク2
があるので詳細は割愛する。

variable inspectorを入れるとRstudioの見た目に近くなる。
ただし動作が重くなる原因になるという話も。
contextual_helpがあればvariable inspectorも別にいらないかもしれない。
他はtocやjupyterlab-lspなどお好みで。

パイプ演算子のショートカットを使う

Rstudioのパイプ演算子%>%のショートカットコマンド⌘ + Shift + Mをjupyter labでも使いたい。
色々調べた結果、拡張機能でテキスト挿入をコマンド化した?方のスクリプトを利用させてもらうのが楽だという結論に至る。

jupyter labextension install @techrah/text-shortcuts

その後jupyter labを起動し、メニューから
Settings > Advanced Setting Editor > Keyboard Shortcuts > User Preference
に移動し、下記コマンドをペースト

{
    "shortcuts": [
        {
            "command": "text-shortcuts:insert-text",
            "args": {
                "text": "%>%",
                "autoPad": true
            },
            "keys": [
                "Accel Shift M"
            ],
            "selector": "body"
        },
        {
            "command": "text-shortcuts:insert-text",
            "args": {
                "text": "<-",
                "autoPad": true
            },
            "keys": [
                "Alt -"
            ],
            "selector": "body"
        }
    ]
}

なお、Chromeは⌘ + Shift + Mがデフォルトのショートカットで埋まっていて、しかも変更が出来ないらしい。
衝突するのではと考えて下記のように起動ブラウザの変更をしてみたが、結局Chromeでも使えた

蛇足

jupyter labで使用するブラウザをsafariに変更したい場合は、

jupyter lab --generate-config

で生成されたjupyter_notebook_config.pyの中身を編集。
95行目あたりにの

## Specify what command to use to invoke a web browser when opening the notebook.
#  If not specified, the default browser will be determined by the `webbrowser`
#  standard library module, which allows setting of the BROWSER environment
#  variable to override it.
#  c.NotebookApp.browser = ''

とコメントアウトされている箇所を、

c.NotebookApp.browser = u'safari'

のように編集する。
%sを後ろにつないと上手く行かないという記事もあったが、私の環境では何故かこれでも動作した。

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5