
RStudioでRMeCab
RStudioをインストール
Windowsパソコンを切り替えたので、RStudioとRMeCabをインストール。
RMeCabに思った以上に手こずり、ChatGPTとGoogleを使い何とか動くようにできたのでメモしておきます。
色んな手順を試したので整理しきれず、余計な処理が含まれたり、正しい順番でなかったりするかもしれません。
まず、RStudioのサイトからexeファイルをダウンロードしてインストールします。
これはスムーズに終わりました。
RMeCabをインストール(失敗)
RStudio上でRMeCabをインストール。
install.packages("RMeCab", repos = "http://rmecab.jp/R")
うまく処理できてないようなメッセージが出ましたが、一応library()で確認。
> library(RMeCab)
The current version is designed to work with the 64-bit version of MeCab and a UTF-8 dictionary.
If you are using old MeCab with a Shift-JIS dictionary, please completely uninstall it, and
then reinstall the 64-bit version of MeCab with a UTF-8 dictionary from 'https://github.com/ikegami-yukino/mecab/releases'.
64ビットMeCabをUTF-8辞書でインストール
https://github.com/ikegami-yukino/mecab/releases から64ビットのMeCabをUTF-8辞書でインストールするようにというメッセージが返ってきました。
このサイトを見ると、「公式ではなく野良ビルド」と書いてありましたが、RStudioが推奨しているので、exeファイルをダウンロードしてインストール。
もう一度RMeCabのインストールを実行。
install.packages("RMeCab", repos = "http://rmecab.jp/R")
Rtoolsインストール
今度はRtoolsが必要と言われました。
WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding:
https://cran.rstudio.com/bin/windows/Rtools/
パッケージを ‘C:/Users/xxxxx/AppData/Local/R/win-library/4.4’ 中にインストールします
(‘lib’ が指定されていないため)
URL 'http://rmecab.jp/R/bin/windows/contrib/4.4/RMeCab_1.14.zip' を試しています
Content type 'application/zip' length 706915 bytes (690 KB)
downloaded 690 KB
メッセージに記載してあるURLかををのexeをダウンロードしてインストール。
再度RMeCabをインストール
もう一度RMeCabをインストール。
install.packages("RMeCab", repos = "http://rmecab.jp/R")
正常に完了したようです。
パッケージを ‘C:/Users/xxxxx/AppData/Local/R/win-library/4.4’ 中にインストールします
(‘lib’ が指定されていないため)
URL 'http://rmecab.jp/R/bin/windows/contrib/4.4/RMeCab_1.14.zip' を試しています
Content type 'application/zip' length 706915 bytes (690 KB)
downloaded 690 KB
パッケージ ‘RMeCab’ は無事に展開され、MD5 サムもチェックされました
ダウンロードされたパッケージは、以下にあります
C:\Users\xxxxx\AppData\Local\Temp\RtmpMJt2Oy\downloaded_packages
(実際には他の手順も試しており、上記を実行する前後に
# アンインストール
remove.packages("RMeCab")
や、
# ソースからインストール
install.packages("RMeCab", repos = "http://rmecab.jp/R", type = "source")
も試しています)
Windowsの環境変数を設定
しかし、まだ足りません。
> library(RMeCab)
The current version is designed to work with the 64-bit version of MeCab and a UTF-8 dictionary.
If you are using old MeCab with a Shift-JIS dictionary, please completely uninstall it, and
then reinstall the 64-bit version of MeCab with a UTF-8 dictionary from 'https://github.com/ikegami-yukino/mecab/releases'.
またこのメッセージが出ます。
以下の手順を追加します。
環境変数の設定
Windowsのスタートメニューから「システム環境変数」を検索して開きます。
「環境変数」ボタンをクリックし、システム環境変数のリストから「Path」を選択して「編集」をクリックします。
「新規」をクリックして、MeCabのインストールパスを追加します(例: C:\Program Files\MeCab\bin)。
変更を保存してウィンドウを閉じます



これで設定完了です。
完了
テストもうまく動きました。
> library(RMeCab)
> # RMeCabテスト
> res <- RMeCabC("すもももももももものうち")
> res
[[1]]
名詞
"すもも"
[[2]]
助詞
"も"
[[3]]
名詞
"もも"
[[4]]
助詞
"も"
[[5]]
名詞
"もも"
[[6]]
助詞
"の"
[[7]]
名詞
"うち"