Title: | Miscellaneous Tools for Reproducible Research |
---|---|
Description: | Tools to load 'R' packages and automatically generate BibTeX files citing them as well as load and cache plain-text and 'Excel' formatted data stored on 'GitHub', and from other sources. |
Authors: | Christopher Gandrud [aut, cre] |
Maintainer: | Christopher Gandrud <[email protected]> |
License: | GPL (>= 3) |
Version: | 0.5 |
Built: | 2024-10-31 16:32:40 UTC |
Source: | https://github.com/christophergandrud/repmis |
The function returns the latest git commit and branch for the repo specified
in repo
or the current working directory if unspecified.
Git is needed for the command to run.
The functions makes it possible to include the latest git commit and branch
in a run to be able to know exactly which code where used.
git_stamp(repo = getwd())
git_stamp(repo = getwd())
repo |
Git repo directory. If unspecified, then the current working directory is used. |
character vector with latest commit and branch
InstallOldPackages
installs specific R package versions.
InstallOldPackages(pkgs, versions, oldRepos = "http://cran.r-project.org", lib = NULL)
InstallOldPackages(pkgs, versions, oldRepos = "http://cran.r-project.org", lib = NULL)
pkgs |
character vector of package names to install. |
versions |
character vector of package version numbers. to install. The
order must match the order of package names in |
oldRepos |
character name of repository to download the packages old
package versions from. Default is
|
lib |
character vector giving the library directories where to install
the packages. Recycled as needed. If |
Installs specific R package versions.
install.packages
and download.file
## Not run: # Install old versions of the e1071 and gtools packages. Names <- c("e1071", "gtools") Vers <- c("1.6", "2.6.1") InstallOldPackages(pkgs = Names, versions = Vers) ## End(Not run)
## Not run: # Install old versions of the e1071 and gtools packages. Names <- c("e1071", "gtools") Vers <- c("1.6", "2.6.1") InstallOldPackages(pkgs = Names, versions = Vers) ## End(Not run)
LoadandCite
can install and load R packages as well as automatically
generate a BibTeX file citing the packages.
LoadandCite(pkgs = NULL, versions = NULL, Rversion = NULL, bibtex = TRUE, style = "plain", tweak = TRUE, install = FALSE, file = NULL, repos = NULL, lib = NULL)
LoadandCite(pkgs = NULL, versions = NULL, Rversion = NULL, bibtex = TRUE, style = "plain", tweak = TRUE, install = FALSE, file = NULL, repos = NULL, lib = NULL)
pkgs |
a character vector of R package names. If |
versions |
character vector of package version numbers to install. Only
works if |
Rversion |
a character string specifying a particular R version. If the
version of R currently running differs from |
bibtex |
logical. If |
style |
character string indicating stylistic elements to add to the
citations. Currently supports |
tweak |
logical. Whether to fix some known problems in the citations, especially non-standard format of authors. |
install |
a logical option for whether or not to install the packages.
The default is |
file |
the name of the BibTeX file you want to create. If
|
repos |
character vector specifying which repository to download
packages from. Only relevant if |
lib |
character vector giving the library directories where to install
the packages. Recycled as needed. If |
The command can install R packages, load them, and create a BibTeX
file that can be used to cite the packages in a LaTeX or similar document. It
can be useful to place this command in a knitr
code chunk at the
beginning of a reproducible research document. Note: the command will
overwrite existing files with the same name as file
, so it is
generally a good idea to create a new BibTeX file with LoadandCite
.
Gandrud, Christopher (2013). Automating R Package Citations in Reproducible
Research Documents. SSRN.
This function is partially based on: https://gist.github.com/3710171.
It also builds on code from knitr's write_bib
. See: Y. Xie. knitr: A
general-purpose package for dynamic report generation in R, 2013. URL
http://CRAN.R-project.org/package=knitr. R package version 1.5. Note
that it does not formally depend on knitr so that knitr can be included in
LoadandCite
so that it is possible to install old versions of that
package.
write_bib
, install.packages
, and
library
# Create vector of package names ## In this example you need to have the packages installed aready. PackNames <- "repmis" # Load the packages and create a BibTeX file LoadandCite(pkgs = PackNames, file = 'PackageCites.bib', style = 'JSS') ## Not run: # Install, load, and cite specific package versions # dontrun due to CRAN restrictions Names <- c("e1071", "gtools") Vers <- c("1.6", "2.6.1") LoadandCite(pkgs = Names, versions = Vers, install = TRUE, file = "PackageCites.bib") ## End(Not run)
# Create vector of package names ## In this example you need to have the packages installed aready. PackNames <- "repmis" # Load the packages and create a BibTeX file LoadandCite(pkgs = PackNames, file = 'PackageCites.bib', style = 'JSS') ## Not run: # Install, load, and cite specific package versions # dontrun due to CRAN restrictions Names <- c("e1071", "gtools") Vers <- c("1.6", "2.6.1") LoadandCite(pkgs = Names, versions = Vers, install = TRUE, file = "PackageCites.bib") ## End(Not run)
Read a character text file from a secure (https) site into R as a single object.
scan_https(url, sha1 = NULL)
scan_https(url, sha1 = NULL)
url |
The files's URL. |
sha1 |
Character string of the file's SHA-1 hash, generated by
|
a charcter object of length 1
Originally based on source_url from the Hadley Wickham's devtools package.
Sets valid working directory from vector of possible directories
set_valid_wd(possible)
set_valid_wd(possible)
possible |
character vector of possible working directores |
Sets the working directory to the first valid directory from a list of possible directories.
## Not run: set_valid_wd(c('examples/directory1', 'anotherExample/directory2')) ## End(Not run)
## Not run: set_valid_wd(c('examples/directory1', 'anotherExample/directory2')) ## End(Not run)
source_data
loads plain-text or RDATA formatted data stored at a URL
(both http and https) into R.
source_data(url, rdata, sha1 = NULL, cache = FALSE, clearCache = FALSE, sep = "auto", header = "auto", stringsAsFactors = FALSE, envir = parent.frame(), ...)
source_data(url, rdata, sha1 = NULL, cache = FALSE, clearCache = FALSE, sep = "auto", header = "auto", stringsAsFactors = FALSE, envir = parent.frame(), ...)
url |
The data's URL. To distinguish between plain-text and RDATA the
|
rdata |
logical. Whether or not the data set is an |
sha1 |
Character string of the file's SHA-1 hash, generated by
|
cache |
logical. Whether or not to cache the data so that it is not downloaded every time the function is called. |
clearCache |
logical. Whether or not to clear the downloaded data from the cache. |
sep |
The separator method for the plain-text data. For example, to load
comma-separated values data (CSV) use |
header |
Logical, whether or not the first line of the file is the header (i.e. variable names). |
stringsAsFactors |
logical. Convert all character columns to factors? |
envir |
the environment where the data should be loaded. |
... |
Loads plain-text data (e.g. CSV, TSV) or RDATA from a URL. Works with
both HTTP and HTTPS sites. Note: the URL you give for the url
argument
must be for the RAW version of the file. The function should work to download
plain-text data from any secure URL (https), though I have not verified this.
From the source_url
documentation: "If a SHA-1 hash is specified with
the sha1
argument, then this function will check the SHA-1 hash of the
downloaded file to make sure it matches the expected value, and throw an error
if it does not match. If the SHA-1 hash is not specified, it will print a
message displaying the hash of the downloaded file. The purpose of this is to
improve security when running remotely-hosted code; if you have a hash of
the file, you can be sure that it has not changed."
a data frame
Originally based on source_url from the Hadley Wickham's devtools package.
## Not run: # Download electoral disproportionality data stored on GitHub # Note: Using shortened URL created by bitly DisData <- source_data("http://bit.ly/156oQ7a") # Check to see if SHA-1 hash matches downloaded file DisDataHash <- source_data("http://bit.ly/Ss6zDO", sha1 = "dc8110d6dff32f682bd2f2fdbacb89e37b94f95d") ## End(Not run)
## Not run: # Download electoral disproportionality data stored on GitHub # Note: Using shortened URL created by bitly DisData <- source_data("http://bit.ly/156oQ7a") # Check to see if SHA-1 hash matches downloaded file DisDataHash <- source_data("http://bit.ly/Ss6zDO", sha1 = "dc8110d6dff32f682bd2f2fdbacb89e37b94f95d") ## End(Not run)
source_XlsxData
loads Excel data stored at a URL (both http
and https) into R.
source_XlsxData(url, sheet, sha1 = NULL, cache = FALSE, clearCache = FALSE, ...)
source_XlsxData(url, sheet, sha1 = NULL, cache = FALSE, clearCache = FALSE, ...)
url |
character string of the Excel files's URL. |
sheet |
character string of number of representing the sheet in the workbook to return. Only one sheet at a time can currently be returned |
sha1 |
Character string of the file's SHA-1 hash, generated by
|
cache |
logical. Whether or not to cache the data so that it is not downloaded every time the function is called. |
clearCache |
logical. Whether or not to clear the downloaded data from the cache. |
... |
arguments to pass to |
a data frame
read.xlsx
, httr, source_data