如何将r中的格式化数据表复制到剪贴板

时间:2018-03-05 14:31:47

标签: r datatable shiny

此数据表有一个复制按钮:

library(DT)
iris2 = head(iris, 20)
# only show the Copy and Print buttons
datatable(
  iris2,
  extensions = 'Buttons', options = list(
    dom = 'Bfrtip',
    buttons = c('copy', 'print')
  )
)

复制按钮在剪贴板(mac os x)中给我这个:

    Sepal.Length    Sepal.Width Petal.Length    Petal.Width Species
1   5.1 3.5 1.4 0.2 setosa
2   4.9 3   1.4 0.2 setosa
3   4.7 3.2 1.3 0.2 setosa
4   4.6 3.1 1.5 0.2 setosa
5   5   3.6 1.4 0.2 setosa
6   5.4 3.9 1.7 0.4 setosa
7   4.6 3.4 1.4 0.3 setosa
8   5   3.4 1.5 0.2 setosa
9   4.4 2.9 1.4 0.2 setosa
10  4.9 3.1 1.5 0.1 setosa
11  5.4 3.7 1.5 0.2 setosa
12  4.8 3.4 1.6 0.2 setosa
13  4.8 3   1.4 0.1 setosa
14  4.3 3   1.1 0.1 setosa
15  5.8 4   1.2 0.2 setosa
16  5.7 4.4 1.5 0.4 setosa
17  5.4 3.9 1.3 0.4 setosa
18  5.1 3.5 1.4 0.3 setosa
19  5.7 3.8 1.7 0.3 setosa
20  5.1 3.8 1.5 0.3 setosa

但我想复制格式化的表格,就像我使用浏览器/鼠标选择,复制,粘贴(例如单词)时获得的表格。例如(但整个表格):

enter image description here

1 个答案:

答案 0 :(得分:0)

你可以使用 R-Markdown Knitr R-Markdown and Knitr Tutorial (Part 1))来创建一个带有 xtable 包的表输出

---
title: "How to copy a formatted datatable in r"
date: "March 5, 2018"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r, results="asis",}

library(xtable)

iris_table <- xtable(iris)

print(iris_table, type = 'html', include.rownames = FALSE)
```

你得到这样的输出 enter image description here