• 沒有找到結果。

R 軟體環境介紹

N/A
N/A
Protected

Academic year: 2021

Share "R 軟體環境介紹"

Copied!
54
0
0

加載中.... (立即查看全文)

全文

(1)

http://www.hmwu.idv.tw

吳漢銘 國立政治大學 統計學系

R 軟體環境介紹

A01

(2)

本章大綱&學習目標

 R軟體環境簡介

 R軟體安裝,R程式執行及套件(Package)

 R相關檔案其副檔名的意義

 求助說明 help() 、內訂Demo程式

 R程式IDE編輯器: RStudio

 建立專案、HELP、常用快速鍵

 更新R軟體及R套件

 R的學習資源

2/54

(3)

The R Project for Statistical Computing

http://www.r-project.org

3/54

(4)

R 是什麼?

R is a language and environment for statistical computing and graphics.

1985, University of Auckland, Robert Gentleman and Ross Ihaka.

R is an official part of the Free Software Foundation's GNU project, and the R Foundation has similar goals to other open source software foundations like the Apache Foundation or the GNOME Foundation.

Similar to the S language (AT&T) and environment.

S-plus (http://www.tibco.com)

R provides a wide variety of statistical ( linear and nonlinear modelling, classical statistical tests, time-series analysis, classification, clustering, ... ) and graphical

techniques, and is highly extensible.

4/54

(5)

R 是什麼?

One of R's strengths is the ease with which well-

designed publication-quality plots can be produced, including mathematical symbols and formulae where needed.

For computationally-intensive tasks, C, C++ and Fortran code can be linked and called at run time.

Advanced users can write C code to manipulate R objects directly.

優點:

• 免費統計分析軟體

• 完整的說明文件與討論區

• 高品質的學術繪圖

• 程式易根據需求修改

缺點:

• 不親切的使用者介面。(已有改進方法)

• 需詳知套件、函式名稱與程式編寫邏輯

• 大量計算效能低。(已有改進方法)

• its lack of security over the Web

5/54

(6)

TIOBE 全球程式語言排名

http://www.tiobe.com/tiobe-index/

(共243種程式語言)

6/54

(7)

其它調查

IEEE

http://spectrum.ieee.org/computing/software/the-2016-top- programming-languages

http://www.kdnuggets.com/2013/08/languages-for-analytics-data- mining-data-science.html

7/54

(8)

軟體外觀 (Windows) 8/54

(9)

CRAN: The Comprehensive R Archive Network

repos="http://cran.csie.ntu.edu.tw/"

9/54

(10)

R 軟體下載

http://cran.csie.ntu.edu.tw/bin/windows/base/R-3.3.2-win.exe

10/54

(11)

軟體安裝; 啟動R

注意: R軟體安裝目錄,使用者目錄、R套件安裝目錄的路徑

11/54

(12)

改成英文介面 12/54

(13)

喜好設定 13/54

(14)

R 選單 14/54

(15)

寫程式及執行

(myRcode.R)程式存檔

(1) 執行程式

(3) 利用source 執行程式

> source(“myRcode.R”)

注意工作目 錄及print (2) 執行程式

15/54

(16)

工作區存檔 File => Workspace (.RData)

歷史指令存檔File => History (. Rhistory)

改變工作目錄 File => Change dir…

> getwd

> setwd(“D:\\my-R”)

> dir() #

查看目前目錄下的檔案

Rnw stands for R NoWeb files. The RNW file type is primarily associated with 'Sweave'. Sweave is a tool that allows you to embed the R code for complete data analyses in latex

documents.

name.rdb: a concatenation of serialized objects.

name.rdx: file contains an index.

name.rds the serialized R objects written by saveRDS

name.rda: 二進位R資料檔。(用load(name)載入)

R 相關檔案副檔名 16/54

(17)

安裝R套件 (RGui選單)

Packages => Install package(s) => CRAN mirror => installr, rgl, scatterplot3d (按Ctrl可多重選取) => OK

17/54

(18)

安裝R套件 (指令方式)

Automatically load packages in the Startup of R:

• call

.First()

in the

Rprofile

file.

編輯檔案(Windows) C:\Program Files\R\R- 3.3.1\library\base\Rprofile

.First <- function(){

cat("\n Welcome to R!\n\n") require(rgl)

}

> setRepositories()

> chooseCRANmirror()

> install.packages("rgl") # 安裝套件

> library(rgl) # 載入套件

> # same as > library("rgl")

> detach("package:rgl", unload=TRUE) # 卸載套件

> remove.packages("rgl") # 移除套件

> .libPaths() # 列出library安裝目錄

> # 指定repositories

> install.packages("rgl", repos = "http://cran.csie.ntu.edu.tw")

> options(repos="http://cran.csie.ntu.edu.tw")

> pks <- c("rgl", "ggplot2", "maps", "e1071")

> install.packages(pks)

> lapply(pks, library, character.only = TRUE)

> # 安裝 BioC 的套件

> # setRepositories(graphics = F, ind = 2)

> # options("repos")

> source("https://bioconductor.org/biocLite.R")

> biocLite("EBImage")

18/54

(19)

安裝R套件 (RStudio選單) 19/54

(20)

安裝R套件常見問題

Why mac OS failed to install rgl: rgl package needs X11 to show its 3D plots.

(1) Download/install XQuartz https://www.xquartz.org (2) Check the library /usr/X11/lib/libGLU.1.dylib is loaded (3) install.packages("rgl")

Installing R on OS X

https://www.r-bloggers.com/installing-r-on-os-x/

20/54

(21)

library require 的差別

狀況(1): 載入已安裝的套件,且 using the command outside of a function definition,則:

"

require

" or "

library

" 相同。

狀況(2): 載入未安裝的套件,則:

library(foo)

: 程式停止with the message

"Error in

library(foo): there is no package called 'foo'. "

require(foo)

: get a warning, but not an error. Your program will continue to run, only to crash later when you try to use a function from the library "

foo

"

require(package) returns (invisibly) TRUE if the package is available

> # install "package" if it doesn’t exist, and then load it.

> if (!require(package)) install.packages("package")

> library(package)

21/54

(22)

CRAN Task Views

https://cran.r-project.org/web/views/

> install.packages("ctv")

> library(ctv)

>

> available.views() CRAN Task Views --- Name: Bayesian

Topic: Bayesian Inference Maintainer: Jong Hee Park

Repository: https://cran.fhcrc.org ...

> ## install MachineLearning view

> install.views("MachineLearning")

> ## only with core packages

> install.views("MachineLearning", coreOnly = TRUE)

> ## update MachineLearning view

> update.views("MachineLearning")

What is the difference between require()and library()? Not much difference in everyday work.

requireis used inside functions, as it outputs a warning and continues if the package is not found, whereas library will throw an error.

22/54

(23)

Popular Packages

http://www.kdnuggets.com/2015/06/top-20-r-packages.html

Top 20 R packages by popularity

http://crantastic.org/

http://cran-logs.rstudio.com/

23/54

(24)

https://awesome-r.com 24/54

(25)

R Related Project

http://www.bioconductor.org

Related Projects

https://www.r-project.org/other-projects.html

• Bioconductor: Bioinformatics with R

http://www.bioconductor.org

• Rgeo: Spatial Statistics with R

https://geodacenter.asu.edu/projects/rsp

• Robust Statistics with R

http://www.statistik.tuwien.ac.at/rsr/

• Rmetrics: Financial Market Analysis with R

http://www.rmetrics.org

• Omegahat: Distributed Statistical Computing

http://www.omegahat.org

• Graphical User Interfaces for R

http://www.sciviews.org/_rgui/

• ESS: Emacs speaks Statistics

http://ess.R-project.org/

• R for Mediawiki

http://mars.wiwi.hu-berlin.de/mediawiki/sk/index.php/R_Extension_for_MediaWiki

• PowerShell R Interop Cmdlet

http://powershellrinterop.codeplex.com

• TANGO/ALGENCAN

http://www.ime.usp.br/~egbirgin/tango/

25/54

(26)

說明手冊 (Help Manuals) 26/54

(27)

求助範例

> help.search("t test")

> ?t.test

> help(solve)

> ?solve

> help("[[")

> help.start()

> ?help

?package.name

?package.name::function.name

apropos("norm") #search all function names for the "norm" key

27/54

(28)

求助範例 (conti.)

語法

參數

回傳值

範例 說明

28/54

(29)

Search R

http://www.rdocumentation.org/ https://cran.r-

project.org/web/packages/sos/index.ht ml

sos

: Search Contributed R Packages, Sort by Package

RSeek: http://www.rseek.org/

NOTE:

> ?’if’

> ?Control

> ?’while’

> ?’%*%’

pkgsearch

: Search and Query CRAN R Packages

https://cran.r-project.org/web/packages/pkgsearch/index.html

# ps() is an alias to pkg_search()

> install.packages("pkgsearch")

> library(pkgsearch)

> ps("survival")

> ps("networks")

> ps("visualization")

29/54

(30)

> demo()

> demo(graphics)

demo(package = .packages(all.available = TRUE))

> library(rgl)

> demo(rgl)

30/54

(31)

演示程式碼 (Demo Code) 31/54

(32)

R 程式IDE編輯器: RStudio

IDE: integrated development environment

Download RStudio Desktop

https://www.rstudio.com/

http://www.rstudio.com/products/rstudio/features/

32/54

(33)

客制化環境 33/54

(34)

Tools => Global Options... 34/54

(35)

新增專案及R程式檔

File => New Project... => New Directory => Empty Project =>

Create Project

File => New File => R Script

35/54

(36)

執行程式

以鍵盤選取程式碼:

配合Home, End, Shift, 上下左右鍵 執行: Ctrl + Enter 或 Ctrl + R

36/54

(37)

安排RStudio 專案目錄結構

制作一R套件的目錄結構

37/54

(38)

RStudio 常用快速鍵 (Windows)

控制台(Console)

游標移動到控制台 (Move cursor to Console): Ctrl+2

清除控制台 (Clear console): Ctrl+L

移動游標到行首 (Move cursor to beginning of line): Home

移動游標到行尾 (Move cursor to end of line): End

調出歷史指令 (Navigate command history): 上箭頭/下箭頭

彈出歷史指令 (Popup command history): Ctrl+上箭頭

中斷正在執行的指令 (Interrupt currently executing command): Esc

原始碼編輯區(Source)

游標移動到原始檔編輯區 (Move cursor to Source Editor): Ctrl+1

存檔 (Save active document): Ctrl+S

關閉當前檔案 (Close active document): Ctrl+W

執行當前行或者選擇的行 (Run current line/selection): Ctrl+Enter

執行當前檔 (Run current document): Ctrl+Alt+R

從檔案開頭執行到該行 (Run from document beginning to current line): Ctrl+Alt+B

行縮排 (Reindent lines) : Ctrl+I

註解(或消除)當前行或者所選程式碼 (Comment/uncomment current line/selection): Ctrl+Shift+C

跳轉到配對的括弧 (Jump to Matching Brace/Parentheses): Ctrl+P

刪除行 (Delete Line): Ctrl+D

選擇 (Select): Shift+箭頭

一般

尋找和替換 (Find and Replace): Ctrl+F; 尋找上一個 (Find Previous): Shift+F3; 尋找下一個(Find Next): F3

上一步 (Undo ): Ctrl+Z; 剪下 (Cut): Ctrl+X; 複制 (Copy): Ctrl+C; 貼上 (Paste): Ctrl+V; 全選 (Select All): Ctrl+A RStudio 0.99.893

C:\Program Files\RStudio\www\docs\keyboard.htm RStudio,重新載入R:

選單: Session => Restart R 快速鍵: ctrl + shift + F10 指令: > .rs.restartR()

38/54

(39)

RStudio Help

See Also:

https://github.com/dspim/R/wiki/R-&-RStudio- Troubleshooting-Guide

https://www.rstudio.com/wp-

content/uploads/2016/01/rstudio-IDE-cheatsheet.pdf

39/54

(40)

RStudio Cheat Sheets

Sparklyr Cheat Sheet

R Markdown Cheat Sheet

RStudio IDE Cheat Sheet

Shiny Cheat Sheet

Data Visualization Cheat Sheet

Package Development Cheat Sheet

Data Wrangling Cheat Sheet

R Markdown Reference Guide

Contributed Cheatsheets

Base R

Advanced R

Regular Expressions

How big is your graph? (base R graphics)

https://www.rstudio.com/resources/cheatsheets/

40/54

(41)

R Packages Developed by RStudio

https://www.rstudio.com/products/rpackages/

https://support.rstudio.com/hc/en-

us/articles/201057987-Quick-list-of-useful-R-packages

Quick list of useful R packages

To load data: RODBC, RMySQL, RPostgresSQL, RSQLite;

XLConnect, xlsx; foreign.

To manipulate data: dplyr tidyr stringr lubridate.

To visualize data: ggplot2 ggvis rgl - htmlwidgets googleVis.

To model data: car lme4/nlme, randomForest, multcomp, vcd, glmnet, survival, caret.

To report results: shiny, RMarkdown, xtable.

For Spatial data: sp, maptools, maps, ggmap.

For Time Series and Financial data: zoo, xts, quantmod.

To write high performance R code: Rcpp, data.table, parallel.

To work with the web: XML, jsonlite, httr.

41/54

(42)

更新R軟體及R套件

安裝R套件

利用installr套件更新R軟體及R套件

> update.packages(checkBuilt=TRUE, ask=FALSE)

RGui => Packages => Update packages

RStudio => Tools => Check for Package Updates...

Note: 先暫時不移除舊版的R及套件!

> setRepositories()

--- Please select repositories for use in this session ---

1: + CRAN

2: BioC software 3: BioC annotation 4: BioC experiment 5: BioC extra

6: CRAN (extras) 7: Omegahat

8: R-Forge 9: rforge.net

Enter one or more numbers separated by spaces, or an empty line to cancel

1: 2

> update.packages("rlang")

> uninstall.packages("rlang")

42/54

(43)

> version

_ platform x86_64-w64-mingw32 arch x86_64 os mingw32 system x86_64, mingw32 status major 3 minor 0.2 year 2013 month 09 day 25 svn rev 63987 language R version.string R version 3.0.2 (2013-09-25) nickname Frisbee Sailing

利用installr套件更新R軟體

> library(installr)

> installr()

> sessionInfo()

43/54

(44)

學習資源

http://www.r-project.org

The R Journal、useR!研討會、Books

rstudio::confis about all things R and RStudio! https://www.rstudio.com/conference/

Google、Youtube、PTT、Taiwan R User Group

中華R軟體學會、中華R軟體研發暨應用協會

R Reference Card 2.0: http://cran.r-project.org/doc/contrib/Baggott-refcard-v2.pdf

R-bloggers: https://www.r-bloggers.com/

RDataMining.com: R and Data Mining: http://www.rdatamining.com/

100 Free Tutorials for learning R: http://www.datasciencecentral.com/profiles/blogs/100-free- tutorials-for-learning-r

44/54

(45)

R-bloggers

https://www.r-bloggers.com

45/54

(46)

相關中文書目 (1) 46/54

(47)

相關中文書目 (2) 47/54

(48)

R/RStudio on the Phone

How to develop with R on an Apple Ipad Iphone or Android using Rstudio Server for Free

https://www.youtube.com/watch?v=fVU PWZMRIBw

在 Android 系统上安装 Debian Linux 与 R ( 更新 RStudio Server 安装), 2015-08-04

https://linuxtoy.org/archives/install- debian-and-r-on-android.html

Install R in Android, via GNURoot -no root required! September 30, 2014, By mareviv

http://www.r-bloggers.com/install-r-in- android-via-gnuroot-no-root-required/

48/54

(49)

Rcmdr : R Commander 49/54

(50)

http://r.analyticflow.com/en/

© Ef-prime, Inc.

R Programming Support

50/54

(51)

系統資訊/參數

> # R.Version: Version Information

> as.data. frame(R.Version())

platform arch os system status major minor year month day svn.rev 1 x86_64-w64-mingw32 x86_64 mingw32 x86_64, mingw32 3 3.1 2016 06 21 70800

language version.string nickname 1 R R version 3.3.1 (2016-06-21) Bug in Your Hair

>

> # Sys.info: Extract System and User Information

> Sys.info()

sysname release version nodename machine login user effective_user

"Windows" "8.1 x64" "build 9600" "HMWU-HOME" "x86-64" "userpc" "userpc" "userpc"

>

> # .Platform: Platform Specific Variables

> as.data.frame(.Platform)

OS.type file.sep dynlib.ext GUI endian pkgType path.sep r_arch 1 windows / .dll Rgui little win.binary ; x64

>

> # .Machine {base}: Numerical Characteristics of the Machine

> as.data.frame(.Machine)

double.eps double.neg.eps double.xmin double.xmax double.base double.digits 1 2.220446e-16 1.110223e-16 2.225074e-308 1.797693e+308 2 53

double.rounding double.guard double.ulp.digits double.neg.ulp.digits double.exponent 1 5 0 -52 -53 11

double.min.exp double.max.exp integer.max sizeof.long sizeof.longlong sizeof.longdouble 1 -1022 1024 2147483647 4 8 16

sizeof.pointer 1 8

51/54

(52)

sessionInfo()

> sessionInfo()

R version 3.4.0 (2017-04-21)

Platform: i386-w64-mingw32/i386 (32-bit) Running under: Windows >= 8 x64 (build 9200) locale:

[1] LC_COLLATE=Chinese (Traditional)_Taiwan.950 [2] LC_CTYPE=Chinese (Traditional)_Taiwan.950 [3] LC_MONETARY=Chinese (Traditional)_Taiwan.950 [4] LC_NUMERIC=C [5] LC_TIME=Chinese (Traditional)_Taiwan.950 attached base packages:

[1] grid parallel stats graphics grDevices utils [7] datasets methods base

other attached packages:

[1] gridExtra_2.2.1 gridGraphics_0.2 corrplot_0.77

loaded via a namespace (and not attached):

[1] Rcpp_0.12.9 ggpubr_0.1.5 plyr_1.8.4 [4] bindr_0.1 tools_3.3.1 digest_0.6.10

> RStudio.Version()

$citation ...

$mode

[1] "desktop"

$version

[1] ‘1.0.143’

# 查詢套件版本

> packageVersion("rlang") [1] ‘0.4.5’

52/54

(53)

> iris

The Most Frequently Used Data Set

UCI Machine Learning Repository

https://archive.ics.uci.edu/ml/

StatLib: http://lib.stat.cmu.edu/

政府資料開放平台

http://data.gov.tw/

開放資料Open Data http://www.opendata.tw/

2014 IASC Data Analysis Competition: http://www.iasc-isi.org/node/227 - World Bank -http://data.worldbank.org

- United Nations -http://www.un.org/en/databases/#stats

- World Health Organization -http://www.who.int/research/en/

The sepal length, sepal width, petal length, and petal width are measured in centimeters on 50 iris specimens from each of three species, Iris setosa, I. versicolor, and I. virginica. Fisher (1936)

53/54

(54)

一些RStudio 技巧

My favorite RStudio tips and tricks

http://datacornering.com/my-favorite-rstudio-tips-and-

tricks/?fbclid=IwAR2ZK9Fu3Q5j_wUQVo-tFuX0lxP4Zp2TwcfWKXHyV2R-C6EIgG1Gn-t3uJg

套件安裝問題,可能的解決方法:

(1) 以「系統管理者員身份執行」開啟RStudio。

(2) 改變R套件安裝資料夾「.libPaths」

https://www.twblogs.net/a/5cc0edfcbd9eee397113dede

(3) 解決RStudio不支持Windows系统中文用户名問題

https://blog.csdn.net/qq_16146103/article/details/105445198

(4) win10用户名為中文導致RStudio繪圖錯誤的解決方法 https://www.cnblogs.com/yanjiamin/p/12064048.html

(5) Windows中文使用者與RStudio的環境變數調校 http://shorturl.at/AMNQ9

(6) 解決RStudio中的亂碼

https://itw01.com/5XYZENB.html

(7) 如何更改R的預設語系

https://psmethods.postach.io/post/ru-he-geng-gai-rde-yu-she-yu-xi

中文帳戶名問題

54/54

參考文獻

相關文件

Rather than requiring a physical press of the reset button before an upload, the Arduino Uno is designed in a way that allows it to be reset by software running on a

[r]

The Seed project, REEL to REAL (R2R): Learning English and Developing 21st Century Skills through Film-making in Key Stage 2, aims to explore ways to use film-making as a means

反之, 有了 parametric equation, 我們可利用這些在 R n 的 direction vectors, 利 用解聯立方程組的方法求出和這些 direction vectors 垂直的 normal vectors,

而利用 row vectors 的方法, 由於可以化為 reduced echelon form, 而 basis 是由此 reduced echelon form 中的 nonzero vectors 所組成, 所以雖然和來的 spanning

(ii) “The dismissal of any teacher who is employed in the school – (a) to occupy a teacher post in the establishment of staff provided for in the code of aid for primary

(ii) “The dismissal of any teacher who is employed in the school – (a) to occupy a teacher post in the establishment of staff provided for in the code of aid for primary

Hence, we have shown the S-duality at the Poisson level for a D3-brane in R-R and NS-NS backgrounds.... Hence, we have shown the S-duality at the Poisson level for a D3-brane in R-R