I am trying to add a button to an html file that I'm producing using RStudio and rmarkdown that copies a table produced with kableExtra
to the clipboard.
As a demo, the following produces and displays a table:
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
I found that this code, borrowed from an answer here: https://stackoverflow.com/a/42210996/9731173, allows me to produce a button:
<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('table_out') );">
The issue with the above (so far as I can tell) is that I'm not referring to the table I produced with kableExtra
correctly. When I click the Copy button in my rendered html file, it doesn't copy table_out
to the clipboard.
How can I give my kableExtra
table an Id and correctly refer to it so that the "Copy" button I made copies the table to the clipboard?
Edit: Here's what the rendered file looks like. I'm happy with the appearance, but hope to make the copy button functional: