How to add png file to a worksheet in excel? - r

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

Related

Writing new data to an existing excel file that has an XML map attached, without losing the XML data in R

I am trying to write to an excel file that needs to be uploaded somewhere. The target software creates an excel file which has an XML map attached to it. I recreated the entire file structure in R using code, but any time I try to write to that excel file, i think R actually deletes the old file and creates a new one instead, because the XML map is gone the moment I start writing any data to it. Loading up the workbook also doesn't seem to bring in the xml map, only the workbook data and sheets.
Is there a way to write data to this existing file within R (or python) without losing the XML map? Now i need to generate a file and manually copy paste the data into the other excel file.
I've been trying with xlsx, readxl, xml2 packages.
In the past Ive deal with a similar problem. To my knowledge, almost all the R packages that interact with excel replace the entire file with a new one. Except the openxlsx package. You can replace specific sheets, and range of cells, whitout touching the rest (data, styling , etc..). One last comment is that I dont know much about XLM maps, but maybe you are lucky.
Here is the vignette:
https://cran.r-project.org/web/packages/openxlsx/vignettes/Introduction.html
Hope it helps

R "openxlsx" package not maintaining formatting

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!

When trying to export a table created in R using the formattable() function I get a blank file

When trying to export a table created using formattable() in R using the png(), jpeg(), pdf(), etc. commands I only get a blank file with the correct title, but no content or file type.
I've created basic graphs and tried to export them multiple times with the various export to png/jpeg/pdf commands and it works so I'm not sure why the table is any different.
# Open png file for exporting
png(file = "Family_Table_Output")
# Create table
formattable(my_data)
# Close png file
dev.off()
I expected there to be a png file in the working directory that when opened displays the chart. What happens in actuality is that the table appears in the table/graph viewer and a file with no type (just says file) is created in the working directory that when opened is blank.

Importing .xls file that is saved as *.htm, *.html as it is saved on the backend

I have a requirement where I have to import an .xls file which is saved as .*htm, .*html.
How do we load this inside R in a data frame. The data is present in Sheet1 starting from Row Number 5. I have been struggling with this by trying to load it using xlsx package and readxl package. But neither of them worked, because the native format of the file is different.
I can't edit and re-save the file manually as .xlsx, as it cannot be automated.
Also to note, saved it as a .xlsx file and it works fine. But that's not what I need.
Kindly help me with this.
Try the openxlsx package and its function read.xlsx. If that doesn't work, you could programmatically rename the file as described for example here, and then open it using one of these excel packages.
Your file could be in xls format instead of xlsx, have you tried read_xls() function from readxl? Or it could also be in text format, in this case read.table() or fread() from data.tableshould work. The fact that it works after saving the file in xlsx strongly suggests that it is not formatted as an xlsx to begin with.
Hope this helps.

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.

Resources