Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NAs get added to xlsx file from just opening and closing #72

Closed
ajdamico opened this issue May 9, 2020 · 11 comments
Closed

NAs get added to xlsx file from just opening and closing #72

ajdamico opened this issue May 9, 2020 · 11 comments
Labels
bug Something isn't working fixed Fixed, issue will be closed within 7 days

Comments

@ajdamico
Copy link

ajdamico commented May 9, 2020

Describe the bug

hi, thanks for the great software. any idea why these NAs get added to the attached test2 using 4_1_5_1.xlsx file? if the behavior is expected, a workaround would be great.. thank you!!

To Reproduce

# behaves as expected with 4.0.17
# remotes::install_version("openxlsx", version = "4.0.17", repos = "http://cran.us.r-project.org") 

# adds NA cells
# remotes::install_github("ycphs/openxlsx")


library(openxlsx)
fn <- "C:/Users/AnthonyD/Desktop/test.xlsx"
fn2 <- "C:/Users/AnthonyD/Desktop/test2.xlsx"
this_wb <- loadWorkbook( fn )
saveWorkbook( this_wb , fn2 )

Expected behavior
loading & immediately saving an xlsx file results in zero changes to xlsx file

test.xlsx is the input
old version of openxlsx gives the expected result (4_0_17)
new version of openxlsx changes the file (4_1_5_1)

test.xlsx
test using 4_1_5_1.xlsx
test using 4_0_17.xlsx

session info

R version 4.0.0 (2020-04-24)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)
@JanMarvin
Copy link
Collaborator

my branch contains a possible fix for this issue. The NA is indeed added by openxlsx upon reading. Though since it is added intentionally, this is a possible wontfix. Also the internal handling of the NA strings might call for a general overhaul. I'm thinking along the lines of na.string = "#N/A" will replace this with NA and replace.na = FALSE would disable adding NA entirely.

@JMPivette
Copy link
Contributor

I had exactly the same kind of issue.
But it happens only on some columns but not on others. I am trying to figure out why some blank cells are converted to "NA" and other not but so far I can't find a pattern..
Maybe it's linked to the use of sharedStrings.

@JanMarvin Do you want me to do some testing on the modification in your branch?
Also it looks like the original code had been written 4 years ago to fix this issue: awalker89/openxlsx#296

@JanMarvin
Copy link
Collaborator

Hi, I never thought of creating a pull request, since it looks to me as if it's intentional behavior. If you want to rebase it on master and create a pull request please go ahead. I'm currently busy with other things. Thanks for finding the original issue, I'll have a look!

@GordonAn
Copy link

I recently encountered thee same issue. I noticed that all cell that is a results of a formula that returns an empty string "" is turned into NA, by simply loadWorkbook() and directly saveWorkbook(). I tried JanMarvin's suggested fixing. It works for me. However, JanMarvin@c47948f#comments

Instead of the changes as suggested by JanMarvin,
just comment out line 248 in R/loadWorkbook.R, fix this bug, without needing touching other places.
line248

line 248

#vals[vals == ""] <- "NA"

I hope someone who has enough knowledge about this package can put this fix into the current development version for test.
my working around is download the source package and modifying this file. and use
install.packages(pkgs="openxlsx", repos = NULL, type="source")
to install the fix on my PC.

deschen1 added a commit to deschen1/openxlsx that referenced this issue Jun 17, 2022
JanMarvin added a commit that referenced this issue Jun 29, 2022
Fixes #72 by adding an optional argument na.convert
@JanMarvin JanMarvin added the fixed Fixed, issue will be closed within 7 days label Jun 29, 2022
@github-actions
Copy link

github-actions bot commented Jul 1, 2023

This issue is stale because it has been open 365 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@github-actions github-actions bot added the Stale label Jul 1, 2023
@ajdamico
Copy link
Author

ajdamico commented Jul 1, 2023

i don't think i have the permissions to remove the Stale label of this issue, but it does look like it's fixed in the development branch! thanks @deschen1 !!

# remotes::install_github("ycphs/openxlsx",ref='development')
library(openxlsx)
fn <- "C:/Users/AnthonyD/Desktop/test.xlsx"
fn2 <- "C:/Users/AnthonyD/Desktop/test2.xlsx"
this_wb <- loadWorkbook( fn , na.convert = FALSE )
saveWorkbook( this_wb , fn2 )

@JanMarvin
Copy link
Collaborator

Just for reference, this is one of the issues solved in openxlsx2.

library(openxlsx2)
wb <- wb_load("https://github.com/ycphs/openxlsx/files/4603444/test.xlsx")
if(interactive()) wb$open()

@ajdamico
Copy link
Author

ajdamico commented Jul 2, 2023

thanks @JanMarvin !! i'm reading the use of upgrade in your docs to imply that openxlsx isn't really going to be maintained going forward. is that true? just curious if i should devote the time to making the switch, or if openxlsx will continue to progress? appreciate it :-)

@JanMarvin
Copy link
Collaborator

I'm not implying anything and luckily the future is unwritten. openxlsx is not going away and if you're uncomfortable or do not have the time/need to switch then don't. I was just providing a possible solution for a stale issue.

@github-actions github-actions bot removed the Stale label Jul 8, 2023
@Catherine-Celice
Copy link

Just to comment so people know this issue is not really stale. I am encountering the same problem where empty cells are being converted to NA simply by loading and saving the workbook. I need to have them remain empty. I can force them to be empty if I read the data into a data frame, then rewrite it to the workbook with writeData(), but I am trying to avoid that since some of my files only need 1 of several tabs updated by the script.

@ajdamico
Copy link
Author

thank you!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed Fixed, issue will be closed within 7 days
Projects
None yet
Development

No branches or pull requests

6 participants