Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output R loop content and output in knitr html

I'm writing something where I run a function (or set) on a number of dataframes using a loop. When I knit this to html (in RStudio) I'd like to be able to (a) see the loop variables, and (b) the output created. So if I have a chunk:

```{r}
dflist <- list(ISEQ0=ISEQ0,ISEQ1=ISEQ1,ISEQ2=ISEQ2,ISEQ3=ISEQ3)
for(i in dflist){
 head(i)
}
```

The knited document would display:

head(ISEQ0)

................(head content)

head(ISEQ1)

..................(head content)

and so on. I've had a look on stackoverflow, the documentation and general websearches and see some references to plot loops (which seem to work) but nothing on this kind of loop as far as I see. My purpose here is to run a set of statistics on different datasets (I'm more familiar with loops than apply, and guess it doesn't make a difference here) which I think is probably a fairly common use-case.

As per comment below, I seem to have a short version working as I'd expect:

ISEQList <- list(ISEQ0=ISEQ0,ISEQ1=ISEQ1,ISEQ2=ISEQ2,ISEQ3=ISEQ3)
for(ISEQData in ISEQList){
print(head(ISEQData))
print(cor(ISEQData))
}

There's still something not working in my full chunk (I only get the first iteration) But the full chunk isn't. I've tried cat and print, I was just trying to get the first element (cor(ISEQData) to print, which wasn't working, so I wondered if storing outputs as variables (rather than trying to print 'during' calculation) would help - which it doesn't seem to have, they don't all need storing as the below chunk does though. I've been moving the functions into the short chunk one by one and I think everything after the vss is problematic...but I don't understand why.

for(ISEQData in ISEQList){
  n <- n +1
a <- cor(ISEQData)
###################################Explore factor options##############
b <- vss(ISEQData,n=9,rotate="oblimin",diagonal=F,fm="ml") 
c <- EFA.Comp.Data(Data=ISEQData, F.Max=9, Graph=T) #uses EFA Comparison Data.R
d <- fa.parallel(ISEQData) 
# Determine Number of Factors to Extract using N Factors library
ev <- eigen(cor(ISEQData)) # get eigenvalues
ap <- parallel(subject=nrow(ISEQData),var=ncol(ISEQData),rep=100,cent=.05)
nS <- nScree(x=ev$values, aparallel=ap$eigen$qevpea)
pnS <- plotnScree(nS) 
#######################################################
for(x in 2:5){
  assign(paste0("fitml",x,"ISEQ",n),fa(r = ISEQData, nfactors = x, rotate = "oblimin",     fm = "ml",residuals=T))
}
e<-fitml2$loadings
f<-fitml3$loadings
m<-fit # print results 
p<-factor.scores(ISEQData,fit)
q<-factor.stats(f=fit)
r<-fa.diagram(fit)
}
like image 492
sjgknight Avatar asked Nov 01 '22 11:11

sjgknight


1 Answers

You should use print or cat to force output :

```{r}
for(i in seq_along(dflist)){
 print(paste('head data set:' , names(dflist)[i]))    ## sub title
 print(head(dflist[[i]]))                             ## content 
 cat(rep("*",20),'\n')                                ##  separator
}
```
like image 122
agstudy Avatar answered Nov 09 '22 14:11

agstudy