I have an excel sheet .xlsx, which have some commented cells in it. After importing it in R, is there any way by which R can identify the commented cells?
Because I have to use some if else conditions only to the commented cells.
To display all comments in the worksheet, go to the Review tab > Show all Comments. To move or resize any overlapping comments, right-click and select Edit Comment, and the border of the comment box will display sizing handles.
The xlsx package, a java-based solution, is one of the powerful R packages to read, write and format Excel files.
Let's say we have this file, test.xlsx
:
Using openxlsx we can read the workbook as a dataframe object to extract only data:
library(openxlsx)
# read the data
df1 <- read.xlsx("test.xlsx")
df1
# 1 no
# 1 2 yes
# 2 3 no
# 3 5 no
# 4 10 yes
If we need to extract comments, we need to read as workbook object:
# read as workbook object
wb <- loadWorkbook("test.xlsx")
Check the structure of the object to see where the cell reference is stored:
str(wb$comments)
# List of 3
# $ :List of 2
# ..$ :List of 5
# .. ..$ ref : chr "B2"
# ...
# ... etc
Then subset, loop through sheets, and subset cell reference with comments:
lapply(wb$comments, function(sheet)
sapply(sheet, "[[", "ref"))
# [[1]]
# [1] "B2" "B5"
#
# [[2]]
# list()
#
# [[3]]
# list()
This means there are 3 sheets, 1st sheet has 2 comments at B2 and B5, other 2 sheets are blank.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With