require(Hmisc)
require(data.table)
getRs('importREDCap.r', put='source')
d <- importREDCap()
o <- subset(d, other != '')

f <- 'comments.md'
cat('---\npagetitle: Survey Comments\n---\n# Respondent Comments on Interrupted Endpoints Survey\n', file=f)
for(ro in levels(o$role)) {
  cat('\n## ', upFirst(ro), '\n\n', file=f, append=TRUE)  
  cat(paste0('* ', o$other[o$role == ro], '\n'), sep='', file=f, append=TRUE)
}

f <- function(..., labs=c('Choice', 'Frequency')) {
  w <- knitr::kable(table(...), col.names=if(length(list(...)) == 1) labs else NA)
  c(w, '')
}
with(d, 
     cat(c(
      paste(nrow(d), 'survey responders'), '',
      f(which), f(role), f(ordinal), f(which, role), f(ordinal, role)),
    sep='\n', file='results.md'))
