Outputting R loop content and outputting to knitr html

I am writing something where I run a function (or set) on a series of data frames using a loop. When I bind this in html (in RStudio), I would like to (a) see the loop variables and (b) create the result. So, if I have a piece:

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

      

The entered document will be displayed:

head (ISEQ0)

................ (title content)

head (ISEQ1)

.................. (title content)

etc. I've looked at stackoverflow, documentation and general web pages and looked at some links to loop loops (which seem to work), but I can't see anything about this type of loop. My goal here is to run a set of statistics on various datasets (I'm more familiar with loops than applicable, and I think it doesn't matter here), which I think is probably a fairly common use case.

As per the comment below, it looks like I have a short version as I expected:

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

      

In my full chunk, still something doesn't work (I only get the first iteration) But the full chunk doesn't. I tried cat and printing, I was just trying to get the first element (cor (ISEQData) to print, which didn't work, so I wondered if storing the outputs as variables (rather than trying to print "during a" computation ") would help - which apparently was not, they dont need to store everything like the next chunk does.I moved the functions in the short chunk one by one and I think everything after 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)
}

      

+3


source to share


1 answer


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
}
```

      

+2


source







All Articles