Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I am writing an R code that has many different functions that eventually I will want to use all together on different data sets.
As I keep building functions it seems to be getting harder to keep track of everything in my script.
My question is, is it proper R coding to break functions into separate R Scripts or should it all be in one massive script?
Thank you for your help. This is my first time trying to code something this large!
-B
Yes, you can store your functions in multiple R scripts.
If you need to call them, you can use source().
For eg:
Say you have func1 , func2 saved in myfunc.R.
To call them,
source('myfunc.R')
#other codes
func1()
func2()
As to whether this approach is recommended, depends on your project requirements.
Alternatively, you can consider packaging them as recommended by Richard.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I built my own function and each time I need to run this function, I need to do that manulally. Is there any way that I can load it automatically as any R-package? Or can I build an R-package used only by me?
Would recommend Nate Days solution, but you could also use Rs save() and load()functions to do this. It works on all R-objects and stores them in a binay .rda-file. You can store multiple objects as well.
Try:
add <- function(x, y){return(x+y)}
save(add, file = 'add_function.rda')
Whenever you need your function, do:
load('add_function.rda')
And add() would be available in the parent environment.
There is a package called pkgmaker on CRAN that has a ton of tools and utilities for you to create your own packages. As an alternate option you might consider creating a functions.R script for you to store all of your personally created and often used functions. You can add the line source('functions.R', local=TRUE) to your programs, scripts or apps and your functions will be accessible to you. Thats how I handle the issue anyway. Cheers
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I am developing a set of R scripts to post-process experimental data.
Currently, these are just scripts that I load individually by manually sourcing them.
I wonder if it would be better to put all those scripts in an R package, but as I constantly add/modify my library functions, I don't really know if this is the recommended way of doing it.
So my question is: should I go on working on "independent" script files or package them?
It is always good to bundle your scripts into a package. which will help you in lot many ways.
All your project fundamentals will be packed together
code portability will be lot easier
you can have test cases for your function using testthat package
You can have proper documentation for your code using roxygen2 package, which will help the readers or other users of the code to understand mode about the code.
you don't have to load your data and functions manually, you just have to load your package by saying library(package_name)
for more details about "R package": http://r-pkgs.had.co.nz/
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm considering using R in my work. Primarily I am interested in ggplot2, dplyr and tidyr, probably using the RStudio environment.
I deal with a lot of sensitive educational data, such as names, dates of birth etc and could only only use R at work if I knew the data were safe.
Will my data be kept private/confidential or is it shared in any way?
By default, data or code is not shared to the outside world. This may not hold for cases including but not confined to:
when you are using (online) version control
if you are pushing a report to RPubs repository
using R on a server, which may be accessible to third parties
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I have planned to start my R program carrier now. I have understand the usage R but i have some doubts on the procedure to be implement in R. As a startup i am planing to retrieve data from google analytics and show it to frontend using R shiny. So which will be the good flow to fetch the data. Is it good to fetch the google analytic data using R and process it or it will be good to fetch the data using other languages like php or Java. Also R is good to fetch the data from database or its a good practice to fetch the data using other commonly used languages and process using R.
Sorry the question is little bit descriptive but i am expecting a help from you guys
Thanks,
Organize it in a package(http://r-pkgs.had.co.nz/).
See https://developers.google.com/analytics/solutions/r-google-analytics?hl=en .
The database question depends on exactly what you are using, but R has a lot of resources.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Can you please provide a specific situation illustrating when a for loop might work more effectively than the more commonly cited apply suite of solutions?
If the results of the previous computation are used in the next computation, it is appropriate to use a for loop, since this behavior is difficult to replicate with lapply (you would have to use something like Reduce). R is not necessarily slow with for loops, merely with memory allocation (which is easy to get wrong with for loops). See Chapter 2 of the R Inferno.