4

我想在 slidify 中将 data.frame 的内容显示为表格。我知道如何使用 ascii 库从 data.frames 创建 Markdown 表,但是当我尝试将它与 slidify 一起使用时,我没有看到输出 html 中的表,而是看到了一堆关于 ascii 表内部结构的信息。

那么如何在 slidify 中打印例如 head(some.data.frame)?

编辑:

事实上,我想在 CRAN 任务视图中显示一个视图表,现在我在 Markdown 中手动输入了该表:

Views | Content
--------|--------
Bayesian| Bayesian Inference
ChemPhys| Chemometrics and Computational Physics
ClinicalTrials| Clinical Trial Design, Monitoring, and Analysis

我想从ctv包中自动创建这个表。我已经在 data.frame 中收集了我需要的信息:

library(ctv)
list.of.views <- available.views()
X <- data.frame(View=NA,Description=NA)
for(i in 1:length(list.of.views))
{
  X[i,1] <- list.of.views[[i]]$name
  X[i,2] <- list.of.views[[i]]$topic
}
head(X)

这导致

                   View                                     Description
1              Bayesian                              Bayesian Inference
2              ChemPhys          Chemometrics and Computational Physics
3        ClinicalTrials Clinical Trial Design, Monitoring, and Analysis
4               Cluster        Cluster Analysis & Finite Mixture Models
5 DifferentialEquations                          Differential Equations
6         Distributions                       Probability Distributions

我使用ascii包进行降价

library(ascii)
print(ascii(X[1:6,1:2]), type = 'pandoc')

在 R 终端中显示了这一点:

    **View**                **Description**                                  
 --- ----------------------- -------------------------------------------------
 1   Bayesian                Bayesian Inference                               
 2   ChemPhys                Chemometrics and Computational Physics           
 3   ClinicalTrials          Clinical Trial Design, Monitoring, and Analysis  
 4   Cluster                 Cluster Analysis & Finite Mixture Models         
 5   DifferentialEquations   Differential Equations                           
 6   Distributions           Probability Distributions                        
 --- ----------------------- -------------------------------------------------

Warning messages:
1: In rep(rownames, length = nrow(x)) :
  'x' is NULL so the result will be NULL
2: In rep(colnames, length = ncol(x)) :
  'x' is NULL so the result will be NULL

但是当我的文件和它print的代码块中的最后一行时,我在幻灯片中看到以下内容:Rmdslidify

## <S4 Type Object>
## attr(,".xData")
## <environment: 0x03b904d8>
## attr(,"class")
## [1] "asciiTable"
## attr(,"class")attr(,"package")
## [1] "ascii"
4

2 回答 2

6

感谢Tyler Rinker我设法创建了我想要使用的表xtable

---
```{r, results='asis'}
print(xtable(X[1:6,1:2]), type = "html")
```
于 2013-09-16T09:12:56.423 回答
3

如果你想要 markdown,我强烈推荐我的pander包,它可以将 R 对象转换为不同的 markdown 格式方言。快速示例:

  1. 加载包

    library(pander)
    
  2. 使用Pandoc的默认多行格式在您的演示数据上创建一个降价表:

    > pander(X[1:6,1:2])
    
    -----------------------------------------------
            View                 Description       
    --------------------- -------------------------
          Bayesian           Bayesian Inference    
    
          ChemPhys            Chemometrics and     
                            Computational Physics  
    
       ClinicalTrials      Clinical Trial Design,  
                          Monitoring, and Analysis 
    
           Cluster        Cluster Analysis & Finite
                               Mixture Models      
    
    DifferentialEquations  Differential Equations  
    
        Distributions     Probability Distributions
    -----------------------------------------------
    
  3. 网格格式

    > pander(X[1:6,1:2], style = 'grid')
    
    +-----------------------+---------------------------+
    |         View          |        Description        |
    +=======================+===========================+
    |       Bayesian        |    Bayesian Inference     |
    +-----------------------+---------------------------+
    |       ChemPhys        |     Chemometrics and      |
    |                       |   Computational Physics   |
    +-----------------------+---------------------------+
    |    ClinicalTrials     |  Clinical Trial Design,   |
    |                       | Monitoring, and Analysis  |
    +-----------------------+---------------------------+
    |        Cluster        | Cluster Analysis & Finite |
    |                       |      Mixture Models       |
    +-----------------------+---------------------------+
    | DifferentialEquations |  Differential Equations   |
    +-----------------------+---------------------------+
    |     Distributions     | Probability Distributions |
    +-----------------------+---------------------------+
    
  4. 没有自动换行符的简单样式:

    > pander(X[1:6,1:2], style = 'simple', split.cells = Inf)
    
            View                            Description                  
    --------------------- -----------------------------------------------
          Bayesian                      Bayesian Inference               
          ChemPhys            Chemometrics and Computational Physics     
       ClinicalTrials     Clinical Trial Design, Monitoring, and Analysis
           Cluster           Cluster Analysis & Finite Mixture Models    
    DifferentialEquations             Differential Equations             
        Distributions                Probability Distributions         
    
  5. 以及PHP Extra Markdown/rmarkdown 格式

    > pander(X[1:6,1:2], style = 'rmarkdown', split.cells = Inf)
    
    |         View          |                   Description                   |
    |:---------------------:|:-----------------------------------------------:|
    |       Bayesian        |               Bayesian Inference                |
    |       ChemPhys        |     Chemometrics and Computational Physics      |
    |    ClinicalTrials     | Clinical Trial Design, Monitoring, and Analysis |
    |        Cluster        |    Cluster Analysis & Finite Mixture Models     |
    | DifferentialEquations |             Differential Equations              |
    |     Distributions     |            Probability Distributions            |
    

有许多全局或自定义选项可以调整表格(如对齐、拆分设置、突出显示单元格等)

于 2013-09-16T10:38:11.293 回答