- 追加された行はこのように表示されます。
- 削除された行は
このように表示されます。
!Rの初期設定
Rを起動すると、プロファイル(.Rprofile)と環境変数(.Renviron)が読み込まれる。
それぞれの役割は次の通り。
:.Rprofile:
R起動時のバッチファイルと考えれば良い。例えば、起動するたびにパッケージの更新を確認するような手間を省くために、このファイルの中に、update.packages()を書いておくとよい。
*options() message()の設定
*CRANのミラーサイトresposの設定
*
:.Renviron:
Rの環境設定変数が保存される。具体的には
*Rのパッケージライブラリの保存ディレクトリ
*プログラムに直接記入してはならないAPIキーの保存
*
!初期設定ファイルの保存場所
初期設定ファイルは起動時に一度だけ読み込まれるが、同一名のファイルが複数のディレクトリに保存されている場合、優先順位がある。
:R_HOME: Rのシステムがインストールされているディレクトリ(R.home()で表示される。)のサブフォルダetc
:HOME: ユーザのホームディレクトリ(Linux: /home/username Windows: c:\Users\username
:作業ディレクトリ: getwd()で得られるディレクトリ
下ほど優先順位が高い。すなわち、作業ディレクトリに初期設定ファイルがあればそれを優先し、他は無視する。
!ファイルの作成方法
:プロジェクト固有の起動スクリプトを用意したい場合:
file.edit('.Rprofile')
※ホームディレクトリの.Rprofileは割愛される
:固有ディレクトリかHOMEディレクトリかの違い:
file.edit(file.path("~", ".Rprofile")) # edit .Rprofile in HOME
file.edit(".Rprofile") # edit project specific .Rprofile
file.edit(file.path("~", ".Rprofile")) # HOMEディレクトリの .Rprofile を編集する
file.edit(".Rprofile") # プロジェクト固有の .Rprofile を編集する
:.Renvironも同様の振る舞いをする:
APIキーなどをプロジェクトをまたがって利用したい場合、以下のようにする。
The following code will create a user specific .Renviron file (where API keys and other cross-project environment variables can be stored), without overwriting any existing file.
user_renviron = path.expand(file.path("~", ".Renviron"))
if(!file.exists(user_renviron)) # check to see if the file already exists
if(!file.exists(user_renviron)) # 既存のファイルがあるかチェック
file.create(user_renviron)
file.edit(user_renviron) # open with another text editor if this fails
file.edit(user_renviron) # テキストエディタで開くことも可能
!参考
* https://csgillespie.github.io/efficientR/r-startup.html