Source user defined functions in R - r

I have created a script with a few custom functions and have saved the file in one of the folders.
However, if i want to access the functions using source("Functions.R") i have to have the "Functions.R" file in my working directory. Is there any way of fetching the file without copying it to my current working directory? (I do not want to create a package for it)
Thanks!

Related

Where should I store private files in an R package?

I'm developing an R package with an XLSX file that serves as a template for a function that exports a report.
I currently store it under inst/ but I don't think this is best as the user should have no access to that file.
I know I could store it privately as an object, but I'd like to know if it is possible to store it privately as a file that's loaded when the package user executes their code. This would make the file more easily update-able and the current codebase would require less restructuring.

Using R, is it possible to dyn.load() a shared object library (.so) file directly from a URL?

I'm trying to figure out if it's possible to use the R dyn.load() function to load a shared object library (.so) file directly from a URL, instead of first having to download it into your R working directory.
The R environment that I'm working with runs on an AWS lambda server, and as a consequence I can only read, not write to or create, files in the working directory.
Any help would be much appreciated!
(.so file URL: http://cloud.sowiso.nl/images/uploads/mvtnorm.so)

R - how to load each file from folder

I have a folder and that contains a lot of R files, that files are functions actually. What I need is to create code in another project in R which will load each file in that folder and load this functions in to the environment.
I know that better option is to create a R package from this functions but it canĀ“t be done for several reasons in my case.
What is the simplest way to achieve my goal?

Load Folder of Scripts in R at startup?

I'm new to R and frankly the amount of documentation is overwhelming, and I haven't been able to find the answer to this question.
I have created a number of .R script files, all stored in a folder that I can access on my server (let's say the folder is, using the Windows backslash character \\servername\Paige\myscripts)
I know that in R you can call each script individually, for example (using the forward slash required in R)
source(file="//servername/Paige/myscripts/con_mdb.r")
and now this script, con_mdb, is available for use.
If I want to make all the scripts in this folder available at startup, how do I do this?
Briefly:
Use your ~/.Rprofile in the directory found via Sys.getenv("HOME") (or if that fails, in R's own Rprofile.site)
Loop over the contents of the directory via dir() or list.files().
Source each file.
as eg via this one liner
sapply(dir("//servername/Paige/myscripts/", "*.r"), source)
but the real story is that you should not do this. Create a package instead, and load that. Bazillion other questions here on how to build a package. Research it -- it is worth it.
Far the best way is to create a package! But as first step, you could also create one r script file (collection.r) in your script directory which includes all the scripts in a relative manner.
In your separate project scripts you can than include only that script with
source(file="//servername/Paige/myscripts/collection.r", chdir = TRUE)
which changes the directory before sourcing. Therefore you would have only to include one file for each project.
In the collection file you could use a loop over all files (except collection.r) or simply list them all.

csv files in opencpu

If I put a very small csv file in my GitHub directory so that it gets copied to /ocpu/github/username/projectname/www/ , will I be able to access the contents of the csv for use in a R function? I tried to ajax the file, but I get a 404 error even though I can see the csv file sitting in the www directory of my local server. I need to have the csv on the server as a static file rather than being uploaded by a function. Thanks
You should be able to access them like any other file. Can you post an example that shows what you are doing and what error you are getting?
That said, if you just want to use this data in your R functions, it is better to include it in the R package as an actual data file. Also see section 1.1.6 of Writing R Extensions. An example is the mapapp package, which includes a dataset called countryExData. Also see the live app.

Resources