1

I am trying to make a reproducible "data dictionary" in RMarkdown to ease my job of describing the various undocumented data sets I work with. I've looked at the most related post here: Programmatically insert text, headers and lists with R markdown, but am running into problems. I have a data.frame that has the colnames from my dataset and a column of strings that describe each variable. When I knit my RMarkdown document, I get one formatted header for the first variable and the rest show up with the formatting hash marks (##) and the variable name, but not as a formatted header.

```{r, results = 'asis'}
varnames <- c("A", "B", "C")
vardesc <- c("A is this.", "B is this.", "C is this.")
df <- data.frame(varnames, vardesc)

for(i in 1:nrow(df)) {
cat("##", df$vars[i], "  \n")
cat("Description:  ", df$vardesc[i])
cat("  \n")
}
```

This gives me variable "A" is a formatted header only. It seems my rookie knowledge of functions could be to blame but I can't figure out what I am doing wrong.

My output is as follows (with A being formatted and the rest not formatted):

 ## A
 Description:
 A is this.
 ## B
 Description:
 B is this.
 ## C
 Description:
 C is this.

Any advice would be greatly appreciated. I'm open to other methods to do this if they exist.

Community
  • 1
  • 1
Aaron
  • 15
  • 1
  • 7

1 Answers1

1

Try this instead:

varnames <- c("A", "B", "C")
vardesc <- c("A is this.", "B is this.", "C is this.")
df <- data.frame(varnames, vardesc, stringsAsFactors = FALSE)

for(i in 1:nrow(df)) {
  cat("##", df$varnames[i], "\n")
  cat("Description:", "\n")
  cat(df$vardesc[i], "\n")
  cat("\n")
}

Output is:

## A 
Description: 
A is this. 

## B 
Description: 
B is this. 

## C 
Description: 
C is this. 
Gopala
  • 10,363
  • 7
  • 45
  • 77