15 Software

15.1 Latest versions

Make sure your packages are up to date

update.packages()

After reinstalling, you can update all packages stored in all .libPaths() with the following command

install.packages(old.packages(checkBuilt=T)[,"Package"])

To find broken packages after an update

library(purrr)

set_names(.libPaths()) %>%
  map(function(lib) {
    .packages(all.available = TRUE, lib.loc = lib) %>%
        keep(function(pkg) {
            f <- system.file('Meta', 'package.rds', package = pkg, lib.loc = lib)
            tryCatch({readRDS(f); FALSE}, error = function(e) TRUE)
        })
  })
## https://stackoverflow.com/questions/31935516/installing-r-packages-error-in-readrdsfile-error-reading-from-connection/55997765

To remove packages duplicated in multiple libraries

## Libraries
i <- installed.packages()
libs <- .libPaths()
## Find Duplicated Packages
i1 <- i[ i[,'LibPath']==libs[1], ]
i2 <- i[ i[,'LibPath']==libs[2], ]
dups <- i2[,'Package'] %in% i1[,'Package']
all( dups )
## Remove
remove.packages(  i2[,'Package'], libs[2] )

15.2 General Workflow

If you want to go further down the reproducibility route (recommended, but not required for our class), consider making your entire workflow use Free Open Source Software

Linux: An alternative to windows and mac operating systems. Used in computing clusters, big labs, and phones. E.g., Ubuntu and Fedora are popular brands

On Fedora, you can open RStudio on the commandline with

rstudio

Alternatively, you are encouraged to try using R without a GUI. E.g., on Fedora, this document can be created directly via

Rscript -e "rmarkdown::render('RMarkown.Rmd')"

Latex: An alternative to Microsoft Word. Great for writing many equations and typesetting. Easy to integrate Figures, Tables, and References. Steep learning curve.

To begin programming, see

15.3 Sweave

Sweave: is an alternative to Rmarkdown for integrating latex and R. While Rmarkdown “writes R and latex within markdown”, Sweave “write R in latex”. Sweave files end in “.Rnw” and can be called within R

Sweave('Sweave_file.Rnw')

or directly from the command line

R CMD Sweave Sweave_file.Rnw 

In both cases, a latex file Sweave_file.tex is produced, which can then be converted to Sweave_file.pdf.

For more on Sweave,

Knitr: You can also produce a pdf from an .Rnw file via knitr

Rscript -e "knitr::Sweave2knitr('Sweave_file.Rnw')"
Rscript -e "knitr::knit2pdf('Sweave_file-knitr.Rnw')"

For background on knitr

15.4 Stata

For those transitioning from Stata or replicating others’ Stata work, you can work with Stata data and code within R.

Translations of common procedures is provided by https://stata2r.github.io/. See also the textbook “R for Stata Users” by Robert A. Muenchen and Joseph M. Hilbe.

Many packages allows you to read data created by different programs. As of right now, haven is a particularly useful for reading in Stata files

library(haven)
read_dta()
## See also foreign::read.dta

You can also execute stata commands directly in R via package Rstata. (Last time I checked, Rstata requires you to have purchased a non-student version of Stata.) Moreover, you can include stata in the markdown reports via package Statamarkdown:

library(Rstata)
library(Statamarkdown)

There are many R packages to replicate or otherwise directly copy what Stata does. For example, see the margins package https://cran.r-project.org/web/packages/margins/vignettes/Introduction.html

For more information on R and Stata, see

You can also use other software (such as Python) within R. You can also use R within Stata, or both within Python. With R, you can easily import many different data types