在Rstudio中的RMarkdown中循环生成pander表 [英] Generating pander tables in a loop in RMarkdown in Rstudio

查看:305
本文介绍了在Rstudio中的RMarkdown中循环生成pander表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  

使用类似以下代码的代码生成一组表格: ```{r tables,echo = TRUE,eval = TRUE}
require(reshape2);要求(pander)
数据(mtcars)
数据(AirPassengers)

dta_lst < - list(mtcars,AirPassengers)
for(i in 1:length(dta_lst)){
pander(melt(head(dta_lst [[i]],n = 2),varnames = c(Something),
value.name =Some other else ),
caption = paste(Some table for:,class(dta_lst [[i]]))
}
```


  ----------------------- ---- 
变量另外
---------- ----------------
mpg 21

mpg 21

cyl 6

cyl 6

disp 160

disp 160

hp 110

hp 110

drat 3.9

drat 3.9

wt 2.62
$ b wt 2.875
$ b $ q qsec 16.46

qsec 17.02

vs 0

vs 0

am 1

am 1

齿轮4

齿轮4

碳水化合物4

碳水化合物4
-------------- -------------

表:某些表为:data.frame


-------- --------
其他
----------------
112

118
----------------

表:一些表格:ts

当我试图在Rstudio中编写代码时,pander表不会出现:



当然,如果没有循环, pander 命令就可以正常工作,并生成整齐排列在HTML文档中的表格。在for循环中,除非使用 print(x),否则不会有输出屏幕 code $函数。

$ for(i in 1:4){i} 不显示任何东西


$ b $ for(i in 1:4){print(i)} 显示数字1 2 3和4解决方案:在FOR循环中构造表(使用knitt)并将其分配给一个变量。然后用 print()函数打印出这个变量。记得。您必须在var table之前和之后添加空行:在 print()中使用粘贴功能


I've a set of tables that I'm generating with use of the code similar to the snippet below:

```{r tables, echo=TRUE, eval=TRUE}
require(reshape2); require(pander)
data("mtcars")
data("AirPassengers")

dta_lst <- list(mtcars, AirPassengers)
for (i in 1:length(dta_lst)) {
    pander(melt(head(dta_lst[[i]], n = 2), varnames = c("Something"), 
            value.name = "Something else"), 
           caption = paste("Some table for: ", class(dta_lst[[i]])))
}
```

When I run the code it produces the desired output (naturally, the provided example makes little sense, in my proper data I melt the data in sensible manner):

---------------------------
 variable   Something else 
---------- ----------------
   mpg            21       

   mpg            21       

   cyl            6        

   cyl            6        

   disp          160       

   disp          160       

    hp           110       

    hp           110       

   drat          3.9       

   drat          3.9       

    wt           2.62      

    wt          2.875      

   qsec         16.46      

   qsec         17.02      

    vs            0        

    vs            0        

    am            1        

    am            1        

   gear           4        

   gear           4        

   carb           4        

   carb           4        
---------------------------

Table: Some table for:  data.frame


----------------
 Something else 
----------------
      112       

      118       
----------------

Table: Some table for:  ts

When I attempt to knit the code in Rstudio the pander tables do not appear:

Naturally, without the loop the pander command works just fine and generates the tables that are neatly knitted into a HTML document.

解决方案

In the for loop there is not 'output screen' unless you use the print(x) function.

for (i in 1:4) { i } does not display anything

for (i in 1:4) {print(i)} displays the Numbers 1 2 3 and 4

Solution: In the FOR loop construct the table (using knitt) and assign it into a variable. Then print out this variable using print() function. Remember. You Must add blank lines after and before the var table: use paste function inside print()

这篇关于在Rstudio中的RMarkdown中循环生成pander表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