I'd like to create button (or a selectable table) in an rmarkdown file that allows a user to copy a table and paste it into word or excel with some pre-defined formatting.
Is there a way to produce:
- A copy function that preserves table formatting, especially borders to cells
- A copy function that captures the entirety of a table
Using this very simple dataframe:
data<-data.frame(Variable=c('Amount','Age','Happiness','Favorite Color'),
Value=c(15,25,7,'Yellow'))
I want a user to be able to click a button in the html file such that when they paste into excel or word, they see:
or
depending on the formatting specified.
As it stands, I can make a table that has cell borders with kable
,
data%>%
kable("html",align = 'clc')%>%
kable_styling(full_width = F,position="left",bootstrap_options =
c("striped","bordered"))
This table, in the html file generated using rmarkdown, displays cell borders and has acceptable spacing and clarity (columns are wide enough to display full column headers, and the gird clearly defines cells.
When I highlight the table and paste it into Excel, I get:
An unsatisfactory result.
Producing a table with DT
, I use:
datatable(data,extensions='Buttons',rownames=FALSE,filter=c("top"),options=list(dom='Bfrtip',buttons=c('csv','copy','excel')))
This produces a table with a CSV, Copy, and Excel button.
The result of using the Copy button and pasting into Excel is:
A user has data that does paste, but lacks any formatting (and puts the title from the file two rows above the datatable itself?)
Is there a way to modify the code for the DT
table's Copy button to include some specified formatting, such as a missing argument that gives an option to copy some formatting to clipboard as well? Or a way to produce a standalone button that I can store an excel-formatted table behind so that a user has an way to copy my table in an excel-friendly, formatted form?
Note: I don't want to write to an excel file from R. There seem to be many options for formatting tables and writing to Excel with openxlsx
and solutions here: Write from R into template in excel while preserving formatting , but this does not answer my question.
Edit: There seems to be the ability to add the formatting to the Excel button, mentioned here: https://datatables.net/extensions/buttons/examples/html5/excelBorder.html
This gives me hope that perhaps the .attr()
specification of the formatting can somehow be added to the copy
button.
As a first step in trying to test whether or not I can even get this intermediate step functioning, I tried:
jscode<-"function ( xlsx ){var sheet = xlsx.xl.worksheets['sheet1.xml']; $('row c[r*=10]', sheet).attr( 's', '25' ); }"
datatable(data1,class='cell-border',extensions='Buttons',rownames=FALSE,filter=c("top"),options=list(dom='Blfrtip',buttons=list(list(extend='excel',customize=jscode))))
The result is a spinning-button-of-nothingness.
When I try:
library(jsonlite)
datatable(data1,class='cell-border',extensions='Buttons',rownames=FALSE,filter=c("top"),options=list(dom='Blfrtip',buttons=list(list(extend='excel',customize=fromJSON(jscode)))))
I receive an error:
Since the solution to this question seems to depend on some javascript (at least if the answer will be based on datatable
, I've added the javascript tag.
I've posted a demo report at http://kelleranalytics.com/R/Demo.html and find that none of these various methods work.
from How can I add a table to my rmarkdown html output that will paste into Excel with formatting?
No comments:
Post a Comment