0

This may be a silly question as I am new to R. I wrote a function that sums the square of two inputs. but I can't figure out how to directly use the function every time I open R without first running the function's script. Any help is appreciated.

sum.of.squares <- function(x,y) {
  x^2 + y^2
}
1

1 Answer 1

1

You can do a couple things:

  1. Include this function in your Rprofile file (See here)
  2. Save an image of your environment as .RData (See here). Whenever you start an R session from that directory, the .Rdata file with the function will be loaded.
Sign up to request clarification or add additional context in comments.

2 Comments

Can you explain? I often have helper functions saved as part of my R environment that I don't want to be loaded globally between different projects (that all use the same R binary).
Storing stuff in your environment means it's hard to see what's there (outside of R) and hard to document. Better to put helper functions into .R files and source() them -- or write a package, once they get complicated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.