在R Markdown中将数据框显示为表格 [英] Display a data frame as table in R Markdown

查看:2650
本文介绍了在R Markdown中将数据框显示为表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Knitr中,我想使用kable包添加一个(小的)数据框作为表:

 `` (dset1,format =html)
dset1 < - read.csv(/ home / runlevel0 / edx_statsAP / mod1d_cor_dset01.csv)

knitr :: kable
[...]
```

相当丑陋的表,而格式降价返回一个不错的表,但跨越整个页面。



我找到了knitr的手册,但没有太多用处,因为它没有涵盖每种格式的额外格式选项。因此,我的问题:我怎样才能改变一个针织表的大小,甚至更好,我可以从中获得这些信息吗?

解决方案

一般的方法是使用自己的自定义CSS,并将其包含在文档开始处的YAML中。 / p>

实际上,您可以在文档中对其进行排序,但是我建议您在文档之外编辑您的CSS并从那里开始工作。



下面是一个简单的例子: $ b

  --- 
title:Test
date:2015年10月24日
输出:
html_document:
css:mystyle.css
---

$```{r,results ='asis'}
writeLines(td,th {padding:6px} th {background-color:brown; color:white; border:1px solid white;} td {颜色:brown; border:1px solid brown},con =mystyle.css)
dset1< - head(ToothGrowth)
knitr :: kable(dset1,format =html)
```

这应该是:


  1. 用你的re创建一个名为mystyle.css的文件levant CSS样式。
  2. 生成如下所示的内容。


In Knitr I want to add a (small) data frame as a table using the kable package:

```{r kable}
dset1 <- read.csv("/home/runlevel0/edx_statsAP/mod1d_cor_dset01.csv")

knitr::kable(dset1,format="html")
[...]
```

This returns a compact but rather ugly table, while the format "markdown" returns a nice table but spanning the whole page.

I have found the knitr "manual" but it's not too useful as it does not cover the extra formatting options for each format.

Hence my question: How can I change the size of a knitr table or even better, were can I get this information from?

解决方案

The general approach would be to use your own custom CSS and include that in the YAML at the start of the document.

You can actually sort of do this from within your document, but I would suggest editing your CSS outside of the document and working from there.

Here's a minimal example:

---
title: "Test"
date: "24 October 2015"
output: 
  html_document:
    css: mystyle.css
---

```{r, results='asis'}
writeLines("td, th { padding : 6px } th { background-color : brown ; color : white; border : 1px solid white; } td { color : brown ; border : 1px solid brown }", con = "mystyle.css")
dset1 <- head(ToothGrowth)
knitr::kable(dset1, format = "html")
```

This should:

  1. Create a file named "mystyle.css" with your relevant CSS styling.
  2. Produce something that looks something like the following.

这篇关于在R Markdown中将数据框显示为表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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