2017年2月26日 星期日

在樹莓派上安裝 R 語言

自從去年對 R 語言產生興趣後, 就一直想找時間好好來學習測試. 但想學的東西實在太多了, 時間總是不夠用. 最近聽了兩場機器學習的演講後, 對 R 語言又熱衷起來. 在資料探勘與機器學習這兩個領域上, Python 與 R 語言的使用度非常高, 我非常看好這兩個語言的發展. 參考資料探勘專業網站 KDnuggets 2014~2015 調查結果, R 與 Python 獨占鰲頭, 分別為 51% 與 29%, 合計占 80%, 且持續上升中, 其他語言則逐漸萎縮 :


今天在 "R語言 : 數學計算、統計模型與金融實務分析應用" 這本書上看到 R 語言也可以安裝在 Linux 機器上, 我想應該可以在樹莓派上跑吧! 下午回到鄉下後, 便將 B+ 那塊板子的 SD 卡先做備份, 然後用下列指令進行安裝 :

首先更新軟體套件清單 (這步驟最好先做, 否則可能在安裝過程發現找不到某些相依套件) :

$ sudo apt-get update
然後安裝 R 軟體 :

$ sudo apt-get install r-base

pi@raspberrypi:~ $ sudo apt-get install r-base
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libasn1-8-heimdal libgssapi3-heimdal libhcrypto4-heimdal libheimbase1-heimdal libheimntlm0-heimdal libhx509-5-heimdal
  libkrb5-26-heimdal libroken18-heimdal libwind0-heimdal libxfce4ui-1-0 xfce-keyboard-shortcuts
Use 'apt-get autoremove' to remove them.
The following extra packages will be installed:
  autotools-dev bzip2-doc cdbs gfortran gfortran-4.9 libblas-dev libbz2-dev libgfortran-4.9-dev libjpeg-dev libjpeg62-turbo-dev
  liblapack-dev liblzma-dev libncurses5-dev libpaper-utils libpaper1 libpcre3-dev libpcrecpp0 libreadline-dev libreadline6-dev
  libtinfo-dev r-base-core r-base-dev r-base-html r-cran-boot r-cran-class r-cran-cluster r-cran-codetools r-cran-foreign
  r-cran-kernsmooth r-cran-lattice r-cran-mass r-cran-matrix r-cran-mgcv r-cran-nlme r-cran-nnet r-cran-rpart r-cran-spatial
  r-cran-survival r-doc-html r-recommended zip
Suggested packages:
  devscripts gfortran-doc gfortran-4.9-doc libgfortran3-dbg liblzma-doc ncurses-doc readline-doc ess r-doc-info r-doc-pdf r-mathlib
  debhelper texlive-base texlive-latex-base texlive-generic-recommended texlive-fonts-recommended texlive-fonts-extra
  texlive-extra-utils texlive-latex-recommended texlive-latex-extra texinfo
The following NEW packages will be installed:
  autotools-dev bzip2-doc cdbs gfortran gfortran-4.9 libblas-dev libbz2-dev libgfortran-4.9-dev libjpeg-dev libjpeg62-turbo-dev
  liblapack-dev liblzma-dev libncurses5-dev libpaper-utils libpaper1 libpcre3-dev libpcrecpp0 libreadline-dev libreadline6-dev
  libtinfo-dev r-base r-base-core r-base-dev r-base-html r-cran-boot r-cran-class r-cran-cluster r-cran-codetools r-cran-foreign
  r-cran-kernsmooth r-cran-lattice r-cran-mass r-cran-matrix r-cran-mgcv r-cran-nlme r-cran-nnet r-cran-rpart r-cran-spatial
  r-cran-survival r-doc-html r-recommended zip
