├── .gitmodules ├── DSC2015R ├── .Rbuildignore ├── DESCRIPTION ├── NAMESPACE ├── R │ └── utility.R ├── configure └── inst │ └── project │ ├── demo1.R │ └── percent_operating_reserve.csv ├── Dockerfile ├── README.md └── init.R /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "DSC2015R/inst/visualization"] 2 | path = DSC2015R/inst/visualization 3 | url = https://github.com/everdark/ggplot2_lecture_dsc2015 4 | [submodule "DSC2015R/inst/basic/swirl"] 5 | path = DSC2015R/inst/basic/swirl 6 | url = https://github.com/dboyliao/DSC2015_RBasic.git 7 | [submodule "DSC2015R/inst/etl/slide"] 8 | path = DSC2015R/inst/etl/slide 9 | url = https://github.com/ntuaha/R_ETL_DSC_2015 10 | [submodule "DSC2015R/inst/etl/swirl"] 11 | path = DSC2015R/inst/etl/swirl 12 | url = https://github.com/ntuaha/DSC_R_ETL_SWIRL_2015 13 | [submodule "DSC2015R/inst/show/RTextMining"] 14 | path = DSC2015R/inst/show/RTextMining 15 | url = https://github.com/whizzalan/RTextMining.git 16 | [submodule "DSC2015R/inst/show/data.table"] 17 | path = DSC2015R/inst/show/data.table 18 | url = https://github.com/mansunkuo/dsc2015_data.table.git 19 | [submodule "DSC2015R/inst/show/baseball"] 20 | path = DSC2015R/inst/show/baseball 21 | url = https://github.com/suensummit/lightning-DSC2015-R.git 22 | [submodule "DSC2015R/inst/show/crawler"] 23 | path = DSC2015R/inst/show/crawler 24 | url = https://github.com/ChihChengLiang/DSC2015_Crawler.git 25 | [submodule "DSC2015R/inst/show/recharts"] 26 | path = DSC2015R/inst/show/recharts 27 | url = https://github.com/KuiMing/lightening_recharts.git 28 | [submodule "DSC2015R/inst/show/rcpp"] 29 | path = DSC2015R/inst/show/rcpp 30 | url = https://github.com/everdark/rcpp_lightning_dsc2015.git 31 | -------------------------------------------------------------------------------- /DSC2015R/.Rbuildignore: -------------------------------------------------------------------------------- 1 | .git 2 | index_cache 3 | DSC2015.zip$ -------------------------------------------------------------------------------- /DSC2015R/DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: DSC2015R 2 | Type: Package 3 | Title: Materials for R Workshop in Data Science Conference in Taiwan 2015 4 | Version: 1.1 5 | Date: 2015-07-27 6 | Author: Wush Wu 7 | Maintainer: Wush Wu 8 | Description: Slides, data, swirl courses of R workshop. 9 | License: GPL (>= 3) 10 | Depends: dplyr, ggplot2, swirl 11 | Imports: DataTaipei 12 | -------------------------------------------------------------------------------- /DSC2015R/NAMESPACE: -------------------------------------------------------------------------------- 1 | exportPattern("^[[:alpha:]]+") 2 | -------------------------------------------------------------------------------- /DSC2015R/R/utility.R: -------------------------------------------------------------------------------- 1 | pkg_file <- function(...) { 2 | retval <- system.file(..., package = .packageName) 3 | retval <- normalizePath(retval) 4 | cat(sprintf("%s\n", retval)) 5 | retval 6 | } 7 | 8 | install_course <- function() { 9 | library(swirl) 10 | if (!file.exists(get_swirl_option("courses_dir"))) { 11 | dir.create(get_swirl_option("courses_dir"), recursive = TRUE) 12 | } 13 | cat("Installing swirl course for R basic...\n") 14 | install_course_directory(pkg_file("basic/swirl/DSC2015")) 15 | cat("Installing swirl course for R ETL\n") 16 | install_course_directory(pkg_file("etl/swirl/DSC2015")) 17 | cat("Installing swirl course for R Visualization\n") 18 | install_course_directory(pkg_file("visualization/DSC2015")) 19 | } 20 | 21 | slide <- function() { 22 | cat(" 23 | 1: R swirl (DBoy) 2: R Basic (Ning) 3: R Basic (Noah)\n4: R ETL (aha) 5: Visualization (Kyle, Mansun, Ben) 24 | 6: OpenCPU 7: Time Series Clustering 8: Quantstrats 9: R Text Mining 10: Baseball 25 | 11: Crawler 12: Rcpp 13: recharts 26 | Please enter the slide number: 27 | ") 28 | switch(readline(), 29 | "1" = browseURL(pkg_file("basic/swirl/slides/RBasic0.html")), 30 | "2" = browseURL(pkg_file("basic/swirl/slides/RBasic1.html")), 31 | "3" = browseURL(pkg_file("basic/swirl/slides/RBasic2.html")), 32 | "4" = browseURL(pkg_file("etl/slide/index.html")), 33 | "5" = browseURL(pkg_file("visualization/index.html")), 34 | "6" = browseURL("https://docs.google.com/presentation/d/1VlmgZ5pTjmOf-qSa2iLWpeas12NxTzx3nIQLJiLn8YQ/edit?usp=sharing"), 35 | "7" = browseURL("https://docs.google.com/presentation/d/180wT8FunXbGFGqj8Kv3p7mtHJ7g-BJt9XipRIlmNMEE/edit?usp=sharing"), 36 | "8" = browseURL("https://docs.google.com/presentation/d/16xjdjXf6hSdpV0hZDjNth9LdyVMVKkXs5WZHg6IFCTU/edit?usp=sharing"), 37 | "9" = browseURL(pkg_file("show/RTextMining/lightingTalk/index.html")), 38 | "10" = browseURL(pkg_file("show/baseball/index.html")), 39 | "11" = browseURL(pkg_file("show/crawler/index.html")), 40 | "12" = browseURL(pkg_file("show/rcpp/index.html")), 41 | "13" = browseURL(pkg_file("show/recharts/recharts.html")) 42 | ) 43 | } 44 | 45 | demo_project <- function() { 46 | browseURL(pkg_file("project/demo1.R")) 47 | } -------------------------------------------------------------------------------- /DSC2015R/configure: -------------------------------------------------------------------------------- 1 | echo "This is unix-like system" 2 | echo "Copying data ecoded with UTF-8" 3 | 4 | cp inst/basic/swirl/DSC2015/R_Basic_Part_1-Data_Structure_in_R/initLesson-utf8.R inst/basic/swirl/DSC2015/R_Basic_Part_1-Data_Structure_in_R/initLesson.R 5 | -------------------------------------------------------------------------------- /DSC2015R/inst/project/demo1.R: -------------------------------------------------------------------------------- 1 | #' ## 台灣用電初探 2 | #' 3 | #' 台灣電力短缺的問題日益嚴重。 4 | #' 本專案想帶領各位同學,利用政府的公開資料, 5 | #' 以及年會課程學到的資料處理技術, 6 | #' 初步分析各產業別用電的經濟效應。 7 | #' 8 | #' ## 台電歷年尖峰負載及備用容量率 9 | #' 10 | #' 我國的政府公開資料平台中,有提供 11 | #' [台電歷年尖峰負載及備用容量率](http://data.gov.tw/node/8307)的資料。 12 | #' 下載之後,我們可以利用read.table和file的函數來告知R 資料的文字編碼, 13 | #' 並將表格讀入R中。 14 | #' 透過網站說明,我們需要使用UTF-16作為編碼,才能正確解碼檔案中的中文。 15 | 16 | # 檢查檔案內容是否正確 17 | 18 | setwd(system.file("project", package = "DSC2015R")) 19 | stopifnot(tools::md5sum("percent_operating_reserve.csv") == "aa6800cb2c6421358dfad782dc95dcfa") 20 | 21 | # 載入資料 22 | # **以下的程式碼出錯了,請校正** 23 | 24 | percent_operating_reserve <- read.table(file(percent_operating_reserve.csv, encoding = "UTF-16"), 25 | header = TRUE, check.names = FALSE) 26 | 27 | stopifnot(nrow(percent_operating_reserve) == 33) 28 | 29 | #' ### 計算歷年平均負載 30 | #' 在描述一個連串數據時,我們最常使用的是平均數。 31 | #' 以下範例的目的是要先整理資料後,計算歷年來尖峰負載的平均。 32 | 33 | # 透過查詢colnames,我們知道尖峰負載是在第二行,單位是:「萬瓩」 34 | colnames(percent_operating_reserve) 35 | 36 | # 檢查raw data 37 | head(percent_operating_reserve[[2]]) 38 | 39 | # 但是直接計算歷年尖峰負載的平均會導致以下錯誤: 40 | 41 | mean(percent_operating_reserve[[2]]) 42 | 43 | # 先利用class查詢第二行的形態,請修改下列的程式碼 44 | 45 | class(percent_operating_reserve<<你的程式碼>>) 46 | 47 | #' factor不是數值形態,所以R如果直接計算平均數的時候,會回傳NA(Not Available)。 48 | #' ps. 同學可以仔細閱讀R的警告訊息: 49 | #' In mean.default(percent_operating_reserve[[2]]): argument is not numeric or 50 | #' logical: returning NA 51 | #' 裡面就明確地指出,參數不是numeric或logical,所以回傳NA。 52 | 53 | # 利用as.numeric函數做轉換 54 | annual_max_power <- as.numeric(<<你的程式碼>>) 55 | 56 | # 利用mean計算轉換後的平均 57 | print(annual_max_power_mean <- mean(<<你的程式碼>>)) 58 | stopifnot(annual_max_power_mean == 17) 59 | 60 | #' 各位同學會不會覺得算出來的數字怪怪的:平均數居然是一個乾淨的整數, 61 | #' 通常平均數都會混雜大量的小數。 62 | #' 如果仔細比對as.numeric(percent_operating_reserve[[2]])和 63 | #' head(percent_operating_reserve[[2]])的結果, 64 | #' 就會發現as.numeric的結果不正確。 65 | #' 在R之中,對於factor形態正確的處理方式, 66 | #' 是透過as.character + as.numeric: 67 | 68 | as.numeric(as.character(<<你的程式碼>>)) 69 | 70 | #' 我們在結果中看到了NA,例如percent_operating_reserve的第6個元素: 71 | 72 | percent_operating_reserve[[2]][6] 73 | 74 | #' 我們可以看到,第六個元素包含了,,而這是在實際處理數據常常遇到的狀況。 75 | #' 為了要讓R計算正確的結果,我們需要先用gsub把,移除: 76 | 77 | # 請利用gsub移除文字中的“," 78 | 79 | annual_max_power_cleaned <- gsub(pattern = ",", replacement = "", x = percent_operating_reserve[[2]]) 80 | 81 | #' gsub也直接會把輸出結果轉換成character,所以我們不需要再使用as.character, 82 | #' 直接使用as.numeric即可。 83 | 84 | # 請利用as.numeric將資料轉換成數值形態 85 | 86 | annual_max_power <- as.numeric(<<你的程式碼>>) 87 | 88 | # 請利用mean計算歷年尖峰負載的平均 89 | 90 | annual_max_power_mean <- mean(<<你的程式碼>>) 91 | 92 | stopifnot(all.equal(annual_max_power_mean, 2233.903)) 93 | 94 | #' ### 繪製歷年尖峰負載圖 95 | 96 | library(ggplot2) 97 | df <- data.frame(year = percent_operating_reserve[[1]], 98 | max_power = annual_max_power) 99 | ggplot(df, aes(x = <<你的程式碼>>, y = <<你的程式碼>>)) + 100 | geom_line() + 101 | ggtitle("test") 102 | 103 | 104 | 105 | -------------------------------------------------------------------------------- /DSC2015R/inst/project/percent_operating_reserve.csv: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TaiwanRUserGroup/DSC2015Tutorial/1bc3ed8d501d0fdc6b13bb62593ecb16f57dd4d8/DSC2015R/inst/project/percent_operating_reserve.csv -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM rocker/rstudio 2 | 3 | MAINTAINER Chih-Cheng Liang and Wush Wu 4 | 5 | COPY init.R /tmp/init.R 6 | RUN apt-get update && apt-get install -y libcurl4-openssl-dev libxml2-dev 7 | RUN cd /tmp && Rscript init.R && rm init.R 8 | COPY DSC2015R /tmp/DSC2015R 9 | RUN cd /tmp && R CMD INSTALL DSC2015R && Rscript -e "library(DSC2015R);install_course()" 10 | 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # R 語言資料分析上手課程 [Taiwan R User Group](http://www.meetup.com/Taiwan-R/) 2 | 3 | 目錄 4 | ================= 5 | 6 | * [R 語言資料分析上手課程](#r-語言資料分析上手課程) 7 | * [準備自己的電腦](#準備自己的電腦) 8 | * [安裝R](#安裝r) 9 | * [Windows](#windows) 10 | * [Mac](#mac) 11 | * [Ubuntu](#ubuntu) 12 | * [安裝Rstudio](#安裝rstudio) 13 | * [安裝課程所需套件](#安裝課程所需套件) 14 | * [安裝swirl課程](#安裝swirl課程) 15 | * [打開課程投影片](#打開課程投影片) 16 | * [使用Docker環境](#使用docker環境) 17 | * [使用雲端學習平台](#使用雲端學習平台) 18 | 19 | Created by [gh-md-toc](https://github.com/ekalinin/github-markdown-toc) 20 | 21 | **最新DSC2015R套件版本: 0.2.1** 22 | 23 | 這次年會,我們提供兩種讓學員在課程之中使用R 的方式: 24 | 25 | - 使用自己的筆電安裝R 3.2以上的版本及課程所需的套件 26 | - 使用年會提供的雲端學習平台 27 | 28 | ## 準備自己的電腦 29 | 30 | 如果學員擁有足夠的電腦知識,願意自行在筆電上安裝R 軟體,則請依照您的作業系統,參考以下的內容安裝R 軟體與相關套件。 31 | 32 | ### 安裝R 33 | 34 | #### Windows 35 | 36 | 請至 [CRAN](http://cran.csie.ntu.edu.tw) 上下載 R3.2 以上的安裝程式。 37 | 38 | 或參考 [![在Windows上安裝R](http://img.youtube.com/vi/FsOHPGUIDZU/0.jpg)](http://www.youtube.com/watch?v=FsOHPGUIDZU) 39 | 40 | 備註:影片中的範例是 3.0.2 的版本,請使用者記得安裝 3.2 以上的版本喔! 41 | 42 | #### Mac 43 | 44 | 請至 [CRAN](http://cran.csie.ntu.edu.tw) 上下載 R3.2 以上的安裝程式。 45 | 46 | 或參考 [![Mac OS X 安裝R軟體](http://img.youtube.com/vi/72MYRBNo5Bk/0.jpg)](http://www.youtube.com/watch?v=72MYRBNo5Bk) 47 | 48 | 備註:此影片由中華R軟體學會的李明昌老師提供 49 | 50 | #### Ubuntu 51 | 52 | Ubuntu 14.04 的使用者可以在 bash 執行以下指令來安裝學習環境: 53 | 54 | sudo sh -c 'echo "deb http://cran.csie.ntu.edu.tw/bin/linux/ubuntu trusty/" >> /etc/apt/sources.list' 55 | sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E084DAB9 56 | sudo apt-get update && apt-get install -y r-base libcurl4-openssl-dev libxml2-dev 57 | 58 | 如果是其他版本的使用者,請修改上述 trusty 的部份。 59 | 60 | ### 安裝[Rstudio](https://www.rstudio.com/products/RStudio/) 61 | 62 | 上課時,講師們使用的是Rstudio做講解,這是目前R最好的IDE。 63 | 64 | 如果學員想要安裝的話,請到[Rstudio Download](https://www.rstudio.com/products/rstudio/download/)依據你的作業系統來安裝課程。 65 | 66 | ### 安裝課程所需套件 67 | 68 | 安裝前,請**重新啟動Rsession**。重新啟動之後,請在R 的Console執行以下指令: 69 | 70 | #### 使用 Github Repository 71 | 72 | ```r 73 | source("http://taiwanrusergroup.github.io/R/init.R") 74 | ``` 75 | 76 | 或是 77 | 78 | ```r 79 | options(repos = "http://taiwanrusergroup.github.io/R") 80 | local({ 81 | install.packages('DSC2015R') 82 | pkgs.remote <- available.packages() 83 | pkgs.local <- installed.packages() 84 | pkgs <- intersect(rownames(pkgs.remote), rownames(pkgs.local)) 85 | pkgs.outdated <- package_version(pkgs.local[pkgs, "Version"]) != package_version(pkgs.remote[pkgs, "Version"]) 86 | if (sum(pkgs.outdated) > 0) install.packages(names(which(pkgs.outdated))) 87 | }) 88 | ``` 89 | 90 | (臺大分流已經關閉了) 91 | 92 | ### 安裝swirl課程 93 | 94 | ```r 95 | library(DSC2015R) 96 | install_course() 97 | ``` 98 | 99 | ### 打開課程投影片 100 | 101 | 我們建議學員使用firefox作為閱讀投影片的瀏覽器。 102 | 103 | ```r 104 | library(DSC2015R) 105 | slide() 106 | ``` 107 | 108 | 執行後會出現如下畫面: 109 | 110 | ```sh 111 | > slide() 112 | 1: R swirl (DBoy) 2: R Basic (Ning) 3: R Basic (Noah) 113 | 4: R ETL (aha) 5: Visualization (Kyle, Mansun, Ben) 114 | ``` 115 | 116 | 請依照指示輸入你想要打開的課程投影片即可。例如,若你想要打開R ETL (aha)的投影片,請輸入`4`。 117 | 118 | ## 使用Docker環境 119 | 120 | 如果你是熟悉Docker的工程師,我們提供一個Docker Image在網路上給大家使用。 121 | 122 | **如果你不知道什麼是Docker,請略過這段** 123 | 124 | ```sh 125 | docker pull twrusergroup/dsc2015tutorial 126 | docker run -d -p 8787:8787 twrusergroup/dsc2015tutorial 127 | ``` 128 | 129 | 使用瀏覽器開啟 130 | 131 | 帳號密碼如下 132 | 133 | - username: rstudio 134 | - password: rstudio 135 | 136 | 137 | ## 使用雲端學習平台 138 | 139 | 如果上課當天電腦出狀況以至於無法使用R,但仍然有網路連線,可以使用線上寫程式的環境。使用方式: 140 | 141 | 1. 請至報名課程使用的電子信箱取得平台註冊邀請網址,網址會在開課前一天晚上寄出。 142 | 2. 依照 [http://chihchengliang.github.io/platform-instruction/](http://chihchengliang.github.io/platform-instruction/)的操作說明使用線上平台。 143 | 144 | ## Q&A 145 | 146 | 如果操作中遇到錯誤,歡迎參考 [Wiki Q&A](https://github.com/TaiwanRUserGroup/DSC2015Tutorial/wiki/Q&A) 的部分來嘗試解決問題。 147 | 148 | 需要協助的話,上課過程中可以請教助教。如果是自學的朋友,可以 [發Issue](https://github.com/TaiwanRUserGroup/DSC2015Tutorial/issues/new) 詢問管理員,或是和其他網友討論解決。 149 | 150 | 解決之後,也歡迎直接把解決辦法貢獻到wiki 151 | -------------------------------------------------------------------------------- /init.R: -------------------------------------------------------------------------------- 1 | options(repos = "http://taiwanrusergroup.github.io/R") 2 | local({ 3 | install.packages('DSC2015R') 4 | pkgs.remote <- available.packages() 5 | pkgs.local <- installed.packages() 6 | pkgs <- intersect(rownames(pkgs.remote), rownames(pkgs.local)) 7 | pkgs.outdated <- package_version(pkgs.local[pkgs, "Version"]) != package_version(pkgs.remote[pkgs, "Version"]) 8 | if (sum(pkgs.outdated) > 0) install.packages(names(which(pkgs.outdated))) 9 | }) --------------------------------------------------------------------------------