user139835
user139835

Reputation:

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

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. But, still it is showing there is no such package 'RODBC' exists. Also, I put this file in my Documents folder in C drive and tried. That also gave the same error. Still the file type is showing as text document. How to change this file type?

Upvotes: 35

Views: 79350

Answers (4)

Konrad
Konrad

Reputation: 18585

It may be worth adding that package usethis now offers a series of function facilitating easy editing and opening of R startup files. Family of edit_* functions can be used to conveniently edit desired startup and configuration files. To edit .Renviron, which is appropriate place to store the relevant configuration use:

# 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. For instance in order to edit Makevars you can do:

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

Upvotes: 25

Saqib Ali
Saqib Ali

Reputation: 4428

On Mac The Renviron file is located in

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

Creating a .Renviron file in the User's home directory will not help.

Upvotes: 2

stevec
stevec

Reputation: 52308

On mac (or 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

open $HOME/.Renviron

On windows

Click on start and open powershell. Copy this code into 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. Close and reopen 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).

Upvotes: 28

Dirk is no longer here
Dirk is no longer here

Reputation: 368261

Look at help(Startup) which has an example at the end:

 ## 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. 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:

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. You can still copy it.

Upvotes: 16

Related Questions