R "openxlsx" package not maintaining formatting - r

R Version: 4.0.1 (2020-06-06), openxlsx Version: 4.2.4
I'm having formatting issues with the openxlsx package. When I load a workbook (loadWorkbook) and then save it again (saveWorkbook), formatting such as text color, cell alignment, "wrap text" status, and font are reset to default.
I've identified the issue down to a simple use case: If I create a sample sample_in.xlsx file with a table of random values and text colors, and then run the below code, all of the text color is lost. Am I doing something wrong?
library(openxlsx)
# Load Workbook
wb <- loadWorkbook("sample_in.xlsx")
# Save Workbook
saveWorkbook(wb, file = "sample_out.xlsx")
After this code, the sample_out.xlsx file contains no formatting. I'm not sure what's going on! It worked before I updated to the latest version of openxlsx.
Thanks!

Related

How to add png file to a worksheet in excel?

My original file is a kableExtra file and I want to insert it as image to excel workbook.
To do so I saved it to my wd as png. file. I wanted to create new workbook and add png image to worksheet.
I tried to do it with addImage function but I cannot install any package where the function is, although I have the newest R version installed.
Is there any way to save kableExtra file straight into excelworkbook as image? If not how can I do it with png. file without using function addImage?
Thank you for help

Is there a R function to add data from a df and append it to a sheet in excel

I have a R code that uses joins from various tables and finally have an output dataframe. I need this to be appended to a new worksheet in excel file that already has three sheets in it. I am on a mac and however I try the library(rjava) that is required for library(xlsx) wont load. Is there any other library (that doesnt need java) that I can use for this?
Edit to add: the existing excel sheet has graphs and charts in it

Is there a way to accelerate formatted table writing from R to excel?

I have a 174603 rows and 178 column dataframe, which I'm importing to Excel using openxlsx::saveWorkbook, (Using this package to obtain the aforementioned format of cells, with colors, header styles and so on). But the process is extremely slow, (depending on the amount of memory used by the machine it can take from 7 to 17 minutes!!) and I need a way to reduce this significantly (Doesn't need to be seconds, but anything bellow 5 min would be OK)
I've already searched other questions but they all seem to focus either in exporting to R (I have no problem with this) or writing non-formatted files to R (using write.csv and other options of the like)
Apparently I can't use xlsx package because of the settings on my computer (industrial computer, Check comments on This question)
Any suggestions regarding packages or other functionalities inside this package to make this run faster would be highly appreciated.
This question has some time ,but I had the same problem as you and came up with a solution worth mentioning.
There is package called writexl that has implemented a way to export a data frame to Excel using the C library libxlsxwriter. You can export to excel using the next code:
library(writexl)
writexl::write_xlsx(df, "Excel.xlsx",format_headers = TRUE)
The parameter format_headers only apply centered and bold titles, but I had edited the C code of the its source in github writexl library made by ropensci.
You can download it or clone it. Inside src folder you can edit write_xlsx.c file.
For example in the part that he is inserting the header format
//how to format headers (bold + center)
lxw_format * title = workbook_add_format(workbook);
format_set_bold(title);
format_set_align(title, LXW_ALIGN_CENTER);
you can add this lines to add background color to the header
format_set_pattern (title, LXW_PATTERN_SOLID);
format_set_bg_color(title, 0x8DC4E4);
There are lots of formating you can do searching in the libxlsxwriter library
When you have finished editing that file and given you have the source code in a folder called writexl, you can build and install the edited package by
shell("R CMD build writexl")
install.packages("writexl_1.2.tar.gz", repos = NULL)
Exporting again using the first chunk of code will generate the Excel with formats and faster than any other library I know about.
Hope this helps.
Have you tried ;
write.table(GroupsAlldata, file = 'Groupsalldata.txt')
in order to obtain it in txt format.
Then on Excel, you can simply transfer you can 'text to column' to put your data into a table
good luck

R XLSX and XLConnect packages - Formatting the workbood object created by XLConnect using xlsx package?

I have written a R code to create excel workbook and added the data to it using XLConnect package.
wb <- XLConnect::loadWorkbook(Name,create = TRUE)
and added some data frame to this file. Now, I want to access this XLConnect object wb from xlsx package and do some formatting like adding a border, font, wraptext and alignment on the dataframe inside the file. Is this possible?
Kindly let me know if anything is unclear or need more clarification.
It seems that it is not possible to use XLConnect and xlsx packages in the same R session or at least not in single package. I am using openxlsx package for formatting the excel file.

How can the Excel print area be set for a worksheet using the openxlsx R package?

Using R package openxlsx, how can the print area be set for a worksheet so that when the Excel worksheet created with openxlsx is opened, the print area is already defined?
I believe the way to solve things like this is to start with an excel workbook. Define the print area for this template, then loadWorkbook into R. Be careful with any other pagesetup stuff that you want to do in R, however. I can only get either excel's preexisting page setup working or R's, not both.
The "fitToWidth = TRUE" option of "pageSetup()" function from openxlsx enables printing of active contents of an Excel sheet to be printed within one page. You can also set "orientation = landscape" if your contents are too wide. The "scale = " option may be used manually to adjust the content print result.

Resources