LoginSignup
1
2

More than 1 year has passed since last update.

【保存版】colaboratoryでRによるデータ分析をするための環境構築まとめ

Last updated at Posted at 2022-11-28

colaborratoryはかなり便利だけど、Rで使う場合は結構不便だったりする。なのでこの記事では、colabでRを使うためのハックをまとめてみた。

Rランタイムのcolaboratoryにアクセスする

下記のリンクをクリックすれば、Rランタイムのcolabが使える。

パッケージのインストールを爆速化する

Rランタイムのcolabだと、パッケージのインストールが5分くらいかかることもざらだけど、下記のコードで爆速化することができる。

x<-installed.packages()
if(any(x[,1]== 'ライブラリ名') == FALSE){
    system("add-apt-repository -y ppa:marutter/rrutter4.0")
    system("add-apt-repository -y ppa:c2d4u.team/c2d4u4.0+") 
    system("add-apt-repository ppa:cran/v8") 
    system("apt-get update") 
    system("apt-get install libnode-dev") 
    system("apt-get install r-cran-ライブラリ名")
}

参考文献

google driveにアクセスする

if (file.exists("/usr/local/lib/python3.6/dist-packages/google/colab/_ipython.py")) {
  install.packages("R.utils")
  library("R.utils")
  library("httr")
  my_check <- function() {return(TRUE)}
  reassignInPackage("is_interactive", pkgName = "httr", my_check) 
  options(rlang_interactive=TRUE)
}
R
drive_auth(
  email = gargle::gargle_oauth_email(),
  path = NULL,
  scopes = "https://www.googleapis.com/auth/drive",
  cache = gargle::gargle_oauth_cache(),
  use_oob = gargle::gargle_oob_default(),
  token = NULL
)

参考文献

BigQueryにアクセスする

データ分析ではおなじみのBigQuery。Rではbigrqueryが用意されているがcolabで使う場合は一工夫いる。

R
if (file.exists("/usr/local/lib/python3.7/dist-packages/google/colab/_ipython.py")) {
  install.packages("R.utils")
  library("R.utils")
  library("httr")
  my_check <- function() {return(TRUE)}
  reassignInPackage("is_interactive", pkgName = "httr", my_check) 
  options(rlang_interactive=TRUE)
}
R
bq_auth(use_oob = TRUE, cache = FALSE)

参考文献

日本語フォントを表示させる

日本語そのままだと文字化けしてしまうので下記のコードを打つ。

R
system("apt-get install -y fonts-noto-cjk")

library(systemfonts)
system_fonts()

参考文献

MeCabをダウンロードする

MeCabのダウンロードは下記のようにする。

system('sudo apt-get install -y mecab', intern=TRUE)
system('sudo apt-get install -y libmecab-dev', intern=TRUE)
system('sudo apt-get install -y mecab-ipadic-utf8', intern=TRUE)
R
install.packages("RMeCab", repos = "https://​rmecab.jp/R") 

参考文献

データサイエンス100本ノックをやりたい

下記のリンクからR用のデータサイエンス100本を動かすことができる。

参考文献

1
2
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
1
2