Sourcing an R script from github, for global session use, from within a wrapper function?
Use:
eval(parse(text = script),envir=.GlobalEnv)
to stick the results into your default search space. Overwriting anything else with the same names, of course.
Source multiple r scripts from github
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:
- This will only download
.R
files. Can modify as needed. invisible
is used to suppress printing to console. However, addingecho=FALSE
orverbose=FALSE
should have suppressed the output but it doesn't.
Sourcing R files in a private github folder
TL;DR: It's possible, see the below code.
For anyone's future use, here's a method to source a R script from a private Github repo, using httr
, and optionally, devtools
.
I searched around enough to find various pieces to the solution, and just stitched them all together. So credit goes to multiple other threads and websites.
Some sources are here, here and here.
See below code:
library(httr)
# Source R script from Github
script <-
GET(
url = "https://api.github.com/repos/{user_name}/{repo_name}/contents/{script_name}.R",
authenticate({github_email}, {github_personal_access_token}), # Instead of PAT, could use password
accept("application/vnd.github.v3.raw")
) %>%
content(as = "text")
# Evaluate and parse to global environment
eval(parse(text = script))
This may only work for a private repo that you own. I'm not sure if it will work for a private repo that was shared with you.
See this link to create a Github Personal Access Token (PAT). You can also save this as an environment variable within R, if desired. devtools::github_pat()
can be useful here.
Both your email and PAT (or password) need to be in quotations.
Read a CSV from github into R
Try this:
library(RCurl)
x <- getURL("https://raw.github.com/aronlindberg/latent_growth_classes/master/LGC_data.csv")
y <- read.csv(text = x)
You have two problems:
- You're not linking to the "raw" text file, but Github's display version (visit the URL for https:\raw.github.com....csv to see the difference between the raw version and the display version).
- https is a problem for R in many cases, so you need to use a package like
RCurl
to get around it. In some cases (not with Github, though) you can simply replace https with http and things work out, so you can always try that out first, but I find using RCurl reliable and not too much extra typing.
How to transfer my files to R Projects, and then to GitHub?
I have broken my answer into three parts:
- The question in your title
- The reworded question in your text
- What I, based on your comments, believe you are actually asking
How to transfer my files to R Projects, and then to GitHub?
From RStudio, just create a new project and move your files to this folder. You can then initialize this folder with git using git init
.
How would [my included code] need to be executed inside a project?
You don't need to change anything in your example code. If you just place your files in a project folder they will run just fine.
An R project mainly takes care of the following for you:
- Working directory (it's always set to the project folder)
- File paths (all paths are relative to the project root folder)
- Settings (you can set project specific settings)
Further, many external packages are meant to work with projects, making many task easier for you. A project is also a very good starting point for sharing your code with Git.
What would be a good workflow for working with multiple scripts in an R project?
One common way of organizing multiple scripts is to make a new script calling the other scripts in order. Typically, I number the scripts so it's easy to see the order to call them. For example, here I would create 00_main.R
and include the code:
source("01_data.R")
source("02_data.R")
source("03_graph.R")
Note that I've renamed your scripts to make the order clear.
In your code, you do not need to save the data to pass it between the scripts. The above code would run just fine if you delete the save()
and load()
parts of your code. The objects created by the scripts would still be in your global environment, ready for the next script to use them.
If you do need to save your data, I would save it to a folder named data/
. The output from your plot I would probably save to outputs/
or plots/
.
When you get used to working with R, the next step to organize your code is probably to create a package instead of using only a project. You can find all the information you need in this book.
Rstudio unable to install 'psimetrica-R' package from Github
A github source repository is not an R package repository like CRAN -- so you cannot use install.packages()
.
Instead, use remotes::install_gihub()
as in
if (!requireNamespace("remotes", quietly=TRUE)) install.packages("remotes")
remotes::install_github("stmueller/psimetrica-R")
This will install remotes
if needed, and use it to install the desired package.
Or, at least, that would work in principle if psimetrica-R
was a regular R source package. Here it fails because of its layout:
> remotes::install_github("stmueller/psimetrica-R")
Error: Failed to install 'unknown package' from GitHub:
cannot open URL 'https://api.github.com/repos/stmueller/psimetrica-R/contents/DESCRIPTION?ref=HEAD'
>
So you could fork the repo and make it proper package first. At least open source lets you do that.
Related Topics
How to Determine If a Character Vector Is a Valid Numeric or Integer Vector
Adding Missing Dates to Dataframe
How to Run a Function Every Second
Knitr: Object Cannot Be Found When Converting Markdown File into HTML
Numbered Code Chunks in Rmarkdown
Highcharter Plotbands, Plotlines with Time Series Data
How to Use the Function Curve in [R] to Graph a Normal Curve
Can Transparency Be Used with Postscript/Eps
How to Round a Date to the Quarter Start/End
As.Posixct Gives an Unexpected Timezone
How to Averaging Over a Time Period by Hours
How to Replace the String Exactly Using Gsub()
How to Read the Files in a Directory in Sorted Order Using R