[R] Setting .Rprofile for RStudio on a Windows 7 x64bit

Henrik Bengtsson henrik.bengtsson at gmail.com
Sun Apr 16 00:19:13 CEST 2017


Hi.

First, there should be no difference in where and how R and RStudio
locate the R startup file.

Second, if there is an .Rprofile in the working directory (i.e.
./.Rprofile), then that file with have higher priority than the file
located in ~/.Rprofile.  You can use the following R calls, also on
Windows, to check if you have either of these two files:

> file <- normalizePath("./.Rprofile")
> file
> file.exist(file)

> file <- normalizePath("~/.Rprofile")
> file
> file.exist(file)

In my case, my working directory is C:/Users/hb/Documents/Projects/, I
have a ~/.Rprofile file, but not a .Rprofile in the working directory.
So, I get:

> file <- normalizePath("./.Rprofile")
> file
[1] "C:\\Users\\hb\\Documents\\Projects\\.Rprofile"
> file.exists(file)
[1] FALSE

> file <- normalizePath("~/.Rprofile")
> file
[1] "C:\\Users\\hb\\Documents\\.Rprofile"
> file.exists(file)
[1] TRUE

This tells me that my startup file that R tries to load / source
during startup is "C:\\Users\\hb\\Documents\\.Rprofile" and that's the
one I should edit.

BTW, the value of normalizePath("~/.Rprofile") and
file.path(Sys.getenv("HOME"), ".Rprofile") should point to the same
file, expect that normalizePath() makes all backward slashed on
Windows; the former is just a neater version to use:

> normalizePath("~/.Rprofile")
[1] "C:\\Users\\hb\\Documents\\.Rprofile"

> file.path(Sys.getenv("HOME"), ".Rprofile")
[1] "C:/Users/hb/Documents/.Rprofile"

> normalizePath(file.path(Sys.getenv("HOME"), ".Rprofile"))
[1] "C:\\Users\\hb\\Documents\\.Rprofile"

(all of the above reference the same file).

So, if file.exists(normalizePath("~/.Rprofile")) gives FALSE, then you
don't have that file.  If you think you've edited that, then it might
be that you hit the peculiar Windows property where it hides the
filename extension from you in the Explorer.  It might be that you
instead have created / edited the file:

normalizePath("~/.Rprofile.txt")

That often happens when one uses Notepad and saves the file as
.Rprofile - Notepad simply add a *.txt filename extension unless you
save it with quotation marks in the Save-As panel.

Now, if you indeed have the file:

normalizePath("~/.Rprofile")

then there is one last annoyance in R that you might have hit.   If you're last
line in that file does not have a newline, the the file will be
silently ignored by R when R start.  There won't be a warning - not
even a message.  That is true for all OSes.  It's a "feature" that
should really be fixed, because I keep seeing it tricking beginners
and advanced R users all the times.  The easiest way to check if this
is your problem, use readLines() to read in the content; readLines()
will give a warning if the last line doesn't have a new line, e.g.

> readLines(normalizePath("~/.Rprofile"))
[1] "options(prompt=\"R> \")" "set.seed(12345)"
Warning message:
In readLines(normalizePath("~/.Rprofile")) :
  incomplete final line found on 'C:\Users\hb\Documents\.Rprofile'

If you don't see the warning message, you should be fine.

Finally, an easy way to setup a ~/.Rprofile startup file is to do it
from within R, e.g.

> cat('options(prompt="R> ")\n', file = "~/.Rprofile")
> cat('set.seed(12345)\n', file = "~/.Rprofile", append = TRUE)

The '\n' at the end of each string represents a newline character, so
make sure you don't forget those.

Hope this help

Henrik



On Sat, Apr 15, 2017 at 1:10 PM, David Winsemius <dwinsemius at comcast.net> wrote:
>
>> On Apr 15, 2017, at 12:46 PM, Boris Steipe <boris.steipe at utoronto.ca> wrote:
>>
>> As with R, do with RStudio: Read The Beautiful Manual, and peruse The Google. For example, searching Google with the two (admittedly hard to guess) cryptograms:
>>  "RStudio Rprofile"
>>
>> will present more than a dozen most enlightening links to fulfil your desire.
>>
>> Perhaps the following link works better for you though:
>>  https://www.bing.com/search?q=rstudio+rprofile
>
> Another promising search strategy would be SO with "[rstudio]" in the tags:
>
> http://stackoverflow.com/search?q=%5Brstudio%5D+rprofile+windows
>
> --
> david.
>>
>> B.
>>
>>
>>> On Apr 15, 2017, at 3:14 PM, BR_email <br at dmstat1.com> wrote:
>>>
>>> Bill:
>>> Thanks for reply.
>>> Sorry, I do not understand it.
>>> For example, where do I put "file.path(getwd(), ".Rprofile")" ?
>>>
>>> Bruce
>>>
>>>
>>> William Dunlap wrote:
>>>> I think the site-specific R profile should be, using R syntax
>>>>   file.path(R.home("etc"), "Rprofile.site") # no dot before the capital R
>>>> The personal R profile will be
>>>>   file.path(Sys.getenv("HOME"), ".Rprofile") # there is a dot before capital R
>>>> but if a local R profile,
>>>>   file.path(getwd(), ".Rprofile") # there is a dot before capital R
>>>> exists it will be used and the one in HOME will not be.  (getwd() should
>>>> be the startup directory.)
>>>>
>>>>
>>>> Bill Dunlap
>>>> TIBCO Software
>>>> wdunlap tibco.com
>>>>
>>>>
>>>> On Sat, Apr 15, 2017 at 9:06 AM, BR_email <br at dmstat1.com> wrote:
>>>>> Hi R-helpers:
>>>>> Can you offer assistance in my getting .Rprofile and .Rprofile.site to run
>>>>> in RStudio?
>>>>> When I start RStudio nothing happens.
>>>>> I have put .Rprofile in [1] and [2], and .Rprofile.site in [2].
>>>>>
>>>>> Below, the info I believe you need to know.
>>>>> Thanks, in advance, for any help.
>>>>> Bruce
>>>>>
>>>>> The .Rprofile and .Rprofile.site are R-type files, which contain the two
>>>>> lines below.
>>>>> Also, I tried the profile files as text files.
>>>>> options(prompt="R> ")
>>>>> set.seed(12345)
>>>>>
>>>>>> Sys.getenv("HOME") [1] "C:/Users/BruceRatner/Documents"
>>>>>> Sys.getenv("R_HOME") [2] "C:/PROGRA~1/R/R-33~1.3"
>>>>>
>>>>>
>>>>> --
>>>>>
>>>>> ______________________________________________
>>>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>
>>>>
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
> David Winsemius
> Alameda, CA, USA
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list