15 Software

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

sessionInfo()
## R version 4.4.2 (2024-10-31)
## Platform: x86_64-redhat-linux-gnu
## Running under: Fedora Linux 40 (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] parallel  stats     graphics  grDevices utils     datasets  compiler 
## [8] methods   base     
## 
## other attached packages:
## [1] Rcpp_1.0.14          profvis_0.4.0        microbenchmark_1.5.0
## [4] sf_1.0-19            plotly_4.10.4        ggplot2_3.5.1       
## [7] colorout_1.3-2      
## 
## loaded via a namespace (and not attached):
##  [1] sandwich_3.1-1     sass_0.4.9         generics_0.1.3     tidyr_1.3.1       
##  [5] class_7.3-23       KernSmooth_2.23-26 lattice_0.22-6     digest_0.6.37     
##  [9] magrittr_2.0.3     evaluate_1.0.3     grid_4.4.2         bookdown_0.42     
## [13] mvtnorm_1.3-3      fastmap_1.2.0      Matrix_1.7-2       jsonlite_1.8.9    
## [17] e1071_1.7-16       DBI_1.2.3          survival_3.8-3     multcomp_1.4-28   
## [21] httr_1.4.7         purrr_1.0.4        crosstalk_1.2.1    viridisLite_0.4.2 
## [25] scales_1.3.0       TH.data_1.1-3      codetools_0.2-20   lazyeval_0.2.2    
## [29] jquerylib_0.1.4    cli_3.6.3          rlang_1.1.5        units_0.8-5       
## [33] splines_4.4.2      munsell_0.5.1      withr_3.0.2        cachem_1.1.0      
## [37] yaml_2.3.10        tools_4.4.2        dplyr_1.1.4        colorspace_2.1-1  
## [41] vctrs_0.6.5        R6_2.5.1           zoo_1.8-12         proxy_0.4-27      
## [45] lifecycle_1.0.4    classInt_0.4-11    htmlwidgets_1.6.4  MASS_7.3-64       
## [49] pkgconfig_2.0.3    pillar_1.10.1      bslib_0.9.0        gtable_0.3.6      
## [53] data.table_1.16.4  glue_1.8.0         xfun_0.50          tibble_3.2.1      
## [57] tidyselect_1.2.1   knitr_1.49         htmltools_0.5.8.1  rmarkdown_2.29

With Rstudio, you can update both R and Rstudio.

15.1 Latest versions

Make sure R is up to date.

Make sure your R packages are up to date.

update.packages()

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

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

Used Rarely:

To find specific 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

Knitr: You can 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

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('Sweavefile.Rnw')

or directly from the command line

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,

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