18 Software


The current version of R (and any packages) used to make this document are

Code
sessionInfo()
## R version 4.5.0 (2025-04-11)
## Platform: x86_64-redhat-linux-gnu
## Running under: Fedora Linux 42 (Workstation Edition)
## 
## Matrix products: default
## BLAS/LAPACK: FlexiBLAS OPENBLAS-OPENMP;  LAPACK version 3.12.0
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
## 
## time zone: Europe/Berlin
## tzcode source: system (glibc)
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  compiler  methods  
## [8] base     
## 
## other attached packages:
## [1] colorout_1.3-2
## 
## loaded via a namespace (and not attached):
##  [1] Matrix_1.7-3         jsonlite_2.0.0       Rcpp_1.0.14         
##  [4] parallel_4.5.0       jquerylib_0.1.4      splines_4.5.0       
##  [7] yaml_2.3.10          fastmap_1.2.0        lattice_0.22-7      
## [10] TH.data_1.1-3        R6_2.6.1             microbenchmark_1.5.0
## [13] knitr_1.50           htmlwidgets_1.6.4    MASS_7.3-65         
## [16] tibble_3.3.0         bookdown_0.43        profvis_0.4.0       
## [19] bslib_0.9.0          pillar_1.10.2        rlang_1.1.6         
## [22] utf8_1.2.6           multcomp_1.4-28      cachem_1.1.0        
## [25] xfun_0.52            sass_0.4.10          cli_3.6.5           
## [28] magrittr_2.0.3       digest_0.6.37        grid_4.5.0          
## [31] mvtnorm_1.3-3        sandwich_3.1-1       lifecycle_1.0.4     
## [34] vctrs_0.6.5          bench_1.1.4          evaluate_1.0.4      
## [37] glue_1.8.0           codetools_0.2-20     zoo_1.8-14          
## [40] survival_3.8-3       profmem_0.7.0        rmarkdown_2.29      
## [43] tools_4.5.0          pkgconfig_2.0.3      htmltools_0.5.8.1

With Rstudio, you can update both R and Rstudio.

18.1 Latest versions

Make sure R is up to date.

Make sure your R packages are up to date.

Code
update.packages()

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

Code
update.packages(checkBuilt=T, ask=F)
# install.packages(old.packages(checkBuilt=T)[,"Package"])

Tricks: Used Rarely:

To find specific broken packages after an update

Code
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

Code
# 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] )

18.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. Ubuntu and Fedora are popular brands.

On Fedora, you can open RStudio on the commandline with

Code
rstudio

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

Code
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

18.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

Code
Sweave('Sweavefile.Rnw')

or directly from the command line

Code
R CMD Sweave Sweavefile.Rnw 

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

For more on Sweave,

Knitr.

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

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

For background on knitr

18.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

Code
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:

Code
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