在knitr html中输出R循环内容和输出

时间:2022-09-20 06:12:30

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:

我在写一些东西,在其中我使用循环在许多数据aframes上运行一个函数(或设置)。当我将它编织到html(在RStudio中)时,我希望能够(a)看到循环变量,(b)创建的输出。如果我有一大块

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

The knited document would display:

编织文件将显示:

head(ISEQ0)

头(ISEQ0)

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

................(内容)

head(ISEQ1)

头(ISEQ1)

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

..................(内容)

and so on. I've had a look on *, 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.

等等。我查看了*、文档和一般的web搜索,看到了一些对情节循环的引用(这似乎很有用),但是我看到的这种循环没有任何内容。我在这里的目的是在不同的数据集上运行一组统计数据(我更熟悉循环而不是apply,并且我猜在这里没有什么不同),我认为这可能是一个相当常见的用例。

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.

仍然有一些东西不能在我的整个块中工作(我只有第一次迭代),但是整个块不是。我试着猫和打印,我只是试图让第一个元素(软木(ISEQData)打印,没有工作,所以我想知道如果存储输出变量(而不是试图打印”在“计算)将帮助——它似乎没有,他们不都需要存储以下块一样。我已经把函数一个一个地移动到短块中,我认为vss之后的所有事情都是有问题的……但我不明白为什么。

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 个解决方案

#1


2  

You should use print or cat to force output :

您应该使用打印或cat来强制输出:

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

#1


2  

You should use print or cat to force output :

您应该使用打印或cat来强制输出:

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

相关文章