How to permanently "source" an R script - r

I am using an R script that someone sent me. This is not a big package but just one function. To use it, I source the file.
However,whenever I restart R, I must type in source("directory") again to use the function.
Is there any way I can avoid this and set that function permanently?

I think that you just want to add source("directory") to your .Rprofile so that the function gets loaded at startup.
See this SO question for some handy things you can do with .Rprofile.

Related

How do I automatically have some code run everything I open up RStudio?

So for example, I want to set my global options as such:
options(stringsAsFactors = FALSE)
Sys.setenv(JAVA_HOME="C:/Program Files/Java/jre1.8.0_171")
for every RStudio session.
How can I write my code so that they are run at the beginning of each RStudio session?
Options
You can add the options script to your .Rprofile.
One of the easiest ways to access this is through the usethis library, specifically:
usethis::edit_r_profile()
The .Rproflie is always run at the start of a new session unless specifically told otherwise.
However, I only give you this with a MAJOR warning - adding code into your .Rprofile will prevent your R code from being reproducible. For this reason, I would strongly recommend you set the options call in a snippet in RStudio instead of using the .Rprofile, allowing for a keyboard shortcut to easily add to any script you run. While perhaps less convenient, I believe it's well worth the trade-off to keep your code fully reproducible. You can find more info on snippets with this support article from RStudio.
Envars
The Sys.setenv call would probably be suited well for using a .Renviron file.
Again, easily added with:
usethis::edit_r_environ()
Here is a nice reference to better explain the full use of .Rprofile and .Renviron files: https://cfss.uchicago.edu/notes/r-startup/

RDCOMClient log file

I have been using RDCOMClient for a while now to interact with vendor software. For the most part it has worked fine. Recently, however, I have the need to loop through many operations (several hundred). I am running into problems with the RDCOM.err file growing to a very large size (easily GBs). This file is put in C: with no apparent option to change that. Is there some way that I can suppress this output or specify another location for the file to go? I don't need any of the output in the file so suppressing it would be best.
EDIT: I tried to add to my script a file.remove but R has the file locked. The only way I can get the lock released is to restart R.
Thanks.
Setting the permissions to read only was going to be my suggested hack.
A slightly more elegant approach is to edit one line of the C code in the package in src/RUtils.h from
\#define errorLog(a,...) fprintf(getErrorFILE(), a, ##__VA_ARGS__); fflush(getErrorFILE());
to
\#define errorLog(a, ...) {}
However, I've pushed some simple updates to the package on github that add a writeErrors() function that one can use to toggle whether errors are written or not. So this allows this to be turned on and off dynamically.
So
library(RDCOMClient)
writeErrors(FALSE)
will turn off the error logging to the file.
I found a work around for this. I created the files C:\RDCOM.err and C:\RDCOM_server.err and marked them both as read-only. I am not sure if there is a better way to accomplish this, but for now I am running without logging.

How to run code when opening Rstudio? [duplicate]

I wrote an R function that updates the version number of a package in another question. I work a lot with GitHub and RStudio, and it would safe me quite some time (plus be much more precise) if this function was automatically run every time I opened a certain project (or better yet, make a git commit/push, but I assume that is harder to do). But I don't know how to do this or if this is even possible.
I could use .Rprofile to run R codes every time I start R, so I could just update versions whenever I start R (or build in that it only updates the version if the date is not today or something) but that seems overdoing it.
You can make a separate .Rprofile for each project. You have to put it in the main directory of the project (http://www.rstudio.com/ide/docs/using/projects).
Well I would use .Rprofile for that. There is something to be said for being independent of the tool chain around you: knitr works from RStudio as well as without it, dito for Rcpp/RInside etc pp.
You can hook into commit hooks for svn, both explicitly via hooks in the back end, or simply at your by end adding wrapper scripts. I presume you can do likewise with git but I simply know much less about it. So to abstract this away, I would write myself a 'commitThis' or 'pushThis' or ... function that does the number increment, test run, code push and what have you.
If your code needs RStudio to be already running (e.g. because it's relying on some rstudioapi:: function), putting it directly in .Rprofile won't work (.Rprofile is executed before RStudio is available).
Instead, you could set a hook for "rstudio.sessionInit":
setHook(
hookName = "rstudio.sessionInit",
action = function(newSession) {
if (newSession) {
# your code goes here
},
action = "append"
)

"Hide" functions in RStudio

I am in the painful process of transitioning from MATLAB to R, and still coming to terms with not having a neatly arranged MathWorks website to consult.
When writing MATLAB functions, they are stored in a local drive and can be accessed in my source code (as long as the function is in the active directory).
When writing a function in R, I need to "run" it, so it is stored in the global environment, then I can use it. Surely there is a 'nicer' way of doing this, as I will need to refer to many, many functions. Can I seemingly "hide" them so I don't have to see them, but always know they exist?
Thanks in advance
source('F:\\RWorkingDirectory\\my_functions.r') or you create your own R package which is very easy to do with Rstudio.
Thanks for the suggestions. I have decided to set up an environment instead.
E.g.,
Set up R script with my desired function(s) called MainFunctions.R
Add to .Rprofile:
e <- new.env()
source("MainFunctions.R",local=e)
attach(e)
Now I need to simply edit this file, and not worry about having to load them in, or create a package.

How to keep modified/downloaded package

R noobie here.
I'm am trying to use a package that I download off of github using source_gist, but it appears that I need to re-download it every time I quit R (I'm using RStudio).
To clarify, the function that I'm using is part of plotrix package and is called barp. Someone made a modified version of it (called barp2) and put it up on github. That's what I want to use.
So my question is this: is there anyway to have this modified code saved inside the plotrix package, so I wouldn't have to download it every time?
I hope I'm explaining this correctly.
So, let's get some quick terminology straight: the function you're getting off of github isn't a package, it's just a single function. If it was a package, you could use devtools::install_github once and then load it with require() or library() like any other package.
A good solution isn't too different. Just go to the gist, copy the code, paste it into your R editor, and save it somewhere as a .R script file. Something like C:/path/to/barp2.R (adjusting, of course, based on where you actually want to keep it and based on your OS). Then you can read it locally using source("C:/path/to/barp2.R") instead of devtools::source_gist().
If you always want to load it, you could load plotrix and then source this file every time R starts with a couple lines in your R profile, see ?Startup as #BondedDust suggests for details on this.
Reading it off of github every time does have the advantage that, if the author fixes bugs or otherwise improves it, you'll always be using the up-to-date version. It has several disadvantages too: requiring an internet connection, losing access if the gist is deleted, or being unable to access old versions if the author changes it in a way you don't like. Keeping a copy of a version you like is a smart move.

Resources