knitr::opts_chunk$set(echo = TRUE)

Write existing

This document describes how to add new information to the nuclearWeaponStates dataset in the Ecdat package.

The first step is to update the companion "nuclearWeaponStates.Rd" file.

Then you want to export the nuclearWeaponStates dataset as a csv file so you can edit it to match the changes in the "nuclearWeaponStates.Rd" file:

library(Ecdat)
availInPkg <- objects(2, pattern='nuclearWeaponStates')
canWrite <- FALSE 
if('nuclearWeaponStates' %in% availInPkg){
  data(nuclearWeaponStates)
  canWr <- try(write.csv(nuclearWeaponStates, 
            'nuclearWeaponStates.csv', 
            row.names=FALSE))
  if(!inherits(canWr, 'try-error')){
    canWrite <- TRUE
  }
} 
getwd()

Edit and read

Next edit the csv file just created as mentioned above.

After that's done, put it in the current working directory and read it:

library(Ecfun)
if(canWrite){
  nWS <- read.csv(
    'nuclearWeaponStates.csv', 
    stringsAsFactors = FALSE)
  nuclearWeaponStates <-
    asNumericDF(nWS, 
          ignore=1:2, Dates=3) 
}

Check

There are two checks we can perform:

Does yearsSincePreviousTest = diff(firstTest)?

if(canWrite){
  nmsNucWeapSt <- names(nuclearWeaponStates)
  if('firstTest' %in% nmsNucWeapSt){
    dDays <- diff(
      nuclearWeaponStates[,
                    'firstTest'])
  } else {
    print(nmsNucWeapSt)
    stop("'firstTest' not in names(nuclearWeaponStates)")
  }
  dYrs <- (dDays / 365.25)

  if('yearsSinceLastFirstTest' %in% nmsNucWeapSt){
    edYrs <- (nuclearWeaponStates[-1,
        'yearsSinceLastFirstTest'] 
          - dYrs)
    cbind(dDays, dYrs, edYrs)

    oopsDifYrs <- which(abs(
      nuclearWeaponStates[-1,
        'yearsSinceLastFirstTest'] - 
            dYrs)>.005)
  }
  if(length(oopsDifYrs)>1){
    cat('difftime compute errors. ', 
        ' bad rows = ')
    print(oopsDifYrs)
    oopsi <- unique(c(oopsDifYrs, 
              oopsDifYrs+1))
    cbind(oopsi, nuclearWeaponStates[oopsi, 
        1:3])
  }
}

number of nuclearWeapons

if(canWrite){
  nukes <- rowSums(nuclearWeaponStates[, 
    c('nYieldNA', 'nLowYield', 'nMidYield',
      'nHighYield')])
  dnuk <- (nuclearWeaponStates[,
      'nuclearWeapons'] - nukes)
  if(length(oopsNuk <- which(dnuk != 0))>0){
    cat('count errors.  bad rows =')
    print(oopsNuk)
    cbind(oopsi, nuclearWeaponStates[, -(2:3)])
  }
}

plot

if(canWrite){
  data(nuclearWeaponStates)
  plot(yearsSinceLastFirstTest~firstTest,
       nuclearWeaponStates, las=1, 
       type='h', xlab='', ylab='')
  with(nuclearWeaponStates, 
    text(firstTest, yearsSinceLastFirstTest, ctry))
}

... to svg

if(canWrite){
  svg('Time2nextNuclearState.svg')
  cex. <- 1.5
  plot(yearsSinceLastFirstTest~firstTest,
       nuclearWeaponStates, las=1, 
       type='h', xlab='', ylab='', cex.axis=cex.)
  with(nuclearWeaponStates, 
    text(firstTest, yearsSinceLastFirstTest, ctry, 
         cex=cex.))
  dev.off()
}

save

if(canWrite){
  save(nuclearWeaponStates, 
       file='nuclearWeaponStates.rda')
}

Then copy the *.rda file from the current working directory to "ecdat\data", and run "R CMD checka" as usual.



joshuaulrich/tmp-ecfun documentation built on July 1, 2019, 12:20 a.m.