I know you can work with Google Sheets data in R by importing published spreadsheets. But what if I want to do something different? I have some R functions that I wrote and stored in a Google Doc. I want to be able to load these functions directly into R using something like the source() command.
Here’s what I’m trying to accomplish:
- Write my R functions and save them in a Google Document
- Make the document publicly accessible so no authentication is needed
- Use a command in R to load these functions from the Google Doc
This would be really helpful when I’m working on different computers and need quick access to my custom functions without having to copy files around.
Here’s an example of the type of function I want to store:
calc_stats <- function(data_vec, use_median=FALSE, show_output=FALSE) {
if (use_median) {
middle_val <- median(data_vec)
spread_val <- mad(data_vec)
} else {
middle_val <- mean(data_vec)
spread_val <- sd(data_vec)
}
if (show_output) {
if (use_median) {
print(paste("Median:", middle_val, "MAD:", spread_val))
} else {
print(paste("Mean:", middle_val, "SD:", spread_val))
}
}
return(list(center=middle_val, spread=spread_val))
}
Is there a way to make this work? Any suggestions would be appreciated.