0 upgraded, 42 newly installed, 0 to remove and 17 not upgraded.
Need to get 44.0 MB of archives.
After this operation, 84.1 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://mirrordirector.raspbian.org/raspbian/ jessie/main libpaper1 armhf 1.1.24+nmu4 [21.4 kB]
Get:2 http://mirrordirector.raspbian.org/raspbian/ jessie/main libpcrecpp0 armhf 2:8.35-3.3+deb8u4 [140 kB]
Get:3 http://mirrordirector.raspbian.org/raspbian/ jessie/main autotools-dev all 20140911.1 [70.5 kB]
Get:4 http://mirrordirector.raspbian.org/raspbian/ jessie/main bzip2-doc all 1.0.6-7 [307 kB]
Get:5 http://mirrordirector.raspbian.org/raspbian/ jessie/main gfortran armhf 4:4.9.2-2 [1,294 B]
Get:6 http://mirrordirector.raspbian.org/raspbian/ jessie/main cdbs all 0.4.130+deb8u1 [77.1 kB]
Get:7 http://mirrordirector.raspbian.org/raspbian/ jessie/main libgfortran-4.9-dev armhf 4.9.2-10 [179 kB]
Get:8 http://mirrordirector.raspbian.org/raspbian/ jessie/main gfortran-4.9 armhf 4.9.2-10 [4,525 kB]
Get:9 http://mirrordirector.raspbian.org/raspbian/ jessie/main libreadline-dev armhf 6.3-8 [994 B]
Get:10 http://mirrordirector.raspbian.org/raspbian/ jessie/main libblas-dev armhf 1.2.20110419-10 [117 kB]
Get:11 http://mirrordirector.raspbian.org/raspbian/ jessie/main libbz2-dev armhf 1.0.6-7 [27.5 kB]
Get:12 http://mirrordirector.raspbian.org/raspbian/ jessie/main libjpeg62-turbo-dev armhf 1:1.3.1-12 [400 kB]
Get:13 http://mirrordirector.raspbian.org/raspbian/ jessie/main libjpeg-dev all 1:1.3.1-12 [49.3 kB]
Get:14 http://mirrordirector.raspbian.org/raspbian/ jessie/main liblapack-dev armhf 3.5.0-4 [1,364 kB]
Get:15 http://mirrordirector.raspbian.org/raspbian/ jessie/main liblzma-dev armhf 5.1.1alpha+20120614-2 [272 kB]
Get:16 http://mirrordirector.raspbian.org/raspbian/ jessie/main libtinfo-dev armhf 5.9+20140913-1 [65.4 kB]
Get:17 http://mirrordirector.raspbian.org/raspbian/ jessie/main libncurses5-dev armhf 5.9+20140913-1 [154 kB]
Get:18 http://mirrordirector.raspbian.org/raspbian/ jessie/main libpaper-utils armhf 1.1.24+nmu4 [17.2 kB]
Get:19 http://mirrordirector.raspbian.org/raspbian/ jessie/main libpcre3-dev armhf 2:8.35-3.3+deb8u4 [413 kB]
Get:20 http://mirrordirector.raspbian.org/raspbian/ jessie/main libreadline6-dev armhf 6.3-8 [105 kB]
Get:21 http://mirrordirector.raspbian.org/raspbian/ jessie/main zip armhf 3.0-8 [326 kB]
Get:22 http://mirrordirector.raspbian.org/raspbian/ jessie/main r-base-core armhf 3.1.1-1 [19.5 MB]
Get:23 http://mirrordirector.raspbian.org/raspbian/ jessie/main r-cran-boot all 1.3-13-1 [571 kB]
Get:24 http://mirrordirector.raspbian.org/raspbian/ jessie/main r-cran-cluster armhf 1.15.3-1 [469 kB]
Get:25 http://mirrordirector.raspbian.org/raspbian/ jessie/main r-cran-foreign armhf 0.8.61-1 [203 kB]
Get:26 http://mirrordirector.raspbian.org/raspbian/ jessie/main r-cran-mass armhf 7.3-34-1 [1,011 kB]
Get:27 http://mirrordirector.raspbian.org/raspbian/ jessie/main r-cran-kernsmooth armhf 2.23-13-1 [80.0 kB]
Get:28 http://mirrordirector.raspbian.org/raspbian/ jessie/main r-cran-lattice armhf 0.20-29-1 [712 kB]
Get:29 http://mirrordirector.raspbian.org/raspbian/ jessie/main r-cran-nlme armhf 3.1.117-1 [1,986 kB]
Get:30 http://mirrordirector.raspbian.org/raspbian/ jessie/main r-cran-matrix armhf 1.1-4-1 [2,851 kB]
Get:31 http://mirrordirector.raspbian.org/raspbian/ jessie/main r-cran-mgcv armhf 1.8-3-1 [1,701 kB]
Get:32 http://mirrordirector.raspbian.org/raspbian/ jessie/main r-cran-survival armhf 2.37-7-1 [4,428 kB]
Get:33 http://mirrordirector.raspbian.org/raspbian/ jessie/main r-cran-rpart armhf 4.1-8-1 [854 kB]
Get:34 http://mirrordirector.raspbian.org/raspbian/ jessie/main r-cran-class armhf 7.3-11-1 [80.4 kB]
Get:35 http://mirrordirector.raspbian.org/raspbian/ jessie/main r-cran-nnet armhf 7.3-8-1 [93.4 kB]
Get:36 http://mirrordirector.raspbian.org/raspbian/ jessie/main r-cran-spatial armhf 7.3-8-1 [115 kB]
Get:37 http://mirrordirector.raspbian.org/raspbian/ jessie/main r-cran-codetools all 0.2-9-1 [45.7 kB]
Get:38 http://mirrordirector.raspbian.org/raspbian/ jessie/main r-recommended all 3.1.1-1 [2,852 B]
Get:39 http://mirrordirector.raspbian.org/raspbian/ jessie/main r-base all 3.1.1-1 [37.5 kB]
Get:40 http://mirrordirector.raspbian.org/raspbian/ jessie/main r-base-dev all 3.1.1-1 [4,018 B]
Get:41 http://mirrordirector.raspbian.org/raspbian/ jessie/main r-doc-html all 3.1.1-1 [529 kB]
Get:42 http://mirrordirector.raspbian.org/raspbian/ jessie/main r-base-html all 3.1.1-1 [81.1 kB]
Fetched 44.0 MB in 7min 7s (103 kB/s)
Extracting templates from packages: 100%
Preconfiguring packages ...
Selecting previously unselected package libpaper1:armhf.
(Reading database ... 131823 files and directories currently installed.)
Preparing to unpack .../libpaper1_1.1.24+nmu4_armhf.deb ...
Unpacking libpaper1:armhf (1.1.24+nmu4) ...
Selecting previously unselected package libpcrecpp0:armhf.
Preparing to unpack .../libpcrecpp0_2%3a8.35-3.3+deb8u4_armhf.deb ...
Unpacking libpcrecpp0:armhf (2:8.35-3.3+deb8u4) ...
Selecting previously unselected package autotools-dev.
Preparing to unpack .../autotools-dev_20140911.1_all.deb ...
Unpacking autotools-dev (20140911.1) ...
Selecting previously unselected package bzip2-doc.
Preparing to unpack .../bzip2-doc_1.0.6-7_all.deb ...
Unpacking bzip2-doc (1.0.6-7) ...
Selecting previously unselected package cdbs.
Preparing to unpack .../cdbs_0.4.130+deb8u1_all.deb ...
Unpacking cdbs (0.4.130+deb8u1) ...
Selecting previously unselected package libgfortran-4.9-dev:armhf.
Preparing to unpack .../libgfortran-4.9-dev_4.9.2-10_armhf.deb ...
Unpacking libgfortran-4.9-dev:armhf (4.9.2-10) ...
Selecting previously unselected package gfortran-4.9.
Preparing to unpack .../gfortran-4.9_4.9.2-10_armhf.deb ...
Unpacking gfortran-4.9 (4.9.2-10) ...
Selecting previously unselected package gfortran.
Preparing to unpack .../gfortran_4%3a4.9.2-2_armhf.deb ...
Unpacking gfortran (4:4.9.2-2) ...
Selecting previously unselected package libblas-dev.
Preparing to unpack .../libblas-dev_1.2.20110419-10_armhf.deb ...
Unpacking libblas-dev (1.2.20110419-10) ...
Selecting previously unselected package libbz2-dev:armhf.
Preparing to unpack .../libbz2-dev_1.0.6-7_armhf.deb ...
Unpacking libbz2-dev:armhf (1.0.6-7) ...
Selecting previously unselected package libjpeg62-turbo-dev:armhf.
Preparing to unpack .../libjpeg62-turbo-dev_1%3a1.3.1-12_armhf.deb ...
Unpacking libjpeg62-turbo-dev:armhf (1:1.3.1-12) ...
Selecting previously unselected package libjpeg-dev.
Preparing to unpack .../libjpeg-dev_1%3a1.3.1-12_all.deb ...
Unpacking libjpeg-dev (1:1.3.1-12) ...
Selecting previously unselected package liblapack-dev.
Preparing to unpack .../liblapack-dev_3.5.0-4_armhf.deb ...
Unpacking liblapack-dev (3.5.0-4) ...
Selecting previously unselected package liblzma-dev:armhf.
Preparing to unpack .../liblzma-dev_5.1.1alpha+20120614-2_armhf.deb ...
Unpacking liblzma-dev:armhf (5.1.1alpha+20120614-2) ...
Selecting previously unselected package libtinfo-dev:armhf.
Preparing to unpack .../libtinfo-dev_5.9+20140913-1_armhf.deb ...
Unpacking libtinfo-dev:armhf (5.9+20140913-1) ...
Selecting previously unselected package libncurses5-dev:armhf.
Preparing to unpack .../libncurses5-dev_5.9+20140913-1_armhf.deb ...
Unpacking libncurses5-dev:armhf (5.9+20140913-1) ...
Selecting previously unselected package libpaper-utils.
Preparing to unpack .../libpaper-utils_1.1.24+nmu4_armhf.deb ...
Unpacking libpaper-utils (1.1.24+nmu4) ...
Selecting previously unselected package libpcre3-dev:armhf.
Preparing to unpack .../libpcre3-dev_2%3a8.35-3.3+deb8u4_armhf.deb ...
Unpacking libpcre3-dev:armhf (2:8.35-3.3+deb8u4) ...
Selecting previously unselected package libreadline6-dev:armhf.
Preparing to unpack .../libreadline6-dev_6.3-8_armhf.deb ...
Unpacking libreadline6-dev:armhf (6.3-8) ...
Selecting previously unselected package libreadline-dev:armhf.
Preparing to unpack .../libreadline-dev_6.3-8_armhf.deb ...
Unpacking libreadline-dev:armhf (6.3-8) ...
Selecting previously unselected package zip.
Preparing to unpack .../archives/zip_3.0-8_armhf.deb ...
Unpacking zip (3.0-8) ...
Selecting previously unselected package r-base-core.
Preparing to unpack .../r-base-core_3.1.1-1_armhf.deb ...
Unpacking r-base-core (3.1.1-1) ...
Selecting previously unselected package r-cran-boot.
Preparing to unpack .../r-cran-boot_1.3-13-1_all.deb ...
Unpacking r-cran-boot (1.3-13-1) ...
Selecting previously unselected package r-cran-cluster.
Preparing to unpack .../r-cran-cluster_1.15.3-1_armhf.deb ...
Unpacking r-cran-cluster (1.15.3-1) ...
Selecting previously unselected package r-cran-foreign.
Preparing to unpack .../r-cran-foreign_0.8.61-1_armhf.deb ...
Unpacking r-cran-foreign (0.8.61-1) ...
Selecting previously unselected package r-cran-mass.
Preparing to unpack .../r-cran-mass_7.3-34-1_armhf.deb ...
Unpacking r-cran-mass (7.3-34-1) ...
Selecting previously unselected package r-cran-kernsmooth.
Preparing to unpack .../r-cran-kernsmooth_2.23-13-1_armhf.deb ...
Unpacking r-cran-kernsmooth (2.23-13-1) ...
Selecting previously unselected package r-cran-lattice.
Preparing to unpack .../r-cran-lattice_0.20-29-1_armhf.deb ...
Unpacking r-cran-lattice (0.20-29-1) ...
Selecting previously unselected package r-cran-nlme.
Preparing to unpack .../r-cran-nlme_3.1.117-1_armhf.deb ...
Unpacking r-cran-nlme (3.1.117-1) ...
Selecting previously unselected package r-cran-matrix.
Preparing to unpack .../r-cran-matrix_1.1-4-1_armhf.deb ...
Unpacking r-cran-matrix (1.1-4-1) ...
Selecting previously unselected package r-cran-mgcv.
Preparing to unpack .../r-cran-mgcv_1.8-3-1_armhf.deb ...
Unpacking r-cran-mgcv (1.8-3-1) ...
Selecting previously unselected package r-cran-survival.
Preparing to unpack .../r-cran-survival_2.37-7-1_armhf.deb ...
Unpacking r-cran-survival (2.37-7-1) ...
Selecting previously unselected package r-cran-rpart.
Preparing to unpack .../r-cran-rpart_4.1-8-1_armhf.deb ...
Unpacking r-cran-rpart (4.1-8-1) ...
Selecting previously unselected package r-cran-class.
Preparing to unpack .../r-cran-class_7.3-11-1_armhf.deb ...
Unpacking r-cran-class (7.3-11-1) ...
Selecting previously unselected package r-cran-nnet.
Preparing to unpack .../r-cran-nnet_7.3-8-1_armhf.deb ...
Unpacking r-cran-nnet (7.3-8-1) ...
Selecting previously unselected package r-cran-spatial.
Preparing to unpack .../r-cran-spatial_7.3-8-1_armhf.deb ...
Unpacking r-cran-spatial (7.3-8-1) ...
Selecting previously unselected package r-cran-codetools.
Preparing to unpack .../r-cran-codetools_0.2-9-1_all.deb ...
Unpacking r-cran-codetools (0.2-9-1) ...
Selecting previously unselected package r-recommended.
Preparing to unpack .../r-recommended_3.1.1-1_all.deb ...
Unpacking r-recommended (3.1.1-1) ...
Selecting previously unselected package r-base.
Preparing to unpack .../r-base_3.1.1-1_all.deb ...
Unpacking r-base (3.1.1-1) ...
Selecting previously unselected package r-base-dev.
Preparing to unpack .../r-base-dev_3.1.1-1_all.deb ...
Unpacking r-base-dev (3.1.1-1) ...
Selecting previously unselected package r-doc-html.
Preparing to unpack .../r-doc-html_3.1.1-1_all.deb ...
Unpacking r-doc-html (3.1.1-1) ...
Selecting previously unselected package r-base-html.
Preparing to unpack .../r-base-html_3.1.1-1_all.deb ...
Unpacking r-base-html (3.1.1-1) ...
Processing triggers for man-db (2.7.0.2-5) ...
Processing triggers for install-info (5.2.0.dfsg.1-6) ...
Processing triggers for hicolor-icon-theme (0.13-1) ...
Processing triggers for gnome-menus (3.13.3-6) ...
Processing triggers for desktop-file-utils (0.22-1) ...
Processing triggers for mime-support (3.58) ...
Setting up libpaper1:armhf (1.1.24+nmu4) ...

