简体   繁体   English

如何创建 .Renviron 文件?

[英]How to create .Renviron file?

I need to use my user defined folder where I stored my packages.我需要使用我的用户定义文件夹来存储我的包。 I have referred the link given below.我已经提到了下面给出的链接。

R libraries installation R 库安装

I have applied the same but it didn't work for me.我已经应用了相同的但它对我不起作用。 I have created a file called .Renviron with data R_LIBS=c:/Rpackages ,where my packages installed.我创建了一个名为 .Renviron 的文件,其中包含数据 R_LIBS=c:/Rpackages ,其中安装了我的软件包。 But, still it is showing there is no such package 'RODBC' exists.但是,它仍然显示不存在这样的包“RODBC”。 Also, I put this file in my Documents folder in C drive and tried.另外,我将此文件放在我的 C 驱动器的 Documents 文件夹中并尝试过。 That also gave the same error.这也给出了同样的错误。 Still the file type is showing as text document.文件类型仍然显示为文本文档。 How to change this file type?如何更改此文件类型?

On mac (or linux)在 mac(或 linux)上

Open the terminal and type打开终端并输入

touch $HOME/.Renviron

To open the file you just created, navigate through finder to /Users/<your-user-name>/.Renviron , or simply open the terminal and type要打开您刚刚创建的文件,请通过 finder 导航到/Users/<your-user-name>/.Renviron ,或者只需打开终端并键入

open $HOME/.Renviron

On windows在窗户上

Click on start and open powershell.单击开始并打开powershell。 Copy this code into powershell将此代码复制到powershell

Add-Content c:\Users\$env:USERNAME\Documents\.Renviron "TEST_VARIABLE_1=my_username"
Add-Content c:\Users\$env:USERNAME\Documents\.Renviron "TEST_VARIABLE_2=123"

You'll now have a file called .Renviron located in the Documents folder.现在,您将在Documents文件夹中拥有一个名为.Renviron的文件。 Close and reopen RStudio .关闭并重新打开 RStudio Then run Sys.getenv('TEST_VARIABLE_1') to access the variable in R (obviously that works for any other environment variables you set as well).然后运行Sys.getenv('TEST_VARIABLE_1')以访问 R 中的变量(显然这也适用于您设置的任何其他环境变量)。

Look at help(Startup) which has an example at the end:看看help(Startup) ,它在最后有一个例子:

 ## Example ~/.Renviron on Unix
 R_LIBS=~/R/library
 PAGER=/usr/local/bin/less

 ## Example .Renviron on Windows
 R_LIBS=C:/R/library
 MY_TCLTK="c:/Program Files/Tcl/bin"

 ## Example of setting R_DEFAULT_PACKAGES (from R CMD check)
 R_DEFAULT_PACKAGES='utils,grDevices,graphics,stats'
 # this loads the packages in the order given, so they appear on
 # the search path in reverse order.

But note the spelling: Renviron with lower case e.但请注意拼写: Renviron小写 e。 Just use a text editor and edit the file.只需使用文本编辑器并编辑文件。 Also note that R has a variant in one of its system folder etc/ below its RHOME :另请注意,R 在其RHOME下方的系统文件夹etc/中有一个变体:

edd@bud:~$ R RHOME
/usr/lib/R
edd@bud:~$ cat $(R RHOME)/etc/Renviron.site
##                                              Emacs please make this -*- R -*-
## empty Renviron.site for R on Debian
##
## Copyright (C) 2008 Dirk Eddelbuettel and GPL'ed
##
## see help(Startup) for documentation on ~/.Renviron and Renviron.site

# ## Example ~/.Renviron on Unix
# R_LIBS=~/R/library
# PAGER=/usr/local/bin/less

# ## Example .Renviron on Windows
# R_LIBS=C:/R/library
# MY_TCLTK="c:/Program Files/Tcl/bin"

# ## Example of setting R_DEFAULT_PACKAGES (from R CMD check)
# R_DEFAULT_PACKAGES='utils,grDevices,graphics,stats'
# # this loads the packages in the order given, so they appear on
# # the search path in reverse order.
edd@bud:~$ 

Hm.嗯。 Looks like I wrote that for the Debian package and it does always exist.看起来我是为 Debian 软件包编写的,它确实一直存在。 You can still copy it.你仍然可以复制它。

It may be worth adding that package usethis now offers a series of function facilitating easy editing and opening of R startup files.值得补充的是,包usethis现在提供了一系列功能,便于轻松编辑和打开 R 启动文件。 Family of edit_* functions can be used to conveniently edit desired startup and configuration files. edit_*函数系列可用于方便地编辑所需的启动和配置文件。 To edit .Renviron , which is appropriate place to store the relevant configuration use:要编辑.Renviron ,这是存储相关配置的合适位置,请使用:

# Edit Renviron
usethis::edit_r_environ()
# You can also consider creating project-specific settings:
usethis::edit_r_environ(scope = "project")

Usethis provides other functions useful for managing R configuration. Usethis提供了其他对管理 R 配置有用的函数。 For instance in order to edit Makevars you can do:例如,为了编辑Makevars您可以执行以下操作:

# Edit .R/Makevars
usethis::edit_r_makevars()

On Mac The Renviron file is located in在 Mac 上, Renviron文件位于

/Library/Frameworks/R.framework/Versions/Current/Resources/etc/

Creating a .Renviron file in the User's home directory will not help.在用户的主目录中创建 .Renviron 文件将无济于事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM