0

It seems that when I call littler from the command line, it will source ~/.Rprofile. Is there a way to prevent it from sourcing ~/.Rprofile?

user1424739
  • 11,937
  • 17
  • 63
  • 152

1 Answers1

1

It goes both ways---that we are now reading ~/.Rprofile is in large part due to users who wanted this feature, as opposed to you not wanting it :)

But there is a (simple and easy) fix: use interactive(). Witness:

edd@rob:~$ r -e 'print(interactive())'
[1] FALSE
edd@rob:~$ r -i -e 'print(interactive())'

Please do not apply R like a magic answers box, because you can mislead
others and cause harm.
   -- Jeff Newmiller (about how much statistical knowledge is needed 
      for using R)
      R-help (May 2016)

[1] TRUE
edd@rob:~$ 

So what happened here? First, we tested interactive(). It came back FALSE. This is the default. Nothing happened.

Second, I added the -i switch to enfore interactive mode. It printed TRUE, but more. Why?

Well my ~/.Rprofile in essence looks like this

   ## header with a few constant settings, mostly to options()

   ## TZ setting and related

   local({     # start of large block, see Rprofile.site

   if (interactive()) {
      if (requireNamespace("fortunes", quietly=TRUE)) {
         print(fortunes::fortune()) 

         #more stuff

      }

   })

and that governs my interactive R sessions on the console, in Emacs/ESS, in RStudio, and my non-interactive r calls from, say, crontab.

So in short: yes, it is always read. But yes, you can also skip parts you do not want executed.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • Thanks. Why is `local` needed here? – user1424739 Apr 14 '20 at 01:05
  • It's an R trick I originally learned a long time ago from that very context (see `help(Startup)`). The main upside, as I recall it, is that no assigned symbols linger in the environment because you provide a fully scoped curly brazes environment, and the call that it may need to be properly enclosed. – Dirk Eddelbuettel Apr 14 '20 at 01:10
  • 3
    By the way, it looks like you never (or very rarely) accept answers. That is ... somewhat against the spirit of stackoverflow. Maybe you can reconsider this practice. – Dirk Eddelbuettel Apr 14 '20 at 01:11
  • Well there is no (enforced) "must" or "need", but it is common practice as it helps to signal "better" answers. If you click on your own profile, you see the questions you asked, and the ones with/without accepted answers are highlighted. More formally you can probably construct a search query to return the exact set of questions i) by you that are ii) without accepted answers. And thanks for accepting my answer here. – Dirk Eddelbuettel Apr 14 '20 at 02:49