Creating config file /etc/papersize with new version
Setting up libpcrecpp0:armhf (2:8.35-3.3+deb8u4) ...
Setting up autotools-dev (20140911.1) ...
Setting up bzip2-doc (1.0.6-7) ...
Setting up cdbs (0.4.130+deb8u1) ...
Setting up libgfortran-4.9-dev:armhf (4.9.2-10) ...
Setting up gfortran-4.9 (4.9.2-10) ...
Setting up gfortran (4:4.9.2-2) ...
update-alternatives: using /usr/bin/gfortran to provide /usr/bin/f95 (f95) in auto mode
update-alternatives: using /usr/bin/gfortran to provide /usr/bin/f77 (f77) in auto mode
Setting up libblas-dev (1.2.20110419-10) ...
update-alternatives: using /usr/lib/libblas/libblas.so to provide /usr/lib/libblas.so (libblas.so) in auto mode
Setting up libbz2-dev:armhf (1.0.6-7) ...
Setting up libjpeg62-turbo-dev:armhf (1:1.3.1-12) ...
Setting up libjpeg-dev (1:1.3.1-12) ...
Setting up liblapack-dev (3.5.0-4) ...
update-alternatives: using /usr/lib/lapack/liblapack.so to provide /usr/lib/liblapack.so (liblapack.so) in auto mode
Setting up liblzma-dev:armhf (5.1.1alpha+20120614-2) ...
Setting up libtinfo-dev:armhf (5.9+20140913-1) ...
Setting up libncurses5-dev:armhf (5.9+20140913-1) ...
Setting up libpaper-utils (1.1.24+nmu4) ...
Setting up libpcre3-dev:armhf (2:8.35-3.3+deb8u4) ...
Setting up libreadline6-dev:armhf (6.3-8) ...
Setting up libreadline-dev:armhf (6.3-8) ...
Setting up zip (3.0-8) ...
Setting up r-base-core (3.1.1-1) ...

