R語言環境變量的設置 環境設置函數為options()


環境設置函數為options(),用options()命令可以設置一些環境變量,使用help(options)可以查看詳細的參數信息。

1. 數字位數的設置,options(digits=n),n一般默認情況下是7位,但實際上的范圍是1~22,可以隨意設置位數。

#這個命令,可以把R的整數表示能力設為10位。 options(digits=10)

2. 擴展包的安裝,使用下面的命令,可以聯網安裝擴展包。

options(CRAN="http://cran.r-project.org") install.packages("擴展包名")

3. 利用R里的options函數進行光標和數字位數設置。

用R寫代碼時,打字水平不高,有時候不知道亂按了一些鍵(現在我還不知道哪個鍵),光標就變成了加粗的豎直線,又改不回去。這種情況下我們可以用options函數進行光標設置,例如:

##可以隨意設置你的光標類型(prompt參數設置)。 ##光標開始默認為 > options(prompt="|") ##光標為 | options(prompt=">") ##光標為 > options(prompt="|") ##光標為 | options(prompt="+") ##光標為 + options(prompt="-") ##光標為 - options(prompt="8") ##光標為 8 options(prompt=">") ##光標設置為開始默認值 >

4. R里的options函數進行錯誤信息顯示(忽略)設置。

#這個命令,可以忽視任何警告 options(warn=-1) #這個命令,不放過任何警告 options(warn=1) #示例展示 ow <- options("warn") for(w in -1:1) { options(warn = w); cat("\n warn =", w, "\n") for(i in 1:4) { cat(i,"..\n"); m <- matrix(1:7, 3,4) } } warnings() options(ow) # reset tail(warnings(), 2) #說明: warn: sets the handling of warning messages. If warn is negative all warnings are ignored. If warn is zero (the default) warnings are stored until the top–level function returns. If 10 or fewer warnings were signalled they will be printed otherwise a message saying how many were signalled. An object called last.warning is created and can be printed through the function warnings. If warn is one, warnings are printed as they occur. If warn is two or larger all warnings are turned into errors. ------- 警告: 設置警告消息的處理。如果警告是負面的,所有的警告都會被忽略。如果警告是零(默認)警告被存儲到頂部,級別的函數返回。如果10個或更少的警告信號,他們將被打印出來,否則一個消息說有多少人發出信號。一個對象稱為last.warning可以通過打印功能warnings。如果警告是一個,警告是印刷的,因為它們發生。如果警告是2或更大的所有警告被變成錯誤。 ##還可以width來調整向量,矩陣的輸出寬度,還包括是否用warnning來顯示錯誤信息(show.error.messages)以及錯誤信息的長度(warning.length)。 options(show.error.locations = TRUE) options("show.error.messages")

5. options()常用於設置R控制台、R語言計算相關的屬性,常用屬性名稱及默認值如下:

add.smooth TRUE
check.bounds FALSE continue "+ " digits 7 echo TRUE encoding "native.enc" error NULL expressions 5000 keep.source interactive() keep.source.pkgs FALSE max.print 99999 OutDec "." prompt "> " scipen 0 show.error.messages TRUE timeout 60 verbose FALSE warn 0 warning.length 1000 width 80 #裝載不同的擴展包還會增加一些新的屬性信息

6. 設置環境變量R_LIBS將R包安裝到自定義路徑

關於R啟動(help(Startup))和選項(help(options))的內容,參見R幫助頁面。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM