輸入數據
使用鍵盤輸入數據
只能處理小樣本,很少使用
在創建 data.txt 字符串之后,用函數 read.table() 創建數據框 data.1。這種方法可以讓我們把數據嵌入到R代碼中,此處切記,read.table() 使我們最為常用的讀取外部數據的函數。
下面的方法是用函數 fix() 創建了一個和data.1一樣的數據框 data.2,函數 edit() 也有同樣的效果。
從文本文件導入數據 read.table()
函數 read.table() 更加常用的是從帶有分隔符的文本文件中國讀入數據並創建數據框。類似的函數有 read.csv(),read.csv2()
例子
data.3 <- read.table(file="example.txt", header = T, quote = "\"", sep = ",") # header = T 表示第一行是變量名不做事數據一部分 header = F 表示第一行也作為數據的一部分
data.4 <- read.table(file="example.txt", header = T, skip = 7, sep = ",", quote = "\"", row.names = "Study")
從 xlsx 文件中讀入數據
需要使用R包 openxlsx 或者 readxl
輸出數據
輸出函數:cat() print() write() sink() writeLines() write.table()
在R里,輸出到屏幕不要用print() 而是用cat()。因為 print() 輸出的字符都是只能顯示在控制台,而控制台只能顯示文本,所以回車符 制表符都會員樣輸出
cat() 函數既能輸出到屏幕也能輸出到文件 需要手動添加換行符
cat(..., file = " ", sep = " ", fill = FALSE, labels = NULL, append = FALSE)
有file時,輸出到file;無file時,輸出到屏幕
append 參數:布爾值。TRUE 輸出內容追加到文件尾部,FALSE 輸出的內容覆蓋文件的原始內容。
print() 函數 原封不動的輸出對象 比對列表
print(x, ...) ## S3 method for class 'factor' print(x, quote = FALSE, max.levels = NULL, width = getOption("width"), ...) ## S3 method for class 'table' print(x, digits = getOption("digits"), quote = FALSE, na.print = "", zero.print = "0", right = is.numeric(x) || is.complex(x), justify = "none", ...) ## S3 method for class 'function' print(x, useSource = TRUE, ...)
write()用於輸出到文件,也可以輸出到標准輸出,無需手動添加換行符
write(x, file = "data", ncolumns = if(is.character(x)) 1 else 5, append = FALSE, sep = " ")
當 file = "" 時 輸出到標准輸出
sink() 函數將輸出結果重定向到文件
sink(file = NULL, append =FALSE, type = c("output", "message"), split = FALSE)
append參數:布爾值。TRUE時 輸出內容追加到文件尾部,FALSE時 覆蓋文件的原始內容。
writeLines() 函數將字符串向量輸出到文件中 也可以輸出到標准輸出 會覆蓋原始內容
writeLines(text, con = stdout(), sep = "\n", useBytes = FALSE)
text:字符串向量
con:輸出文件, 默認輸出到標准輸出
write.table()函數將 data.frame 的內容輸出到文件或者標准輸出
write.table(x file = "", append = FALSE, quote = TRUE, sep = " ", eol = "\n", na = "NA", dec = ",", row.names = TRUE, col.names = TRUE, qmethod = c("escape", "double"), fileEncoding = "")
讀取命令行參數
commandArgs() 是R自帶的參數傳遞,屬於位置參數
args = commandArgs(trailingOnly = FALSE) # R程序自帶參數,從第6個開始才是命令行輸入的參數
args = commandArgs(trailingOnly = TRUE) # 此時 輸入的第一個參數就是程序的第一個參數
如果改為 args = commandArgs(trailingOnly = FALSE) 輸出結果如下:
getopt 包中的 getopt() 函數也可以接受從終端傳遞的參數,並且功能更加豐富
getopt(spec = NULL, opt = commandArgs(TRUE), command = get_Rscript_filename(), usage = FALSE, debug = FALSE)
其中 spec 是一個4列或者5列的矩陣,里面包括了參數信息,前四列是必須的,第五列是可選的
第一列:指定參數的 longname ,多個字符
第二列:指定參數的 shortname,一個字符
第三列:指定參數是必須的還是可選的,數字,0 不接受參數 1 必須有參數 2 參數可選
第四列:指定參數的類型 logical integer double complex character numeric
第五列:注釋信息 可選
usage:默認為FALSE 這是參數無實際意義,而是以用法的形式輸出。
library(getopt) spec = matrix(c( 'verbose', 'v', 2, "integer", 'help' , 'h', 0, "logical", 'count' , 'c', 1, "integer", 'mean' , 'm', 1, "double", ), byrow=TRUE, ncol=4) opt = getopt(spec) print(opt$count) print(opt$mean)
如何制作腳本的幫助
command=matrix(c("bam","b",1,"character", "bed","d",1,"character", "png","p",1,"character", "help","h",0,"logical"),byrow=T,ncol=4) args=getopt(command) if (!is.null(args$help) || is.null(args$bam) || is.null(args$png) || is.null(args$bed)) { cat(paste(getopt(command, usage = T), "\n")) q() }