Sourcing R script over HTTPS
Yes you can, try running this R tutorial:
source("http://www.mayin.org/ajayshah/KB/R/tutorial.R")
(Source)
Https is only supported on Windows, when R is started with the --internet2
command line option (see FAQ):
> source("https://pastebin.com/raw.php?i=zdBYP5Ft")
> test()
[1] "passed"
Without this option, or on linux, you will get the error "unsupported URL scheme". In that case resort to the solution suggested by @ulidtko, or:
Here is a way to do it using RCurl
, which also supports https:
library(RCurl)
eval( expr =
parse( text = getURL("http://www.mayin.org/ajayshah/KB/R/tutorial.R",
ssl.verifypeer=FALSE) ))
(You can remove the ssl.verifypeer
if the ssl certificate is valid)
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.
R: source() and path to source files
If you are distributing a script to colleagues, you should really not be writing a script that sources other scripts. What if you want to rename or move functions.R
in the future? What if you need to modify a function in functions.R
, but wrapper.R
relies on the older version of that function? It's a flimsy solution that will cause headache. I would recommend either of the following instead.
Put everything needed into a single, self-contained script and distribute that.
If you really want to separate code into different files, write a package. Might sound like overkill, but packages can actually be very simple and lightweight. In the simplest form a package is just a directory with a
DESCRIPTION
andNAMESPACE
file along with anR/
directory. Hadley breaks this down nicely: https://r-pkgs.org/whole-game.html.
Sourcing R script stored in AWS S3
Use cloudyr::s3source()
.
From the README:
s3source()
sources an R script directly from S3
In your case, something like this...
s3source(object = "rscript.R", bucket = get_bucket('[bucket name]'), echo = TRUE)
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.
How to source file.R without output
It's not the best sounding solution, but If you might be running this script often like this, you could declare a boolean whether graphics are required (graphics_required=TRUE) and then enclose all your plot commands in if/then clauses based on your boolean, then just change the boolean to change the behaviour of the script
Error in sourcing functions/data from script that is sourced from another script in a different directory
I dug into documention on the source command and changed my code accordingly. This works (added chdir=TRUE from script1.R and removed setwd() from script2.R):
script1.R
source('dir1/script2.R'
local=TRUE,
echo=TRUE,
spaced=TRUE,
chdir=TRUE)
script2.R
source('script3.R')
source('dir2/data.csv')
https://www.rdocumentation.org/packages/base/versions/3.6.1/topics/source
Related Topics
Pattern Matching Using a Wildcard
Ggplot2 Multiple Scales/Legends Per Aesthetic, Revisited
Multiple Time Series in One Plot
Different Legend-Keys Inside Same Legend in Ggplot2
Create Counter of Consecutive Runs of a Certain Value
Find Neighbouring Elements of a Matrix in R
Breaking Loop When "Warnings()" Appear in R
When Should I Use Setdt() Instead of Data.Table() to Create a Data.Table
How to Add a Index by Set of Data When Using Rbindlist
Venn Diagram Proportional and Color Shading with Semi-Transparency
Smaller Gap Between Two Legends in One Plot (E.G. Color and Size Scale)
Modify X-Axis Labels in Each Facet
Plotly: Updating Data with Dropdown Selection
Adding New Column with Diff() Function When There Is One Less Row in R