├── .gitattributes ├── .gitignore ├── 1_intro.R ├── DESCRIPTION_text.txt ├── README.md ├── Writing an R Package.pdf ├── Writing an R Package.pptx ├── add_to_github.md ├── bonobos.R ├── bonobos.csv ├── caribou.R ├── caribou.csv ├── data.R ├── data_processing.R ├── dci.R ├── domR ├── .Rbuildignore ├── .Rhistory ├── .Rprofile ├── .gitignore ├── DESCRIPTION ├── NAMESPACE ├── R │ ├── bonobos.R │ ├── caribou.R │ ├── dci.R │ ├── get_di_matrix.R │ └── ttri.R ├── data-raw │ ├── bonobos.csv │ ├── caribou.csv │ ├── data.R │ └── data_processing.R ├── data │ ├── bonobos.rda │ └── caribou.rda ├── domR.Rproj ├── man │ ├── bonobos.Rd │ ├── caribou.Rd │ ├── dci.Rd │ ├── get_di_matrix.Rd │ └── ttri.Rd ├── packrat │ ├── init.R │ ├── packrat.lock │ ├── packrat.opts │ └── src │ │ └── packrat │ │ └── packrat_0.4.7-1.tar.gz └── vignettes │ └── introduction.Rmd ├── functions_for_package.R ├── get_di_matrix.R └── ttri.R /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # Windows shortcuts 18 | *.lnk 19 | 20 | # ========================= 21 | # Operating System Files 22 | # ========================= 23 | 24 | # OSX 25 | # ========================= 26 | 27 | .DS_Store 28 | .AppleDouble 29 | .LSOverride 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear in the root of a volume 35 | .DocumentRevisions-V100 36 | .fseventsd 37 | .Spotlight-V100 38 | .TemporaryItems 39 | .Trashes 40 | .VolumeIcon.icns 41 | 42 | # Directories potentially created on remote AFP share 43 | .AppleDB 44 | .AppleDesktop 45 | Network Trash Folder 46 | Temporary Items 47 | .apdisk 48 | -------------------------------------------------------------------------------- /1_intro.R: -------------------------------------------------------------------------------- 1 | 2 | ### Install These Packages 3 | 4 | install.packages("devtools") 5 | install.packages("roxygen2") 6 | 7 | 8 | 9 | ### Examples of using Functions when not in package. 10 | 11 | mat <- matrix(c(NA,2,30,6,19,122,0,NA,18, 12 | 0,19,85,0,1,NA,3,8,84,0,0,0,NA,267,50,0, 13 | 0,0,5,NA,10,1,0,4,4,1,NA), ncol=6) 14 | 15 | mat 16 | 17 | get_di_matrix(mat) 18 | dci(mat) 19 | ttri(mat) 20 | 21 | 22 | ### How do we start to put these into a package ? 23 | 24 | -------------------------------------------------------------------------------- /DESCRIPTION_text.txt: -------------------------------------------------------------------------------- 1 | Package: compete 2 | Title: Analyzing Social Hierarchies 3 | Type: Package 4 | Version: 0.1 5 | Author: c(person("James P.", "Curley", role = c("aut", "cre"), 6 | email = "jc3181@columbia.edu") 7 | Maintainer: James P. Curley 8 | Description: Organizing and Analyzing Social Dominance 9 | Hierarchy Data. 10 | Depends: 11 | R (>= 3.1.0) 12 | License: GPL-3 13 | LazyData: true 14 | Imports: 15 | graphics, 16 | igraph, 17 | sna, 18 | stats, 19 | utils 20 | URL: https://github.com/jalapic/compete 21 | BugReports: https://github.com/jalapic/compete 22 | RoxygenNote: 6.0.1 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RPackage 2 | Materials for writing an R Package 3 | 4 | These are the materials associated with a one-hour workshop on writing an R Package. 5 | 6 | A PDF of the presentation can be [found here](https://github.com/jalapic/RPackage/blob/master/Writing%20an%20R%20Package.pdf) 7 | 8 | There are many resources that the tutorial builds upon that are referenced in the presentation. 9 | 10 | 11 | Briefly: 12 | 13 | We created a package called "domR". It has 3 functions (`dci`, `get_di_matrix`, `ttri`) and 2 datasets (`caribou`, `bonobos`). 14 | 15 | The tutorial takes you through: 16 | 17 | - setting up and naming a R package 18 | - adding .R function files 19 | - adding documentation to functions 20 | - adding data files 21 | - adding raw data files 22 | - adding documentation to data files 23 | - adding a vignette 24 | - building the package 25 | - running CRAN checks on the package 26 | - how to install the package locally 27 | - how to publish to GitHub or CRAN 28 | 29 | The final package can be found in the `domR` folder. 30 | 31 | The other files include example .R files that we may wanted to put into a package, and files that we could adapt for documentation etc. 32 | 33 | 34 | Please contact me for more info via the issues tab or @jalapic on twitter. 35 | -------------------------------------------------------------------------------- /Writing an R Package.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jalapic/RPackage/a4e768f64a31d367e9022709747f9b0656496922/Writing an R Package.pdf -------------------------------------------------------------------------------- /Writing an R Package.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jalapic/RPackage/a4e768f64a31d367e9022709747f9b0656496922/Writing an R Package.pptx -------------------------------------------------------------------------------- /add_to_github.md: -------------------------------------------------------------------------------- 1 | ## Add package to GitHub 2 | 3 | There are different ways of doing this. You may find some don't work - in which case try the others! 4 | 5 | ### Using RStudio and a bit of the command line. 6 | 7 | 1. When making your R package make sure the "Create a Git Repository" tab is checked. 8 | 2. When your package is open in RStudio, you should see the "Git" tab in the top right. 9 | 3. Use the Git tab to add and commit your package files. 10 | 4. Create a new repository on GitHub with same name. 11 | Importantly - do not add a README or anything just yet - the repo needs to be empty at this point. 12 | 5. Open the command line (e.g. git bash) 13 | You can do this by opening it from your start menu, or 14 | in RStudio click Tools->Shell to open a terminal/command prompt that is automatically in the directory of the R package 15 | 6. Connect the package with the GitHub repository type: 16 | "git remote add origin https://github.com/yourusername/PackageName.git" 17 | 7. Then type "git push -u origin master" to push everything 18 | 8. You can push/pull future changes using the Git tab in RStudio. 19 | 20 | 21 | *OR* you can do it in the following sequence: 22 | 23 | 1. When making your R package make sure the "Create a Git Repository" tab is checked. 24 | 2. When your package is open in RStudio, you should see the "Git" tab in the top right. 25 | 3. Open the command line (e.g. git bash) 26 | You can do this by opening it from your start menu, or 27 | in RStudio click Tools->Shell to open a terminal/command prompt that is automatically in the directory of the R package 28 | 4. Go to the package directory (use cd ...) 29 | 5. Initialize the repository - type "git init" 30 | 6. Add and commit everything - type "git add -A" and then "git commit" 31 | 7. Create a new repository on GitHub with same name. 32 | Importantly - do not add a README or anything just yet - the repo needs to be empty at this point. 33 | 8. Connect the package with the GitHub repository type: 34 | "git remote add origin https://github.com/yourusername/PackageName.git" 35 | 9. Then type "git push -u origin master" to push everything 36 | 10. Repeat the git add, git commit, git push each time you want to add things (you don't have to do the origin master bit each time). 37 | 38 | 39 | 40 | ### Drag and drop 41 | 42 | 1. Create repository on GitHub 43 | 2. Copy over all your project files to GitHub using the "upload files" tab 44 | 3. Note - if you have very large files this many not work. 45 | 4. I don't recommend this - but it can work. 46 | 47 | 48 | 49 | 50 | ### If you don't see a git tab in your package..... 51 | 52 | Make sure you have an empty repository on GitHub that is of the same name as your package. 53 | 54 | 1. Open the shell from RStudio/tools. 55 | 2. to set up git, type the following in order... 56 | 57 | git init 58 | git add -A 59 | git commit 60 | 61 | [now you have to try and write a commit message. After you manage to do that, press...] 62 | 63 | do this by typing the following: 64 | 65 | I 66 | 67 | 68 | 69 | Enter 70 | 71 | 72 | now type 73 | 74 | 75 | Esc :wq Enter (this is to quit the commit message). 76 | 77 | 78 | Now type: 79 | 80 | git remote add origin https://github.com/yourusername/PackageName.git 81 | 82 | git push -u origin master 83 | 84 | 3. Now check online to see if your package is there in your repository. 85 | 86 | 87 | If you make changes to your project and wish to add them........ 88 | 89 | 4. Open the shell using RStudio/tools 90 | 91 | type: 92 | 93 | git add -A 94 | 95 | git commit [then add a message and quit the viewer using Esc :wq Enter] 96 | 97 | git push 98 | 99 | Check online - the changes should be there. 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | -------------------------------------------------------------------------------- /bonobos.R: -------------------------------------------------------------------------------- 1 | #' Bonobo sociomatrix 2 | #' 3 | #' A win-loss sociomatrix with each row being the number of wins 4 | #' against individual bonobos in each column. 5 | #' 6 | #' Reference: Vervaecke, H., de Vries, H. & van Elsacker, L. 2000. 7 | #' Dominance and its behavioral measures in a captive group of 8 | #' bonobos (Pan paniscus). International Journal of Primatology, 9 | #' 21, 47-68. 10 | #' 11 | #' @format A data frame with 6 rows and 6 variables: 12 | #' \describe{ 13 | #' \item{Dz}{The number of losses by bonobo Dz against all other bonobos} 14 | #' \item{He}{The number of losses by bonobo He against all other bonobos} 15 | #' \item{De}{The number of losses by bonobo De against all other bonobos} 16 | #' \item{Ho}{The number of losses by bonobo Ho against all other bonobos} 17 | #' \item{Lu}{The number of losses by bonobo Lu against all other bonobos} 18 | #' \item{Ki}{The number of losses by bonobo Ki against all other bonobos} 19 | #' } 20 | "bonobos" 21 | -------------------------------------------------------------------------------- /bonobos.csv: -------------------------------------------------------------------------------- 1 | ,Dz,He,De,Ho,Lu,Ki 2 | Dz,NA,0,0,0,0,1 3 | He,2,NA,1,0,0,0 4 | De,30,18,NA,0,0,4 5 | Ho,6,0,3,NA,5,4 6 | Lu,19,19,8,267,NA,1 7 | Ki,122,85,84,50,10,NA 8 | -------------------------------------------------------------------------------- /caribou.R: -------------------------------------------------------------------------------- 1 | #' Caribou sociomatrix 2 | #' 3 | #' A win-loss sociomatrix with each row being the number of wins 4 | #' against individual bonobos in each column. 5 | #' 6 | #' Reference: Barrette, C. & Vandal, D. 1986. Social rank, 7 | #' dominance, antler size, and access to food in snow-bound wild 8 | #' woodland caribou. Behaviour, 97, 118-146. 9 | #' 10 | #' @format A data frame with 20 rows and 20 variables: 11 | #' \describe{ 12 | #' \item{a}{The number of losses by caribou a against all other caribou} 13 | #' \item{b}{The number of losses by caribou b against all other caribou} 14 | #' \item{c}{The number of losses by caribou c against all other caribou} 15 | #' \item{d}{The number of losses by caribou d against all other caribou} 16 | #' \item{e}{The number of losses by caribou e against all other caribou} 17 | #' \item{f}{The number of losses by caribou f against all other caribou} 18 | #' \item{g}{The number of losses by caribou g against all other caribou} 19 | #' \item{h}{The number of losses by caribou h against all other caribou} 20 | #' \item{i}{The number of losses by caribou i against all other caribou} 21 | #' \item{j}{The number of losses by caribou j against all other caribou} 22 | #' \item{k}{The number of losses by caribou k against all other caribou} 23 | #' \item{l}{The number of losses by caribou l against all other caribou} 24 | #' \item{m}{The number of losses by caribou m against all other caribou} 25 | #' \item{n}{The number of losses by caribou n against all other caribou} 26 | #' \item{o}{The number of losses by caribou o against all other caribou} 27 | #' \item{p}{The number of losses by caribou p against all other caribou} 28 | #' \item{q}{The number of losses by caribou q against all other caribou} 29 | #' \item{r}{The number of losses by caribou r against all other caribou} 30 | #' \item{s}{The number of losses by caribou s against all other caribou} 31 | #' \item{t}{The number of losses by caribou t against all other caribou} 32 | #' } 33 | "caribou" 34 | -------------------------------------------------------------------------------- /caribou.csv: -------------------------------------------------------------------------------- 1 | ,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t 2 | a,0,11,3,8,4,2,4,5,3,10,3,5,4,7,5,2,4,0,3,0 3 | b,0,0,4,11,6,2,5,6,1,5,4,4,4,4,3,7,4,2,6,1 4 | c,0,1,0,0,0,16,10,7,8,7,3,5,6,7,5,6,4,3,7,1 5 | d,2,0,8,0,6,9,4,5,3,6,15,9,7,5,9,8,5,2,5,2 6 | e,4,1,3,2,0,5,2,2,5,13,22,2,4,6,2,2,5,2,6,1 7 | f,0,0,0,0,1,0,5,1,6,3,8,9,4,2,3,5,8,8,8,1 8 | g,0,0,0,0,0,0,0,1,3,5,2,9,3,7,4,5,2,2,3,3 9 | h,0,0,0,0,3,1,2,0,3,2,4,1,1,2,5,2,1,1,1,3 10 | i,0,0,0,0,0,0,0,0,0,17,0,0,4,0,0,1,0,4,5,0 11 | j,0,0,0,0,0,0,0,0,0,0,16,6,5,4,0,3,2,0,6,1 12 | k,0,1,0,0,2,0,0,0,6,1,0,8,4,3,0,4,4,5,4,2 13 | l,0,0,0,0,0,0,0,0,2,0,0,0,2,3,1,4,2,0,4,1 14 | m,0,0,0,0,0,0,0,0,0,0,0,1,0,0,2,2,6,3,1,1 15 | n,0,0,0,0,0,0,0,0,1,0,0,1,2,0,5,0,1,2,1,3 16 | o,0,0,0,0,0,0,0,1,1,4,1,0,0,1,0,0,2,0,0,0 17 | p,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,5,2 18 | q,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,2 19 | r,0,0,0,0,0,0,0,0,0,0,0,1,1,0,1,0,2,0,0,0 20 | s,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9 21 | t,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0 22 | -------------------------------------------------------------------------------- /data.R: -------------------------------------------------------------------------------- 1 | mouse <- read.csv("data-raw/mouse.csv", 2 | stringsAsFactors = FALSE) 3 | bonobos <- read.csv("data-raw/bonobos.csv", 4 | stringsAsFactors = FALSE) 5 | people <- read.csv("data-raw/people.csv", 6 | stringsAsFactors = FALSE) 7 | caribou <- read.csv("data-raw/caribou.csv", 8 | stringsAsFactors = FALSE) 9 | el <- read.csv("data-raw/el.csv", 10 | stringsAsFactors = FALSE) 11 | devtools::use_data(mouse, bonobos, people, caribou,el, overwrite = TRUE) 12 | -------------------------------------------------------------------------------- /data_processing.R: -------------------------------------------------------------------------------- 1 | 2 | #Script to turn raw csv into a object for Package 3 | 4 | bonobos <- read.csv("C:/Users/curley1/Dropbox/Work/DataVisualizationTalks/Rpackage/bonobos.csv") 5 | caribou <- read.csv("C:/Users/curley1/Dropbox/Work/DataVisualizationTalks/Rpackage/caribou.csv") 6 | 7 | 8 | bonobos <- bonobos[,-1] 9 | caribou <- caribou[,-1] 10 | 11 | rownames(bonobos)<-colnames(bonobos) 12 | rownames(caribou)<-colnames(caribou) 13 | 14 | write.csv(bonobos, "data-raw/bonobos.csv", row.names=F) 15 | write.csv(caribou, "data-raw/caribou.csv", row.names=F) 16 | -------------------------------------------------------------------------------- /dci.R: -------------------------------------------------------------------------------- 1 | #' Get the directional consistency index (DCI) of a sociomatrix. 2 | #' 3 | #' @param m A matrix with individuals ordered identically in rows and columns. 4 | #' @return The directional consistency of \code{m}. 5 | #' @examples 6 | #' dci(bonobos) 7 | #' @section References: 8 | #' Van Hooff JARAM, Wensing JAB. 1987. 9 | #' Dominance and its behavioural measures in a captive wolf pack. 10 | #' In: Frank HW, editor. Man and Wolf. 11 | #' Dordrecht, Olanda (Netherlands): Junk Publishers 12 | #' pp.219-252. 13 | #' @section Further details: 14 | #' The DCI represents the proportion of occurrences of a behavior 15 | #' that occurs across all dyads in a group from the individual 16 | #' within each dyad performing the behavior with a higher frequency (H) 17 | #' to the individual within each dyad performing the behavior with 18 | #' a lower frequency (L). It is calculated by averaging the following 19 | #' formula across all dyads: DCI = (H - L)/(H + L). The DCI ranges from 0 20 | #' (no directional asymmetry) to 1 (completely unidirectional). 21 | #' @export 22 | 23 | dci<-function(m){ 24 | m<-as.matrix(m) 25 | diag(m)<-0 26 | N=sum(m)/2 27 | dc=sum(abs(m-t(m)))/2/sum(m) 28 | return(dc) 29 | } 30 | -------------------------------------------------------------------------------- /domR/.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^packrat/ 2 | ^\.Rprofile$ 3 | ^.*\.Rproj$ 4 | ^\.Rproj\.user$ 5 | ^data-raw$ 6 | -------------------------------------------------------------------------------- /domR/.Rhistory: -------------------------------------------------------------------------------- 1 | install.packages("devtools") 2 | install.packages("roxygen2") 3 | devtools::load_all() 4 | install.packages("igraph") 5 | devtools::load_all() 6 | devtools::document() 7 | ?ttri 8 | devtools::document() 9 | library(domR) 10 | m <- matrix(c(NA,2,30,6,19,122,0,NA,18, 11 | 0,19,85,0,1,NA,3,8,84,0,0,0,NA,267,50,0, 12 | 0,0,5,NA,10,1,0,4,4,1,NA), ncol=6) 13 | m 14 | ttri(m) 15 | library(domR) 16 | getwd() 17 | setwd("C:/Users/curley1/Dropbox/Work/R/mypackages/domR") 18 | getwd() 19 | library(readr) 20 | caribou <- read_csv("C:/Users/curley1/Dropbox/Work/DataVisualizationTalks/Rpackage/caribou.csv") 21 | View(caribou) 22 | cat("\014") 23 | devtools::use_data(caribou,overwrite=T) 24 | devtools::use_data_raw() 25 | write.csv(caribou, "data-raw/caribou.csv", row.names=F) 26 | library(readr) 27 | bonobos <- read_csv("C:/Users/curley1/Dropbox/Work/DataVisualizationTalks/Rpackage/bonobos.csv") 28 | View(bonobos) 29 | library(readr) 30 | bonobos <- read_csv("C:/Users/curley1/Dropbox/Work/DataVisualizationTalks/Rpackage/bonobos.csv") 31 | View(bonobos) 32 | library(readr) 33 | caribou <- read_csv("C:/Users/curley1/Dropbox/Work/DataVisualizationTalks/Rpackage/caribou.csv") 34 | View(caribou) 35 | write.csv(caribou, "data-raw/caribou.csv", row.names=F) 36 | devtools::use_data(caribou,overwrite=T) 37 | bonobos <- read.csv("data-raw/bonobos.csv", 38 | stringsAsFactors = FALSE) 39 | caribou <- read.csv("data-raw/caribou.csv", 40 | stringsAsFactors = FALSE) 41 | write.csv(bonobos, "data-raw/bonobos.csv", row.names=F) 42 | bonobos <- read.csv("data-raw/bonobos.csv", 43 | stringsAsFactors = FALSE) 44 | caribou <- read.csv("data-raw/caribou.csv", 45 | stringsAsFactors = FALSE) 46 | devtools::use_data(bonobos, caribou, overwrite = TRUE) 47 | cat("\014") 48 | bonobos <- read.csv("data-raw/bonobos.csv", 49 | stringsAsFactors = FALSE) 50 | caribou <- read.csv("data-raw/caribou.csv", 51 | stringsAsFactors = FALSE) 52 | devtools::use_data(bonobos, caribou, overwrite = TRUE) 53 | library(domR) 54 | library(domR) 55 | devtools::document() 56 | m 57 | dci(m) 58 | rm(m) 59 | m 60 | library(domR) 61 | bonobos 62 | bonobos 63 | bonobos[,-1] 64 | bonobos <- bonobos[,-1] 65 | caribou <- caribou[,-1] 66 | rownames(bonobos)<-colnames(bonobos) 67 | rownames(caribou)<-colnames(caribou) 68 | write.csv(bonobos, "data-raw/bonobos.csv", row.names=F) 69 | write.csv(caribou, "data-raw/caribou.csv", row.names=F) 70 | bonobos <- read.csv("data-raw/bonobos.csv", 71 | stringsAsFactors = FALSE) 72 | caribou <- read.csv("data-raw/caribou.csv", 73 | stringsAsFactors = FALSE) 74 | devtools::use_data(bonobos, caribou, overwrite = TRUE) 75 | library(domR) 76 | 1 77 | cat("\014") 78 | bonbos 79 | bonobos 80 | dci(bonobos) 81 | library(domR) 82 | library(domR) 83 | library(domR) 84 | devtools::document() 85 | library(domR) 86 | cat("\014") 87 | devtools::use_vignette("introduction") 88 | -------------------------------------------------------------------------------- /domR/.Rprofile: -------------------------------------------------------------------------------- 1 | #### -- Packrat Autoloader (version 0.4.7-1) -- #### 2 | source("packrat/init.R") 3 | #### -- End Packrat Autoloader -- #### 4 | -------------------------------------------------------------------------------- /domR/.gitignore: -------------------------------------------------------------------------------- 1 | packrat/lib*/ 2 | .Rproj.user 3 | inst/doc 4 | -------------------------------------------------------------------------------- /domR/DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: domR 2 | Title: Analyzing Social Hierarchies 3 | Type: Package 4 | Version: 0.1 5 | Author: c(person("James P.", "Curley", role = c("aut", "cre"), 6 | email = "jc3181@columbia.edu") 7 | Maintainer: James P. Curley 8 | Description: Organizing and Analyzing Social Dominance 9 | Hierarchy Data. 10 | Depends: 11 | R (>= 3.1.0) 12 | License: GPL-3 13 | LazyData: true 14 | Imports: 15 | igraph 16 | URL: https://github.com/jalapic/compete 17 | BugReports: https://github.com/jalapic/compete 18 | RoxygenNote: 6.0.1 19 | Suggests: knitr, 20 | rmarkdown 21 | VignetteBuilder: knitr 22 | -------------------------------------------------------------------------------- /domR/NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | export(dci) 4 | export(get_di_matrix) 5 | export(ttri) 6 | importFrom(igraph,"graph.adjacency") 7 | importFrom(igraph,"triad.census") 8 | -------------------------------------------------------------------------------- /domR/R/bonobos.R: -------------------------------------------------------------------------------- 1 | #' Bonobo sociomatrix 2 | #' 3 | #' A win-loss sociomatrix with each row being the number of wins 4 | #' against individual bonobos in each column. 5 | #' 6 | #' Reference: Vervaecke, H., de Vries, H. & van Elsacker, L. 2000. 7 | #' Dominance and its behavioral measures in a captive group of 8 | #' bonobos (Pan paniscus). International Journal of Primatology, 9 | #' 21, 47-68. 10 | #' 11 | #' @format A data frame with 6 rows and 6 variables: 12 | #' \describe{ 13 | #' \item{Dz}{The number of losses by bonobo Dz against all other bonobos} 14 | #' \item{He}{The number of losses by bonobo He against all other bonobos} 15 | #' \item{De}{The number of losses by bonobo De against all other bonobos} 16 | #' \item{Ho}{The number of losses by bonobo Ho against all other bonobos} 17 | #' \item{Lu}{The number of losses by bonobo Lu against all other bonobos} 18 | #' \item{Ki}{The number of losses by bonobo Ki against all other bonobos} 19 | #' } 20 | "bonobos" 21 | -------------------------------------------------------------------------------- /domR/R/caribou.R: -------------------------------------------------------------------------------- 1 | #' Caribou sociomatrix 2 | #' 3 | #' A win-loss sociomatrix with each row being the number of wins 4 | #' against individual bonobos in each column. 5 | #' 6 | #' Reference: Barrette, C. & Vandal, D. 1986. Social rank, 7 | #' dominance, antler size, and access to food in snow-bound wild 8 | #' woodland caribou. Behaviour, 97, 118-146. 9 | #' 10 | #' @format A data frame with 20 rows and 20 variables: 11 | #' \describe{ 12 | #' \item{a}{The number of losses by caribou a against all other caribou} 13 | #' \item{b}{The number of losses by caribou b against all other caribou} 14 | #' \item{c}{The number of losses by caribou c against all other caribou} 15 | #' \item{d}{The number of losses by caribou d against all other caribou} 16 | #' \item{e}{The number of losses by caribou e against all other caribou} 17 | #' \item{f}{The number of losses by caribou f against all other caribou} 18 | #' \item{g}{The number of losses by caribou g against all other caribou} 19 | #' \item{h}{The number of losses by caribou h against all other caribou} 20 | #' \item{i}{The number of losses by caribou i against all other caribou} 21 | #' \item{j}{The number of losses by caribou j against all other caribou} 22 | #' \item{k}{The number of losses by caribou k against all other caribou} 23 | #' \item{l}{The number of losses by caribou l against all other caribou} 24 | #' \item{m}{The number of losses by caribou m against all other caribou} 25 | #' \item{n}{The number of losses by caribou n against all other caribou} 26 | #' \item{o}{The number of losses by caribou o against all other caribou} 27 | #' \item{p}{The number of losses by caribou p against all other caribou} 28 | #' \item{q}{The number of losses by caribou q against all other caribou} 29 | #' \item{r}{The number of losses by caribou r against all other caribou} 30 | #' \item{s}{The number of losses by caribou s against all other caribou} 31 | #' \item{t}{The number of losses by caribou t against all other caribou} 32 | #' } 33 | "caribou" 34 | -------------------------------------------------------------------------------- /domR/R/dci.R: -------------------------------------------------------------------------------- 1 | #' Get the directional consistency index (DCI) of a sociomatrix. 2 | #' 3 | #' @param m A matrix with individuals ordered identically in rows and columns. 4 | #' @return The directional consistency of \code{m}. 5 | #' @examples 6 | #' dci(bonobos) 7 | #' @section References: 8 | #' Van Hooff JARAM, Wensing JAB. 1987. 9 | #' Dominance and its behavioural measures in a captive wolf pack. 10 | #' In: Frank HW, editor. Man and Wolf. 11 | #' Dordrecht, Olanda (Netherlands): Junk Publishers 12 | #' pp.219-252. 13 | #' @section Further details: 14 | #' The DCI represents the proportion of occurrences of a behavior 15 | #' that occurs across all dyads in a group from the individual 16 | #' within each dyad performing the behavior with a higher frequency (H) 17 | #' to the individual within each dyad performing the behavior with 18 | #' a lower frequency (L). It is calculated by averaging the following 19 | #' formula across all dyads: DCI = (H - L)/(H + L). The DCI ranges from 0 20 | #' (no directional asymmetry) to 1 (completely unidirectional). 21 | #' @export 22 | 23 | dci<-function(m){ 24 | m<-as.matrix(m) 25 | diag(m)<-0 26 | N=sum(m)/2 27 | dc=sum(abs(m-t(m)))/2/sum(m) 28 | return(dc) 29 | } 30 | -------------------------------------------------------------------------------- /domR/R/get_di_matrix.R: -------------------------------------------------------------------------------- 1 | #' Transforms a frequency interaction sociomatrix (valued data) into a dichotomized 1/0 matrix 2 | #' 3 | #' @param m A matrix with individuals ordered identically in rows and columns. 4 | #' @param type Determines the type of dichotomized matrix to be returned. 5 | #' \strong{\code{type}="wl"} is the default which returns a win-loss matrix 6 | #' with a '1' representing a consistent winner and a '0' representing a 7 | #' consistent loser for each dyad of the matrix. A consistent winner is 8 | #' defined as being the individual in each dyad that has absolutely more 9 | #' wins than defeats. In the default condition if competitors have the 10 | #' same number of wins each, they both receive a 0. 11 | #' If \strong{\code{type}="wlties"} the default dichotomized win-loss 12 | #' matrix will be returned but it will also return 0.5 into cells for tied 13 | #' relationships. 14 | #' @return A dichotomized win/loss or presence/absence matrix. 15 | #' @examples 16 | #' get_di_matrix(bonobos) 17 | #' get_di_matrix(caribou) 18 | #' @section References: 19 | #' Appleby, M. C. 1983. The probability of linearity in hierarchies. 20 | #' Animal Behaviour, 31, 600-608. 21 | #' @export 22 | 23 | get_di_matrix <- function(m, type="wl"){ 24 | 25 | 26 | 27 | if(type=="wl") { 28 | m <- as.matrix(m) 29 | m <- (m > t(m)) + 0 30 | return(m) 31 | } 32 | 33 | if(type=="wlties") { 34 | m <- as.matrix(m) 35 | m <-((m > t(m)) + 0) + ((m == t(m)) + 0)/2 36 | return(m) 37 | } 38 | 39 | 40 | } 41 | -------------------------------------------------------------------------------- /domR/R/ttri.R: -------------------------------------------------------------------------------- 1 | #' Get Triangle Transitivity of Sociomatrix 2 | #' 3 | #' @param m A frequency binary win-loss matrix 4 | #' @return Pt and t.tri 5 | #' @examples 6 | #' ttri(caribou) 7 | #' @importFrom igraph "graph.adjacency" 8 | #' @importFrom igraph "triad.census" 9 | #' @section Further details: 10 | #' Algorithm described in D. Shizuka and D. B. McDonald, 2012, 11 | #' A social network perspective on transitivity and linearity 12 | #' in dominance hierarchies. Animal Behaviour. 13 | #' DOI:10.1016/j.anbehav.2012.01.011 14 | #' Code adapted from original code by Dai Shikua - see 15 | #' http://www.shizukalab.com/toolkits/sna/triangle-transitivity 16 | #' @export 17 | 18 | ttri=function(m){ 19 | mat <- get_di_matrix(as.matrix(m)) 20 | diag(mat)=0 21 | g = igraph::graph.adjacency(mat, mode="directed", diag=FALSE) 22 | tri=igraph::triad.census(g) 23 | Ntri=sum(tri*as.vector(c(0,0,0,0,0,0,0,0,1,1,0,1,1,1,1,1))) 24 | sumtri = sum(tri*as.vector(c(0,0,0,0,0,0,0,0,1,0,0,1,1,0.5,0.75,0.75)) ) 25 | Pt=sumtri/Ntri 26 | ttri.val = 4*(Pt-0.75) 27 | return(list("Pt"=Pt, "ttri"=ttri.val)) 28 | } 29 | -------------------------------------------------------------------------------- /domR/data-raw/bonobos.csv: -------------------------------------------------------------------------------- 1 | "Dz","He","De","Ho","Lu","Ki" 2 | NA,0,0,0,0,1 3 | 2,NA,1,0,0,0 4 | 30,18,NA,0,0,4 5 | 6,0,3,NA,5,4 6 | 19,19,8,267,NA,1 7 | 122,85,84,50,10,NA 8 | -------------------------------------------------------------------------------- /domR/data-raw/caribou.csv: -------------------------------------------------------------------------------- 1 | "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t" 2 | 0,11,3,8,4,2,4,5,3,10,3,5,4,7,5,2,4,0,3,0 3 | 0,0,4,11,6,2,5,6,1,5,4,4,4,4,3,7,4,2,6,1 4 | 0,1,0,0,0,16,10,7,8,7,3,5,6,7,5,6,4,3,7,1 5 | 2,0,8,0,6,9,4,5,3,6,15,9,7,5,9,8,5,2,5,2 6 | 4,1,3,2,0,5,2,2,5,13,22,2,4,6,2,2,5,2,6,1 7 | 0,0,0,0,1,0,5,1,6,3,8,9,4,2,3,5,8,8,8,1 8 | 0,0,0,0,0,0,0,1,3,5,2,9,3,7,4,5,2,2,3,3 9 | 0,0,0,0,3,1,2,0,3,2,4,1,1,2,5,2,1,1,1,3 10 | 0,0,0,0,0,0,0,0,0,17,0,0,4,0,0,1,0,4,5,0 11 | 0,0,0,0,0,0,0,0,0,0,16,6,5,4,0,3,2,0,6,1 12 | 0,1,0,0,2,0,0,0,6,1,0,8,4,3,0,4,4,5,4,2 13 | 0,0,0,0,0,0,0,0,2,0,0,0,2,3,1,4,2,0,4,1 14 | 0,0,0,0,0,0,0,0,0,0,0,1,0,0,2,2,6,3,1,1 15 | 0,0,0,0,0,0,0,0,1,0,0,1,2,0,5,0,1,2,1,3 16 | 0,0,0,0,0,0,0,1,1,4,1,0,0,1,0,0,2,0,0,0 17 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,5,2 18 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,2 19 | 0,0,0,0,0,0,0,0,0,0,0,1,1,0,1,0,2,0,0,0 20 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9 21 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0 22 | -------------------------------------------------------------------------------- /domR/data-raw/data.R: -------------------------------------------------------------------------------- 1 | bonobos <- read.csv("data-raw/bonobos.csv", 2 | stringsAsFactors = FALSE) 3 | caribou <- read.csv("data-raw/caribou.csv", 4 | stringsAsFactors = FALSE) 5 | devtools::use_data(bonobos, caribou, overwrite = TRUE) 6 | -------------------------------------------------------------------------------- /domR/data-raw/data_processing.R: -------------------------------------------------------------------------------- 1 | 2 | #Script to turn raw csv into a object for Package 3 | 4 | bonobos <- readr::read_csv("C:/Users/curley1/Dropbox/Work/DataVisualizationTalks/Rpackage/bonobos.csv") 5 | caribou <- readr::read_csv("C:/Users/curley1/Dropbox/Work/DataVisualizationTalks/Rpackage/caribou.csv") 6 | 7 | 8 | bonobos <- bonobos[,-1] 9 | caribou <- caribou[,-1] 10 | 11 | rownames(bonobos)<-colnames(bonobos) 12 | rownames(caribou)<-colnames(caribou) 13 | 14 | write.csv(bonobos, "data-raw/bonobos.csv", row.names=F) 15 | write.csv(caribou, "data-raw/caribou.csv", row.names=F) 16 | -------------------------------------------------------------------------------- /domR/data/bonobos.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jalapic/RPackage/a4e768f64a31d367e9022709747f9b0656496922/domR/data/bonobos.rda -------------------------------------------------------------------------------- /domR/data/caribou.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jalapic/RPackage/a4e768f64a31d367e9022709747f9b0656496922/domR/data/caribou.rda -------------------------------------------------------------------------------- /domR/domR.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: Default 4 | SaveWorkspace: Default 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 2 10 | Encoding: UTF-8 11 | 12 | RnwWeave: Sweave 13 | LaTeX: pdfLaTeX 14 | 15 | AutoAppendNewline: Yes 16 | StripTrailingWhitespace: Yes 17 | 18 | BuildType: Package 19 | PackageUseDevtools: Yes 20 | PackageInstallArgs: --no-multiarch --with-keep.source 21 | PackageCheckArgs: --as-cran 22 | -------------------------------------------------------------------------------- /domR/man/bonobos.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bonobos.R 3 | \docType{data} 4 | \name{bonobos} 5 | \alias{bonobos} 6 | \title{Bonobo sociomatrix} 7 | \format{A data frame with 6 rows and 6 variables: 8 | \describe{ 9 | \item{Dz}{The number of losses by bonobo Dz against all other bonobos} 10 | \item{He}{The number of losses by bonobo He against all other bonobos} 11 | \item{De}{The number of losses by bonobo De against all other bonobos} 12 | \item{Ho}{The number of losses by bonobo Ho against all other bonobos} 13 | \item{Lu}{The number of losses by bonobo Lu against all other bonobos} 14 | \item{Ki}{The number of losses by bonobo Ki against all other bonobos} 15 | }} 16 | \usage{ 17 | bonobos 18 | } 19 | \description{ 20 | A win-loss sociomatrix with each row being the number of wins 21 | against individual bonobos in each column. 22 | } 23 | \details{ 24 | Reference: Vervaecke, H., de Vries, H. & van Elsacker, L. 2000. 25 | Dominance and its behavioral measures in a captive group of 26 | bonobos (Pan paniscus). International Journal of Primatology, 27 | 21, 47-68. 28 | } 29 | \keyword{datasets} 30 | -------------------------------------------------------------------------------- /domR/man/caribou.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/caribou.R 3 | \docType{data} 4 | \name{caribou} 5 | \alias{caribou} 6 | \title{Caribou sociomatrix} 7 | \format{A data frame with 20 rows and 20 variables: 8 | \describe{ 9 | \item{a}{The number of losses by caribou a against all other caribou} 10 | \item{b}{The number of losses by caribou b against all other caribou} 11 | \item{c}{The number of losses by caribou c against all other caribou} 12 | \item{d}{The number of losses by caribou d against all other caribou} 13 | \item{e}{The number of losses by caribou e against all other caribou} 14 | \item{f}{The number of losses by caribou f against all other caribou} 15 | \item{g}{The number of losses by caribou g against all other caribou} 16 | \item{h}{The number of losses by caribou h against all other caribou} 17 | \item{i}{The number of losses by caribou i against all other caribou} 18 | \item{j}{The number of losses by caribou j against all other caribou} 19 | \item{k}{The number of losses by caribou k against all other caribou} 20 | \item{l}{The number of losses by caribou l against all other caribou} 21 | \item{m}{The number of losses by caribou m against all other caribou} 22 | \item{n}{The number of losses by caribou n against all other caribou} 23 | \item{o}{The number of losses by caribou o against all other caribou} 24 | \item{p}{The number of losses by caribou p against all other caribou} 25 | \item{q}{The number of losses by caribou q against all other caribou} 26 | \item{r}{The number of losses by caribou r against all other caribou} 27 | \item{s}{The number of losses by caribou s against all other caribou} 28 | \item{t}{The number of losses by caribou t against all other caribou} 29 | }} 30 | \usage{ 31 | caribou 32 | } 33 | \description{ 34 | A win-loss sociomatrix with each row being the number of wins 35 | against individual bonobos in each column. 36 | } 37 | \details{ 38 | Reference: Barrette, C. & Vandal, D. 1986. Social rank, 39 | dominance, antler size, and access to food in snow-bound wild 40 | woodland caribou. Behaviour, 97, 118-146. 41 | } 42 | \keyword{datasets} 43 | -------------------------------------------------------------------------------- /domR/man/dci.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/dci.R 3 | \name{dci} 4 | \alias{dci} 5 | \title{Get the directional consistency index (DCI) of a sociomatrix.} 6 | \usage{ 7 | dci(m) 8 | } 9 | \arguments{ 10 | \item{m}{A matrix with individuals ordered identically in rows and columns.} 11 | } 12 | \value{ 13 | The directional consistency of \code{m}. 14 | } 15 | \description{ 16 | Get the directional consistency index (DCI) of a sociomatrix. 17 | } 18 | \section{References}{ 19 | 20 | Van Hooff JARAM, Wensing JAB. 1987. 21 | Dominance and its behavioural measures in a captive wolf pack. 22 | In: Frank HW, editor. Man and Wolf. 23 | Dordrecht, Olanda (Netherlands): Junk Publishers 24 | pp.219-252. 25 | } 26 | 27 | \section{Further details}{ 28 | 29 | The DCI represents the proportion of occurrences of a behavior 30 | that occurs across all dyads in a group from the individual 31 | within each dyad performing the behavior with a higher frequency (H) 32 | to the individual within each dyad performing the behavior with 33 | a lower frequency (L). It is calculated by averaging the following 34 | formula across all dyads: DCI = (H - L)/(H + L). The DCI ranges from 0 35 | (no directional asymmetry) to 1 (completely unidirectional). 36 | } 37 | 38 | \examples{ 39 | dci(bonobos) 40 | } 41 | -------------------------------------------------------------------------------- /domR/man/get_di_matrix.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/get_di_matrix.R 3 | \name{get_di_matrix} 4 | \alias{get_di_matrix} 5 | \title{Transforms a frequency interaction sociomatrix (valued data) into a dichotomized 1/0 matrix} 6 | \usage{ 7 | get_di_matrix(m, type = "wl") 8 | } 9 | \arguments{ 10 | \item{m}{A matrix with individuals ordered identically in rows and columns.} 11 | 12 | \item{type}{Determines the type of dichotomized matrix to be returned. 13 | \strong{\code{type}="wl"} is the default which returns a win-loss matrix 14 | with a '1' representing a consistent winner and a '0' representing a 15 | consistent loser for each dyad of the matrix. A consistent winner is 16 | defined as being the individual in each dyad that has absolutely more 17 | wins than defeats. In the default condition if competitors have the 18 | same number of wins each, they both receive a 0. 19 | If \strong{\code{type}="wlties"} the default dichotomized win-loss 20 | matrix will be returned but it will also return 0.5 into cells for tied 21 | relationships.} 22 | } 23 | \value{ 24 | A dichotomized win/loss or presence/absence matrix. 25 | } 26 | \description{ 27 | Transforms a frequency interaction sociomatrix (valued data) into a dichotomized 1/0 matrix 28 | } 29 | \section{References}{ 30 | 31 | Appleby, M. C. 1983. The probability of linearity in hierarchies. 32 | Animal Behaviour, 31, 600-608. 33 | } 34 | 35 | \examples{ 36 | get_di_matrix(bonobos) 37 | get_di_matrix(caribou) 38 | } 39 | -------------------------------------------------------------------------------- /domR/man/ttri.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/ttri.R 3 | \name{ttri} 4 | \alias{ttri} 5 | \title{Get Triangle Transitivity of Sociomatrix} 6 | \usage{ 7 | ttri(m) 8 | } 9 | \arguments{ 10 | \item{m}{A frequency binary win-loss matrix} 11 | } 12 | \value{ 13 | Pt and t.tri 14 | } 15 | \description{ 16 | Get Triangle Transitivity of Sociomatrix 17 | } 18 | \section{Further details}{ 19 | 20 | Algorithm described in D. Shizuka and D. B. McDonald, 2012, 21 | A social network perspective on transitivity and linearity 22 | in dominance hierarchies. Animal Behaviour. 23 | DOI:10.1016/j.anbehav.2012.01.011 24 | Code adapted from original code by Dai Shikua - see 25 | http://www.shizukalab.com/toolkits/sna/triangle-transitivity 26 | } 27 | 28 | \examples{ 29 | ttri(caribou) 30 | } 31 | -------------------------------------------------------------------------------- /domR/packrat/init.R: -------------------------------------------------------------------------------- 1 | local({ 2 | 3 | ## Helper function to get the path to the library directory for a 4 | ## given packrat project. 5 | getPackratLibDir <- function(projDir = NULL) { 6 | path <- file.path("packrat", "lib", R.version$platform, getRversion()) 7 | 8 | if (!is.null(projDir)) { 9 | 10 | ## Strip trailing slashes if necessary 11 | projDir <- sub("/+$", "", projDir) 12 | 13 | ## Only prepend path if different from current working dir 14 | if (!identical(normalizePath(projDir), normalizePath(getwd()))) 15 | path <- file.path(projDir, path) 16 | } 17 | 18 | path 19 | } 20 | 21 | ## Ensure that we set the packrat library directory relative to the 22 | ## project directory. Normally, this should be the working directory, 23 | ## but we also use '.rs.getProjectDirectory()' if necessary (e.g. we're 24 | ## rebuilding a project while within a separate directory) 25 | libDir <- if (exists(".rs.getProjectDirectory")) 26 | getPackratLibDir(.rs.getProjectDirectory()) 27 | else 28 | getPackratLibDir() 29 | 30 | ## Unload packrat in case it's loaded -- this ensures packrat _must_ be 31 | ## loaded from the private library. Note that `requireNamespace` will 32 | ## succeed if the package is already loaded, regardless of lib.loc! 33 | if ("packrat" %in% loadedNamespaces()) 34 | try(unloadNamespace("packrat"), silent = TRUE) 35 | 36 | if (suppressWarnings(requireNamespace("packrat", quietly = TRUE, lib.loc = libDir))) { 37 | 38 | # Check 'print.banner.on.startup' -- when NA and RStudio, don't print 39 | print.banner <- packrat::get_opts("print.banner.on.startup") 40 | if (print.banner == "auto" && is.na(Sys.getenv("RSTUDIO", unset = NA))) { 41 | print.banner <- TRUE 42 | } else { 43 | print.banner <- FALSE 44 | } 45 | return(packrat::on(print.banner = print.banner)) 46 | } 47 | 48 | ## Escape hatch to allow RStudio to handle bootstrapping. This 49 | ## enables RStudio to provide print output when automagically 50 | ## restoring a project from a bundle on load. 51 | if (!is.na(Sys.getenv("RSTUDIO", unset = NA)) && 52 | is.na(Sys.getenv("RSTUDIO_PACKRAT_BOOTSTRAP", unset = NA))) { 53 | Sys.setenv("RSTUDIO_PACKRAT_BOOTSTRAP" = "1") 54 | setHook("rstudio.sessionInit", function(...) { 55 | # Ensure that, on sourcing 'packrat/init.R', we are 56 | # within the project root directory 57 | if (exists(".rs.getProjectDirectory")) { 58 | owd <- getwd() 59 | setwd(.rs.getProjectDirectory()) 60 | on.exit(setwd(owd), add = TRUE) 61 | } 62 | source("packrat/init.R") 63 | }) 64 | return(invisible(NULL)) 65 | } 66 | 67 | ## Bootstrapping -- only performed in interactive contexts, 68 | ## or when explicitly asked for on the command line 69 | if (interactive() || "--bootstrap-packrat" %in% commandArgs(TRUE)) { 70 | 71 | message("Packrat is not installed in the local library -- ", 72 | "attempting to bootstrap an installation...") 73 | 74 | ## We need utils for the following to succeed -- there are calls to functions 75 | ## in 'restore' that are contained within utils. utils gets loaded at the 76 | ## end of start-up anyhow, so this should be fine 77 | library("utils", character.only = TRUE) 78 | 79 | ## Install packrat into local project library 80 | packratSrcPath <- list.files(full.names = TRUE, 81 | file.path("packrat", "src", "packrat") 82 | ) 83 | 84 | ## No packrat tarballs available locally -- try some other means of installation 85 | if (!length(packratSrcPath)) { 86 | 87 | message("> No source tarball of packrat available locally") 88 | 89 | ## There are no packrat sources available -- try using a version of 90 | ## packrat installed in the user library to bootstrap 91 | if (requireNamespace("packrat", quietly = TRUE) && packageVersion("packrat") >= "0.2.0.99") { 92 | message("> Using user-library packrat (", 93 | packageVersion("packrat"), 94 | ") to bootstrap this project") 95 | } 96 | 97 | ## Couldn't find a user-local packrat -- try finding and using devtools 98 | ## to install 99 | else if (requireNamespace("devtools", quietly = TRUE)) { 100 | message("> Attempting to use devtools::install_github to install ", 101 | "a temporary version of packrat") 102 | library(stats) ## for setNames 103 | devtools::install_github("rstudio/packrat") 104 | } 105 | 106 | ## Try downloading packrat from CRAN if available 107 | else if ("packrat" %in% rownames(available.packages())) { 108 | message("> Installing packrat from CRAN") 109 | install.packages("packrat") 110 | } 111 | 112 | ## Fail -- couldn't find an appropriate means of installing packrat 113 | else { 114 | stop("Could not automatically bootstrap packrat -- try running ", 115 | "\"'install.packages('devtools'); devtools::install_github('rstudio/packrat')\"", 116 | "and restarting R to bootstrap packrat.") 117 | } 118 | 119 | # Restore the project, unload the temporary packrat, and load the private packrat 120 | packrat::restore(prompt = FALSE, restart = TRUE) 121 | 122 | ## This code path only reached if we didn't restart earlier 123 | unloadNamespace("packrat") 124 | requireNamespace("packrat", lib.loc = libDir, quietly = TRUE) 125 | return(packrat::on()) 126 | 127 | } 128 | 129 | ## Multiple packrat tarballs available locally -- try to choose one 130 | ## TODO: read lock file and infer most appropriate from there; low priority because 131 | ## after bootstrapping packrat a restore should do the right thing 132 | if (length(packratSrcPath) > 1) { 133 | warning("Multiple versions of packrat available in the source directory;", 134 | "using packrat source:\n- ", shQuote(packratSrcPath)) 135 | packratSrcPath <- packratSrcPath[[1]] 136 | } 137 | 138 | 139 | lib <- file.path("packrat", "lib", R.version$platform, getRversion()) 140 | if (!file.exists(lib)) { 141 | dir.create(lib, recursive = TRUE) 142 | } 143 | lib <- normalizePath(lib, winslash = "/") 144 | 145 | message("> Installing packrat into project private library:") 146 | message("- ", shQuote(lib)) 147 | 148 | surround <- function(x, with) { 149 | if (!length(x)) return(character()) 150 | paste0(with, x, with) 151 | } 152 | 153 | ## The following is performed because a regular install.packages call can fail 154 | peq <- function(x, y) paste(x, y, sep = " = ") 155 | installArgs <- c( 156 | peq("pkgs", surround(packratSrcPath, with = "'")), 157 | peq("lib", surround(lib, with = "'")), 158 | peq("repos", "NULL"), 159 | peq("type", surround("source", with = "'")) 160 | ) 161 | installCmd <- paste(sep = "", 162 | "utils::install.packages(", 163 | paste(installArgs, collapse = ", "), 164 | ")") 165 | 166 | fullCmd <- paste( 167 | surround(file.path(R.home("bin"), "R"), with = "\""), 168 | "--vanilla", 169 | "--slave", 170 | "-e", 171 | surround(installCmd, with = "\"") 172 | ) 173 | system(fullCmd) 174 | 175 | ## Tag the installed packrat so we know it's managed by packrat 176 | ## TODO: should this be taking information from the lockfile? this is a bit awkward 177 | ## because we're taking an un-annotated packrat source tarball and simply assuming it's now 178 | ## an 'installed from source' version 179 | 180 | ## -- InstallAgent -- ## 181 | installAgent <- 'InstallAgent: packrat 0.4.7-1' 182 | 183 | ## -- InstallSource -- ## 184 | installSource <- 'InstallSource: source' 185 | 186 | packratDescPath <- file.path(lib, "packrat", "DESCRIPTION") 187 | DESCRIPTION <- readLines(packratDescPath) 188 | DESCRIPTION <- c(DESCRIPTION, installAgent, installSource) 189 | cat(DESCRIPTION, file = packratDescPath, sep = "\n") 190 | 191 | # Otherwise, continue on as normal 192 | message("> Attaching packrat") 193 | library("packrat", character.only = TRUE, lib.loc = lib) 194 | 195 | message("> Restoring library") 196 | restore(restart = FALSE) 197 | 198 | # If the environment allows us to restart, do so with a call to restore 199 | restart <- getOption("restart") 200 | if (!is.null(restart)) { 201 | message("> Packrat bootstrap successfully completed. ", 202 | "Restarting R and entering packrat mode...") 203 | return(restart()) 204 | } 205 | 206 | # Callers (source-erers) can define this hidden variable to make sure we don't enter packrat mode 207 | # Primarily useful for testing 208 | if (!exists(".__DONT_ENTER_PACKRAT_MODE__.") && interactive()) { 209 | message("> Packrat bootstrap successfully completed. Entering packrat mode...") 210 | packrat::on() 211 | } 212 | 213 | Sys.unsetenv("RSTUDIO_PACKRAT_BOOTSTRAP") 214 | 215 | } 216 | 217 | }) 218 | -------------------------------------------------------------------------------- /domR/packrat/packrat.lock: -------------------------------------------------------------------------------- 1 | PackratFormat: 1.4 2 | PackratVersion: 0.4.7.1 3 | RVersion: 3.3.0 4 | Repos: CRAN=https://cran.rstudio.com/, 5 | CRANextra=http://www.stats.ox.ac.uk/pub/RWin 6 | 7 | Package: packrat 8 | Source: CRAN 9 | Version: 0.4.7-1 10 | Hash: 16ca16d36eb18d1d16113606ecc5c400 11 | -------------------------------------------------------------------------------- /domR/packrat/packrat.opts: -------------------------------------------------------------------------------- 1 | auto.snapshot: TRUE 2 | use.cache: FALSE 3 | print.banner.on.startup: auto 4 | vcs.ignore.lib: TRUE 5 | vcs.ignore.src: FALSE 6 | external.packages: 7 | local.repos: 8 | load.external.packages.on.startup: TRUE 9 | ignored.packages: 10 | quiet.package.installation: TRUE 11 | snapshot.recommended.packages: FALSE 12 | -------------------------------------------------------------------------------- /domR/packrat/src/packrat/packrat_0.4.7-1.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jalapic/RPackage/a4e768f64a31d367e9022709747f9b0656496922/domR/packrat/src/packrat/packrat_0.4.7-1.tar.gz -------------------------------------------------------------------------------- /domR/vignettes/introduction.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Vignette Title" 3 | author: "Vignette Author" 4 | date: "`r Sys.Date()`" 5 | output: rmarkdown::html_vignette 6 | vignette: > 7 | %\VignetteIndexEntry{Vignette Title} 8 | %\VignetteEngine{knitr::rmarkdown} 9 | %\VignetteEncoding{UTF-8} 10 | --- 11 | 12 | Vignettes are long form documentation commonly included in packages. Because they are part of the distribution of the package, they need to be as compact as possible. The `html_vignette` output type provides a custom style sheet (and tweaks some options) to ensure that the resulting html is as small as possible. The `html_vignette` format: 13 | 14 | - Never uses retina figures 15 | - Has a smaller default figure size 16 | - Uses a custom CSS stylesheet instead of the default Twitter Bootstrap style 17 | 18 | ## Vignette Info 19 | 20 | Note the various macros within the `vignette` section of the metadata block above. These are required in order to instruct R how to build the vignette. Note that you should change the `title` field and the `\VignetteIndexEntry` to match the title of your vignette. 21 | 22 | ## Styles 23 | 24 | The `html_vignette` template includes a basic CSS theme. To override this theme you can specify your own CSS in the document metadata as follows: 25 | 26 | output: 27 | rmarkdown::html_vignette: 28 | css: mystyles.css 29 | 30 | ## Figures 31 | 32 | The figure sizes have been customised so that you can easily put two images side-by-side. 33 | 34 | ```{r, fig.show='hold'} 35 | plot(1:10) 36 | plot(10:1) 37 | ``` 38 | 39 | You can enable figure captions by `fig_caption: yes` in YAML: 40 | 41 | output: 42 | rmarkdown::html_vignette: 43 | fig_caption: yes 44 | 45 | Then you can use the chunk option `fig.cap = "Your figure caption."` in **knitr**. 46 | 47 | ## More Examples 48 | 49 | You can write math expressions, e.g. $Y = X\beta + \epsilon$, footnotes^[A footnote here.], and tables, e.g. using `knitr::kable()`. 50 | 51 | ```{r, echo=FALSE, results='asis'} 52 | knitr::kable(head(mtcars, 10)) 53 | ``` 54 | 55 | Also a quote using `>`: 56 | 57 | > "He who gives up [code] safety for [code] speed deserves neither." 58 | ([via](https://twitter.com/hadleywickham/status/504368538874703872)) 59 | -------------------------------------------------------------------------------- /functions_for_package.R: -------------------------------------------------------------------------------- 1 | # Functions I want to include in my R package 2 | 3 | 4 | ### Calculates Triangle Transitivity Index 5 | ttri <- function(m){ 6 | mat <- get_di_matrix(as.matrix(m)) 7 | diag(mat)=0 8 | g = igraph::graph.adjacency(mat, mode="directed", diag=FALSE) 9 | tri=igraph::triad.census(g) 10 | Ntri=sum(tri*as.vector(c(0,0,0,0,0,0,0,0,1,1,0,1,1,1,1,1))) 11 | sumtri = sum(tri*as.vector(c(0,0,0,0,0,0,0,0,1,0,0,1,1,0.5,0.75,0.75)) ) 12 | Pt=sumtri/Ntri 13 | ttri.val = 4*(Pt-0.75) 14 | return(list("Pt"=Pt, "ttri"=ttri.val)) 15 | } 16 | 17 | 18 | ### Calculates Directional Consistency Index 19 | dci<-function(m){ 20 | m<-as.matrix(m) 21 | diag(m)<-0 22 | N=sum(m)/2 23 | dc=sum(abs(m-t(m)))/2/sum(m) 24 | return(dc) 25 | } 26 | 27 | 28 | ### Convert Raw Matrix into a Binary 1/0 Matrix 29 | get_di_matrix <- function(m, type="wl"){ 30 | 31 | 32 | 33 | if(type=="wl") { 34 | m <- as.matrix(m) 35 | m <- (m > t(m)) + 0 36 | return(m) 37 | } 38 | 39 | if(type=="wlties") { 40 | m <- as.matrix(m) 41 | m <-((m > t(m)) + 0) + ((m == t(m)) + 0)/2 42 | return(m) 43 | } 44 | 45 | 46 | } 47 | 48 | 49 | -------------------------------------------------------------------------------- /get_di_matrix.R: -------------------------------------------------------------------------------- 1 | #' Transforms a frequency interaction sociomatrix (valued data) into a dichotomized 1/0 matrix 2 | #' 3 | #' @param m A matrix with individuals ordered identically in rows and columns. 4 | #' @param type Determines the type of dichotomized matrix to be returned. 5 | #' \strong{\code{type}="wl"} is the default which returns a win-loss matrix 6 | #' with a '1' representing a consistent winner and a '0' representing a 7 | #' consistent loser for each dyad of the matrix. A consistent winner is 8 | #' defined as being the individual in each dyad that has absolutely more 9 | #' wins than defeats. In the default condition if competitors have the 10 | #' same number of wins each, they both receive a 0. 11 | #' If \strong{\code{type}="wlties"} the default dichotomized win-loss 12 | #' matrix will be returned but it will also return 0.5 into cells for tied 13 | #' relationships. 14 | #' @return A dichotomized win/loss or presence/absence matrix. 15 | #' @examples 16 | #' get_di_matrix(bonobos) 17 | #' get_di_matrix(caribou) 18 | #' @section References: 19 | #' Appleby, M. C. 1983. The probability of linearity in hierarchies. 20 | #' Animal Behaviour, 31, 600-608. 21 | #' @export 22 | 23 | get_di_matrix <- function(m, type="wl"){ 24 | 25 | 26 | 27 | if(type=="wl") { 28 | m <- as.matrix(m) 29 | m <- (m > t(m)) + 0 30 | return(m) 31 | } 32 | 33 | if(type=="wlties") { 34 | m <- as.matrix(m) 35 | m <-((m > t(m)) + 0) + ((m == t(m)) + 0)/2 36 | return(m) 37 | } 38 | 39 | 40 | } 41 | -------------------------------------------------------------------------------- /ttri.R: -------------------------------------------------------------------------------- 1 | #' Get Triangle Transitivity of Sociomatrix 2 | #' 3 | #' @param m A frequency binary win-loss matrix 4 | #' @return Pt and t.tri 5 | #' @examples 6 | #' ttri(caribou) 7 | #' @importFrom igraph "graph.adjacency" 8 | #' @importFrom igraph "triad.census" 9 | #' @section Further details: 10 | #' Algorithm described in D. Shizuka and D. B. McDonald, 2012, 11 | #' A social network perspective on transitivity and linearity 12 | #' in dominance hierarchies. Animal Behaviour. 13 | #' DOI:10.1016/j.anbehav.2012.01.011 14 | #' Code adapted from original code by Dai Shikua - see 15 | #' http://www.shizukalab.com/toolkits/sna/triangle-transitivity 16 | #' @export 17 | 18 | ttri <- function(m){ 19 | mat <- get_di_matrix(as.matrix(m)) 20 | diag(mat)=0 21 | g = igraph::graph.adjacency(mat, mode="directed", diag=FALSE) 22 | tri=igraph::triad.census(g) 23 | Ntri=sum(tri*as.vector(c(0,0,0,0,0,0,0,0,1,1,0,1,1,1,1,1))) 24 | sumtri = sum(tri*as.vector(c(0,0,0,0,0,0,0,0,1,0,0,1,1,0.5,0.75,0.75)) ) 25 | Pt=sumtri/Ntri 26 | ttri.val = 4*(Pt-0.75) 27 | return(list("Pt"=Pt, "ttri"=ttri.val)) 28 | } 29 | --------------------------------------------------------------------------------