0

This is a small sample of many separate analyses I want to run together, flagging each analysis with a heading generated in the function. The first heading outputs as expected, but the headings generated by the function do not. How can I get the headings generated by the function to format as html?

R version 3.3.2 (2016-10-31)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X El Capitan 10.11.6

RStudio 1.0.136

knitr 1.15.1
```{r initialize, echo=F, comment=NA}

htmPrint = function(htm) {
    structure(htm, class='knit_asis')
    }

doAnalysis = function(dat, depVar, indVar) {

    print(htmPrint(paste0('<h3>AusMCP1 on ', indVar, '</h3>')))
    eval(parse(text=paste0('print(summary(lm(', depVar, '~', indVar, ', data=dat)))')))
    print(htmPrint('<hr>'))
    }

demoData = data.frame(dep1=rnorm(100), dep2=rnorm(100), ind1=runif(100), ind2=runif(100), ind3=runif(100))

varDep = names(demoData)[1:2]
varInd = names(demoData)[3:5]
```

This is a small sample of many separate analyses I want to run together, flagging each analysis with a heading generated in the function.

I could use *results=asis* in the chunk command, but it produces
unwanted formatting and extraneous output in the analysis output.

```{r doAnalyses}
htmPrint('<h2>Begin analyses</h2>')

for (k in 1:length(varDep)) for (i in 1:length(varInd)) doAnalysis(demoData, varDep[k], varInd[i])
```

1 Answers1

0

I think you'll have to use results = 'asis' and just alter you function to get the output that you want. I'm going to use some methods referenced in a previous question that make use of the package pander.

You don't have to use your htmPrint function as you can get the same functionality with cat and html tags. Then you'd just use print_lm instead of print and remove the summary, since you're getting nice tabular output.

```{r initialize, echo=F, comment=NA}

library(pander)

#Use the pander print method to print the model tables.  
#You could use other package print methods as well.

print_lm <- function (x, ...) UseMethod("pander")

doAnalysis = function(dat, depVar, indVar) {

    cat('<h3>AusMCP1 on ', indVar, '</h3>')
    eval(parse(text=paste0('print_lm(summary(lm(', depVar, '~', indVar, ', data=dat)))')))
    cat('<hr>')
    }

demoData = data.frame(dep1=rnorm(100), dep2=rnorm(100), ind1=runif(100), ind2=runif(100), ind3=runif(100))

varDep = names(demoData)[1:2]
varInd = names(demoData)[3:5]
```

I  use *results=asis* and changed the output of your doAnalysis function to create formatted output.

```{r doAnalyses, echo = FALSE, results='asis'}
cat('<h2>Begin analyses</h2>')

for (k in 1:length(varDep)) for (i in 1:length(varInd)) doAnalysis(demoData, varDep[k], varInd[i])
```

enter image description here

You could also do all of the modelling beforehand, and use the broom package to tidy all of your model summaries to your liking, then print them out using kable() or tabular print method of your choice.

Jake Kaupp
  • 7,892
  • 2
  • 26
  • 36
  • Doesn't quite do what I need. I'd like to display the summary info. Using pander appears to generate a single line for the intercept in big bold letters, a line of headings with a nonbreakable space character, and the regression coefficient that doesn't line up with the headings. – user7528768 Feb 07 '17 at 16:00
  • I'm confused as to what you mean about the formatting and alignment in `pander`, seeing as the above screenshot is the code output using `pander`. If you want to have the full text version of the summary, you may have to define your own print method to do so in the appropriate format. – Jake Kaupp Feb 07 '17 at 16:31
  • My css is generating output that looks different from the output you displayed. I was hoping for a simpler way to do this without redefining my css and using pander. Isn't there some way to format html in the output when it's generated inside a function? – user7528768 Feb 07 '17 at 17:12
  • You'd have to go deep under the hood with `summary` for that. What you could do is right a function using `coefs()` or `broom` functions to get the variables then output an html table in the format you desire. – Jake Kaupp Feb 07 '17 at 17:23