Javascript 如何在Rmarkdown html输出中获得复制按钮,以便将R代码中生成的表复制到剪贴板?
我正在尝试向一个html文件添加一个按钮,我正在使用RStudio和rmarkdown生成该文件,该文件将使用Javascript 如何在Rmarkdown html输出中获得复制按钮,以便将R代码中生成的表复制到剪贴板?,javascript,html,r,r-markdown,kableextra,Javascript,Html,R,R Markdown,Kableextra,我正在尝试向一个html文件添加一个按钮,我正在使用RStudio和rmarkdown生成该文件,该文件将使用kableExtra生成的表复制到剪贴板 作为演示,下面生成并显示一个表: library(dplyr) library(kableExtra) #making a dataframe into a table with kable df<-data.frame(Flavor=c("Raspberry","Lemonade","
kableExtra
生成的表复制到剪贴板
作为演示,下面生成并显示一个表:
library(dplyr)
library(kableExtra)
#making a dataframe into a table with kable
df<-data.frame(Flavor=c("Raspberry","Lemonade","Raspberry","Lemonade"),
Color=c("Blue","Pink","Red","Yellow"))
table_out<-df%>%
kable("html",align = 'clc')%>%
kable_styling(full_width = F,position="left",bootstrap_options = c("striped","bordered"))%>%
add_header_above(c("Colorful Stuff"=2))
table_out
库(dplyr)
图书馆(kableExtra)
#使用kable将数据帧制作成表
df%
kable_样式(全宽=F,位置=“左”,引导选项=c(“带条纹”、“带边框”))%>%
在上面添加标题(c(“彩色材料”=2))
表1
我发现这个代码,借用了这里的答案:,
允许我生成一个按钮:
<script type="text/javascript">
function selectElementContents(el) {
var body = document.body, range, sel;
if (document.createRange && window.getSelection) {
range = document.createRange();
sel = window.getSelection();
sel.removeAllRanges();
try {
range.selectNodeContents(el);
sel.addRange(range);
} catch (e) {
range.selectNode(el);
sel.addRange(range);
}
document.execCommand("copy");
} else if (body.createTextRange) {
range = body.createTextRange();
range.moveToElementText(el);
range.select();
range.execCommand("Copy");
}
}
功能选择元素内容(el){
var body=document.body,范围,sel;
if(document.createRange&&window.getSelection){
range=document.createRange();
sel=window.getSelection();
选择removeAllRanges();
试一试{
范围。选择节点内容(el);
选择添加范围(范围);
}捕获(e){
范围。选择节点(el);
选择添加范围(范围);
}
文件。执行命令(“副本”);
}else if(body.createTextRange){
range=body.createTextRange();
范围。移动到元素文本(el);
range.select();
range.execCommand(“复制”);
}
}
上述问题(据我所知)是我没有正确地引用使用kableExtra
生成的表。当我在呈现的html文件中单击“复制”按钮时,它不会将table\u复制到剪贴板
如何给我的kableExtra
表格一个Id并正确引用它,以便我制作的“复制”按钮将表格复制到剪贴板?
编辑:以下是渲染文件的外观。我对外观很满意,但希望复制按钮能够正常工作:
我通过将id属性添加到我的kableExtra
表中,然后在复制按钮中引用id,实现了这一点
看看这里的10.1.11:,
您可以使用table.attr
添加属性
您将看到我将kableExtra
部分更改为包含table.attr=“id=mytab”
,这允许在R代码块之外引用
我的全部功能代码是:
---
title: "Demo"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(dplyr)
library(kableExtra)
```
```{r}
#making a dataframe into a table with kable
df<-data.frame(Flavor=c("Raspberry","Lemonade","Rasp2berry","Lemonade"),
Color=c("Blue","Pink","Red","Yellow"))
table_out<-df%>%
kable("html",align = 'clc', table.attr = "id=mytab")%>%
kable_styling(full_width = F,position="left",bootstrap_options =
c("striped","bordered"))%>%
add_header_above(c("Colorful Stuff"=2))
table_out
```
<script type="text/javascript">
function selectElementContents(el) {
var body = document.body, range, sel;
if (document.createRange && window.getSelection) {
range = document.createRange();
sel = window.getSelection();
sel.removeAllRanges();
try {
range.selectNodeContents(el);
sel.addRange(range);
} catch (e) {
range.selectNode(el);
sel.addRange(range);
}
document.execCommand("copy");
} else if (body.createTextRange) {
range = body.createTextRange();
range.moveToElementText(el);
range.select();
range.execCommand("Copy");
}
}
---
标题:“演示”
输出:html\u文档
---
```{r设置,include=FALSE}
knitr::opts_chunk$set(echo=TRUE)
图书馆(dplyr)
图书馆(kableExtra)
```
```{r}
#使用kable将数据帧制作成表
df%
kable_样式(全宽=F,位置=“左”,引导选项=
c(“带条纹”、“带边框”))%>%
在上面添加标题(c(“彩色材料”=2))
表1
```
功能选择元素内容(el){
var body=document.body,范围,sel;
if(document.createRange&&window.getSelection){
range=document.createRange();
sel=window.getSelection();
选择removeAllRanges();
试一试{
范围。选择节点内容(el);
选择添加范围(范围);
}捕获(e){
范围。选择节点(el);
选择添加范围(范围);
}
文件。执行命令(“副本”);
}else if(body.createTextRange){
range=body.createTextRange();
范围。移动到元素文本(el);
range.select();
range.execCommand(“复制”);
}
}
如果你有更好的解决方案或更有力的解释,请仍然考虑张贴答案。我的解决方案是暴力的结果,而不是丰富的知识。
也许这有帮助:
---
title: "Demo"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(dplyr)
library(kableExtra)
```
```{r}
#making a dataframe into a table with kable
df<-data.frame(Flavor=c("Raspberry","Lemonade","Rasp2berry","Lemonade"),
Color=c("Blue","Pink","Red","Yellow"))
table_out<-df%>%
kable("html",align = 'clc', table.attr = "id=mytab")%>%
kable_styling(full_width = F,position="left",bootstrap_options =
c("striped","bordered"))%>%
add_header_above(c("Colorful Stuff"=2))
table_out
```
<script type="text/javascript">
function selectElementContents(el) {
var body = document.body, range, sel;
if (document.createRange && window.getSelection) {
range = document.createRange();
sel = window.getSelection();
sel.removeAllRanges();
try {
range.selectNodeContents(el);
sel.addRange(range);
} catch (e) {
range.selectNode(el);
sel.addRange(range);
}
document.execCommand("copy");
} else if (body.createTextRange) {
range = body.createTextRange();
range.moveToElementText(el);
range.select();
range.execCommand("Copy");
}
}
<input type="button" value="Copy" onclick="selectElementContents( document.getElementById('mytab') );">