5

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

1 Answer 1

2

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
}
```
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @agstudy ! I've just had a look at the print and cat help, is print something I could wrap around everything in the loop e.g. for(i in lis){ print( { head(i) nrow(i) ncol(i) }) }
use something like : print(paste(nrow(i) ,ncol(i))) where i is your i not mine.
Ah! I realise now this isn't really about knitr as such, those values wouldn't ever be printed in a loop so of course they aren't in the knitted doc either. In my full code block I've tried both the print and cat method on a single line producing output and I get the first iteration printed, but not the subsequent ones. This works as I'd expect: for(ISEQData in ISEQList){ print(head(ISEQData)) print(cor(ISEQData)) } But in my longer code chunk/loop it doesn't. Just playing a bit more (accidentally pressed enter on this).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.