Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Composing functions
- DRY (Don't repeat yourself!). If writing essentially the same code more than once, make a function.
- Functions should be designed to be simple, to always return the same outputs when given the same
- inputs, and to avoid the use of side effects (having effects in addition to returning an output)
- ## Bad
- ```
- #t_output_IT.R
- data <- read.data(...)
- filter <- "IT"
- if (filter == "IT"){
- filtered_data <- data %>% filter(...)
- }else if(filter == "SE"){
- filtered_data <- data %>% filter(...)
- }
- run_analysis(filtered_data)
- #t_output_SE.R
- data <- read.data(...)
- filter <- "SE"
- if (filter == "IT"){
- filtered_data <- data %>% filter(...)
- }else if(filter == "SE"){
- filtered_data <- data %>% filter(...)
- }
- run_analysis(filtered_data)
- ```
- ## Good
- ```
- #t_output.R
- data <- read.data(...)
- filter_data <- function(datain, filter){
- if (filter == "IT"){
- return(datain %>% filter(...))
- }
- if(filter == "SE"){
- return(datain %>% filter(...))
- }
- }
- t_output <- function(datain, filter){
- filtered_data <- filter_data(datain, filter)
- run_analysis(filtered_data)
- }
- t_output(data, "SE")
- t_output(data, "IT")
- ```
- Note that in this example I could probably take my filter_data function and use it in multiple
- different files now, while before I would need to copy and paste it everywhere!
- # Code style
- Truncate all lines at 80 characters (In RStudio: Tools - Global Options - Code - Display - Show Margin (Margin column = 80))
- Make blank lines between code chunks (eg, between functions, etc) for better readability
- Use comments to say WHY not WHAT.
- ## Naming functions/objects
- Try not to name things like df1, df2, temp1, temp2, etc. Take time to name things more properly (not easy!)
- Suggestion: for functions, use verb_noun construction, e.g. derive_baseline, get_data
- Avoid using "." in R names, as S3 methods make use of the ".", so this can confuse.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement