| 1 |
\name{format_md} |
| 2 |
\alias{format_md} |
| 3 |
\alias{format_md.codebook} |
| 4 |
\alias{format_md.codebookEntry} |
| 5 |
\title{ |
| 6 |
Format Codebooks as Markdown |
| 7 |
} |
| 8 |
\description{ |
| 9 |
\code{format_md} is for showing objects in a convenient way in Markdown |
| 10 |
format. Can be included to Rmarkdown file with the \code{cat()} function and the |
| 11 |
\code{results='asis'} code block option. The following example should be runned |
| 12 |
in a Rmd file with different output formats. |
| 13 |
} |
| 14 |
\usage{ |
| 15 |
\method{format_md}{codebook}(x, \dots) |
| 16 |
\method{format_md}{codebookEntry}(x, name = "", add_rules = TRUE, \dots) |
| 17 |
} |
| 18 |
\arguments{ |
| 19 |
\item{x}{a "codebook" or "codebookEntry" object} |
| 20 |
\item{name}{a string; the variable name} |
| 21 |
\item{add_rules}{a boolean value; if TRUE adds a horizontal rules before and after the title} |
| 22 |
\item{\dots}{further arguments, passed to other functions} |
| 23 |
} |
| 24 |
\value{ |
| 25 |
\code{format_md} character string with code suitable for inclusion into a Markdown-file. |
| 26 |
} |
| 27 |
\examples{ |
| 28 |
library(memisc) |
| 29 |
|
| 30 |
Data1 <- data.set( |
| 31 |
vote = sample(c(1,2,3,8,9,97,99),size=300,replace=TRUE), |
| 32 |
region = sample(c(rep(1,3),rep(2,2),3,99),size=300,replace=TRUE), |
| 33 |
income = exp(rnorm(300,sd=.7))*2000 |
| 34 |
) |
| 35 |
|
| 36 |
Data1 <- within(Data1,{ |
| 37 |
description(vote) <- "Vote intention" |
| 38 |
description(region) <- "Region of residence" |
| 39 |
description(income) <- "Household income" |
| 40 |
foreach(x=c(vote,region),{ |
| 41 |
measurement(x) <- "nominal" |
| 42 |
}) |
| 43 |
measurement(income) <- "ratio" |
| 44 |
labels(vote) <- c( |
| 45 |
Conservatives = 1, |
| 46 |
Labour = 2, |
| 47 |
"Liberal Democrats" = 3, |
| 48 |
"Don't know" = 8, |
| 49 |
"Answer refused" = 9, |
| 50 |
"Not applicable" = 97, |
| 51 |
"Not asked in survey" = 99) |
| 52 |
labels(region) <- c( |
| 53 |
England = 1, |
| 54 |
Scotland = 2, |
| 55 |
Wales = 3, |
| 56 |
"Not applicable" = 97, |
| 57 |
"Not asked in survey" = 99) |
| 58 |
foreach(x=c(vote,region,income),{ |
| 59 |
annotation(x)["Remark"] <- "This is not a real survey item, of course ..." |
| 60 |
}) |
| 61 |
missing.values(vote) <- c(8,9,97,99) |
| 62 |
missing.values(region) <- c(97,99) |
| 63 |
}) |
| 64 |
|
| 65 |
codebook_data <- codebook(Data1) |
| 66 |
|
| 67 |
codebook_md <- format_md(codebook_data, digits = 2) |
| 68 |
|
| 69 |
writeLines(codebook_md) |
| 70 |
|
| 71 |
\dontrun{ |
| 72 |
writeLines(codebook_md,con="codebook-example.md") |
| 73 |
} |
| 74 |
|
| 75 |
} |