我想将data.frame的内容显示为slidify中的表格。 我知道如何使用ascii库从data.frames创建Markdown表,但是当我尝试使用slidify,而不是在输出html中看到表时,我看到了一堆关于ascii表的内部结构的信息。 / p>
那么如何打印,例如在slidify中头(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
文件和Rmd
中的代码块中的最后一行slidify
时,我在幻灯片中看到了以下内容:
## <S4 Type Object>
## attr(,".xData")
## <environment: 0x03b904d8>
## attr(,"class")
## [1] "asciiTable"
## attr(,"class")attr(,"package")
## [1] "ascii"
答案 0 :(得分:6)
感谢Tyler Rinker
我设法使用xtable
---
```{r, results='asis'}
print(xtable(X[1:6,1:2]), type = "html")
```
答案 1 :(得分:3)
如果你想要降价,我强烈推荐我的pander包,它可以将R对象转换为不同的降价格式。快速举例:
加载包
library(pander)
使用默认的multi-line format 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
-----------------------------------------------
> 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 |
+-----------------------+---------------------------+
Simple sytle没有自动换行符:
> 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
PHP Extra Markdown/rmarkdown format:
> 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 |
有很多global or custom options来调整表格(如对齐,拆分设置,突出显示单元格等)。