corrado
corrado

Reputation: 135

Description and plot for every variable in R Markdown

I have a dataframe dataof nobservations of several numeric and factor variables. I would like to produce a html report in which class and describe are reported and a histogram (qplotor ggplot) is plotted for every variable.

  1. How can I do that?
  2. Is it possible in R Markdown to produce an automatic header preceding every variable analysis?

Thank you for your help.

Corrado

Upvotes: 0

Views: 974

Answers (1)

juba
juba

Reputation: 49063

You can put a loop in your R chunks in Markdown files. Something like that for example :

```{r, echo=FALSE}
library(ggplot2)
```

This is an introductory sentence with absolutely no interest.

```{r, results="asis", eval=TRUE, echo=FALSE}
data(cars)
for (varname in names(cars)) {
  var <- cars[,varname]
  cat(paste0("<h2>",varname,"</h2>"))
  cat(paste0("Class : <pre>",class(var),"</pre>"))
  cat("Summary : <pre>")  
  print(summary(var))
  cat("</pre>")  
  if (is.numeric(var)) print(qplot(var, binwidth=diff(range(var))/30))
}
```

This is an astonishing conclusion. 

Which gives the following result : http://rpubs.com/juba/mdloop

Upvotes: 5

Related Questions