About

This is a minimal demonstration of how to have an R package that is entirely archived:

Init

Load some packages we want for this project.

options(digits = 2)
library(renv)
## 
## Attaching package: 'renv'
## The following object is masked from 'package:stats':
## 
##     update
## The following objects are masked from 'package:utils':
## 
##     history, upgrade
## The following objects are masked from 'package:base':
## 
##     load, remove
library(osfr)
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.0     ✓ purrr   0.3.4
## ✓ tibble  3.0.1     ✓ dplyr   0.8.5
## ✓ tidyr   1.0.3     ✓ stringr 1.4.0
## ✓ readr   1.3.1     ✓ forcats 0.5.0
## ── Conflicts ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
## x purrr::modify() masks renv::modify()
library(psych)
## 
## Attaching package: 'psych'
## The following objects are masked from 'package:ggplot2':
## 
##     %+%, alpha
#make subdirs if needed
dir.create("data", showWarnings = F)
dir.create("figs", showWarnings = F)

#you need to set up a personal token for osfr
#good idea is to then place it somewhere safe for reuse
osf_auth(read_lines("~/.config/osf_token"))
## Registered PAT from the provided token
#the project we will use
osf_proj = osf_retrieve_node("https://osf.io/bgx5v/")

Start using renv

If we haven’t run this command already.

#test if already init
if (!file.exists("renv.lock")) {
  #initiatelize
  renv::init()
} else {
  #update packages in renv
  renv::snapshot()
}
## * The lockfile is already up to date.

Do some science

#factor analyze iris dataset with psych
iris_fa = fa(iris[-5], nfactors = 2)
## Loading required namespace: GPArotation
## Warning in fac(r = r, nfactors = nfactors, n.obs = n.obs, rotate = rotate, : I
## am sorry, to do these rotations requires the GPArotation package to be installed
## Warning in fa.stats(r = r, f = f, phi = phi, n.obs = n.obs, np.obs = np.obs, :
## The estimated weights for the factor scores are probably incorrect. Try a
## different factor score estimation method.
## Warning in fac(r = r, nfactors = nfactors, n.obs = n.obs, rotate = rotate, : An
## ultra-Heywood case was detected. Examine the results carefully
iris_fa
## Factor Analysis using method =  minres
## Call: fa(r = iris[-5], nfactors = 2)
## Standardized loadings (pattern matrix) based upon correlation matrix
##                MR1  MR2   h2     u2 com
## Sepal.Length  0.84 0.32 0.81  0.188 1.3
## Sepal.Width  -0.47 0.87 0.97  0.031 1.5
## Petal.Length  1.00 0.05 1.01 -0.011 1.0
## Petal.Width   0.95 0.09 0.90  0.097 1.0
## 
##                        MR1  MR2
## SS loadings           2.83 0.86
## Proportion Var        0.71 0.22
## Cumulative Var        0.71 0.92
## Proportion Explained  0.77 0.23
## Cumulative Proportion 0.77 1.00
## 
## Mean item complexity =  1.2
## Test of the hypothesis that 2 factors are sufficient.
## 
## The degrees of freedom for the null model are  6  and the objective function was  4.8 with Chi Square of  707
## The degrees of freedom for the model are -1  and the objective function was  0.11 
## 
## The root mean square of the residuals (RMSR) is  0.01 
## The df corrected root mean square of the residuals is  NA 
## 
## The harmonic number of observations is  150 with the empirical chi square  0.06  with prob <  NA 
## The total number of observations was  150  with Likelihood Chi Square =  16  with prob <  NA 
## 
## Tucker Lewis Index of factoring reliability =  1.1
## Fit based upon off diagonal values = 1
#plot scores by Species
iris = cbind(iris, iris_fa$scores)
ggplot(iris, aes(MR1, MR2, color = Species)) +
  geom_point() +
  geom_smooth() +
  theme_bw() +
  ggtitle("Factor scores from iris dataset by species")
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

ggsave("figs/factor_scores_species.png")
## Saving 7 x 5 in image
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
#save iris to data dir
iris %>% 
  write_tsv("data/iris.tsv")

Versions

sessionInfo()
## R version 4.0.0 (2020-04-24)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Linux Mint 19.3
## 
## Matrix products: default
## BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1
## 
## 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=de_DE.UTF-8    LC_MESSAGES=en_US.UTF-8   
##  [7] LC_PAPER=de_DE.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=de_DE.UTF-8 LC_IDENTIFICATION=C       
## 
## attached base packages:
## [1] stats     graphics  grDevices datasets  utils     methods   base     
## 
## other attached packages:
##  [1] psych_1.9.12.31 forcats_0.5.0   stringr_1.4.0   dplyr_0.8.5    
##  [5] purrr_0.3.4     readr_1.3.1     tidyr_1.0.3     tibble_3.0.1   
##  [9] ggplot2_3.3.0   tidyverse_1.3.0 osfr_0.2.8      renv_0.10.0    
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_1.0.4.6     lubridate_1.7.8  lattice_0.20-41  assertthat_0.2.1
##  [5] digest_0.6.25    R6_2.4.1         cellranger_1.1.0 backports_1.1.7 
##  [9] reprex_0.3.0     evaluate_0.14    httr_1.4.1       pillar_1.4.4    
## [13] rlang_0.4.6      curl_4.3         readxl_1.3.1     rstudioapi_0.11 
## [17] Matrix_1.2-18    rmarkdown_2.1    labeling_0.3     splines_4.0.0   
## [21] urltools_1.7.3   triebeard_0.3.0  munsell_0.5.0    broom_0.5.6     
## [25] compiler_4.0.0   modelr_0.1.7     xfun_0.13        pkgconfig_2.0.3 
## [29] mnormt_1.5-7     mgcv_1.8-31      htmltools_0.4.0  tidyselect_1.1.0
## [33] httpcode_0.3.0   fansi_0.4.1      crayon_1.3.4     dbplyr_1.4.3    
## [37] withr_2.2.0      crul_0.9.0       grid_4.0.0       nlme_3.1-147    
## [41] jsonlite_1.6.1   gtable_0.3.0     lifecycle_0.2.0  DBI_1.1.0       
## [45] magrittr_1.5     scales_1.1.1     cli_2.0.2        stringi_1.4.6   
## [49] farver_2.0.3     fs_1.4.1         xml2_1.3.2       ellipsis_0.3.1  
## [53] generics_0.0.2   vctrs_0.3.0      tools_4.0.0      glue_1.4.1      
## [57] hms_0.5.3        parallel_4.0.0   yaml_2.2.1       colorspace_1.4-1
## [61] rvest_0.3.5      memoise_1.1.0    knitr_1.28       haven_2.2.0

Upload project to OSF with osfr

Same as this minimal example project.

#upload all files in project
#overwrite existing (versioning)
osf_upload(osf_proj, path = list.files(), conflicts = "overwrite", recurse = T)
## Searching for conflicting files on OSF
## Retrieving 31 of 31 available items:
## ..retrieved 10 items
## ..retrieved 20 items
## ..retrieved 30 items
## ..retrieved 31 items
## ..done
## Updating 69 existing file(s) on OSF
## Uploading 1 new file(s) to OSF
#list files
osf_ls_files(osf_proj)