1

I usually use some personal functions during my r sessions, as:

s1<-"https://github.com/juanchiem/R-sources-Juan/blob/master/rend_aj.R?raw=TRUE" # rend_aj.R
s2<-"https://github.com/juanchiem/R-sources/blob/master/theme_juan.R?raw=TRUE" # theme_juan.R
s3<-"https://github.com/juanchiem/R-sources-Juan/blob/master/assumptions.R?raw=TRUE" # lm_assumptions.R

devtools::source_url(s1)
devtools::source_url(s2)
devtools::source_url(s3)

I wonder if Is it possible to create a function to source these 3 files from github directly by their file names (detailed after #) Thanks!

1
  • Why not get the exact raw file url and use wget/curl or any other tool(e.g m(M)apping the devtools call)? Commented Feb 17, 2020 at 12:41

1 Answer 1

2

We could write up a custom function(see note below) to achieve the goal:

custom_source <- function(repo_name,repo_branch,name,
                          ...){

  url_to_use <- paste0("https://github.com/",repo_name,
                        "/blob/",repo_branch,"/",name,".R",
                       "?raw=TRUE"
                       )
  devtools::source_url(url_to_use,...)
}

Calling the function(you could include this in the same function above but I prefer calling it individually)

invisible(Map(function(x) custom_source("juanchiem/R-sources-Juan",
               "master",x), c("rend_aj","theme_juan", "lm_assumptions")))

Result(truncated):

  SHA-1 hash of file is 586d156021371098ec51c35c4c056d7c98a94d3d
SHA-1 hash of file is 5c84c1e8e7bd4fe9bf6cd26c4f5e955c20cf851b
SHA-1 hash of file is 63790352a1dc712611bbd26a67cd33d2d1ce5b2c

NOTE:

  1. This will only download .R files. Can modify as needed.
  2. invisible is used to suppress printing to console. However, adding echo=FALSE or verbose=FALSE should have suppressed the output but it doesn't.
Sign up to request clarification or add additional context in comments.

4 Comments

Do you notice that when you call the three files at once, all the code functions are printed in the console, and not when calling them individually..
Yes, I just experimented with merely using source instead of source_url and it works(devtools uses base source but also provides the SHA code). Add echo=FALSE to turn it off.
sorry, where should I inlcude echo=FALSE
I have edited. Please see note 2. I think it might be an issue with Map's print method overriding that of source(purrr's map also prints).

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.