Creating config file /etc/R/Renviron with new version
Setting up r-cran-boot (1.3-13-1) ...
Setting up r-cran-cluster (1.15.3-1) ...
Setting up r-cran-foreign (0.8.61-1) ...
Setting up r-cran-mass (7.3-34-1) ...
Setting up r-cran-kernsmooth (2.23-13-1) ...
Setting up r-cran-lattice (0.20-29-1) ...
Setting up r-cran-nlme (3.1.117-1) ...
Setting up r-cran-matrix (1.1-4-1) ...
Setting up r-cran-mgcv (1.8-3-1) ...
Setting up r-cran-survival (2.37-7-1) ...
Setting up r-cran-rpart (4.1-8-1) ...
Setting up r-cran-class (7.3-11-1) ...
Setting up r-cran-nnet (7.3-8-1) ...
Setting up r-cran-spatial (7.3-8-1) ...
Setting up r-cran-codetools (0.2-9-1) ...
Setting up r-recommended (3.1.1-1) ...
Setting up r-base (3.1.1-1) ...
Setting up r-base-dev (3.1.1-1) ...
Setting up r-doc-html (3.1.1-1) ...
Setting up r-base-html (3.1.1-1) ...
Processing triggers for libc-bin (2.19-18+deb8u7) ...

安裝完畢後用 R (注意是大寫) 即可進入 R 語言命令列, 要跳出  R 回到 Linux 的話則輸入 q() :

pi@raspberrypi:~ $ R

R version 3.1.1 (2014-07-10) -- "Sock it to Me"
Copyright (C) 2014 The R Foundation for Statistical Computing
Platform: arm-unknown-linux-gnueabihf (32-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> 1+2
[1] 3
> 1/2
[1] 0.5
> q()
Save workspace image? [y/n/c]: n
pi@raspberrypi:~ $

可見樹莓派支援的是 R 3.1.1 版, 沒想到 R 語言在樹莓派也跑得挺順的, 如果寫個 R Script 放在 Crontab 裡面執行, 不就可以自動進行統計分析與機器學習了嗎? 樹莓派作為一個低功耗可 24 小時開機的 Linux 伺服器, 有了 R 語言可說如虎添翼啊!

大部分的 R 操作可在命令列執行, 但若執行 plot() 等繪圖函數時就必須在 X Window 桌面系統的 Terminal 視窗下才會顯示 (例如透過 VNC 進行遠端桌面存取), 如果是透過 Putty 連線, 則執行 plot() 之後不會顯示圖形.



參考 :

Using R with the Pi
R on the Raspberry Pi

沒有留言 :