Skip to content

Commit

Permalink
Fixed a bug (identified by Robert Zeigler), when row.names = TRUE. Th…
Browse files Browse the repository at this point in the history
…at process also identified another bug involving the loss of the original data frame row names, which has been fixed. This version will be submitted to CRAN as 3.2.1.
  • Loading branch information
marcschwartz committed Aug 8, 2013
1 parent ba8af9b commit 310bf5a
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 6 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: WriteXLS
Version: 3.2.0
Date: 2013-07-28
Version: 3.2.1
Date: 2013-08-08
Title: Cross-platform Perl based R function to create Excel 2003 (XLS) and Excel 2007 (XLSX) files
Description: Cross-platform Perl based R function to create Excel 2003 (XLS) and Excel 2007 (XLSX)
files from one or more data frames. Each data frame will be
Expand Down
17 changes: 16 additions & 1 deletion R/WriteXLS.R
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,30 @@ WriteXLS <- function(x, ExcelFileName = "R.xls", SheetNames = NULL, perl = "perl
# Need to convert all columns in DF.LIST[[i]] to character
# to allow for rbinding of COMMENTS, since columns may be of various types.
# Everything is going to be output via write.table() as character anyway.
DF.LIST[[i]] <- as.data.frame(lapply(DF.LIST[[i]], as.character), stringsAsFactors = FALSE)
# Preserve the rownames from the original DF.LIST, lest they get
# re-named to numbers by default.
DF.LIST[[i]] <- as.data.frame(lapply(DF.LIST[[i]], as.character),
stringsAsFactors = FALSE,
row.names = rownames(DF.LIST[[i]]))

# Pre-pend "WRITEXLS COMMENT:" to each comment so that we can differentiate
# the comment row from column names, which may or may not be written
# out depending upon 'col.names' argument
COMMENTS <- paste("WRITEXLS COMMENT:", COMMENTS)

# rbind() COMMENTS to the data frame as the first row
# This may result in a renaming of the DF.LIST[[i]]
# rownames after rbind()ing which will get picked up in the Excel
# file if row.names = TRUE. (eg. What was row '1' will then be row '2').
# Get rownames from DF.LIST[[i]] and reset after rbind()ing.
# Set the rowname for the COMMENTS row also, so that if row.names = TRUE,
# the rownames will get dumped by write.table() below and the
# the first row gets picked up as the comments row in the Perl code.
# The Perl code only checks the first parsed field in the CSV file row and
# the rownames will be the first column in each row.
RowNames <- c("WRITEXLS COMMENT: ", rownames(DF.LIST[[i]]))
DF.LIST[[i]] <- rbind(COMMENTS, DF.LIST[[i]])
rownames(DF.LIST[[i]]) <- RowNames

# Write out the data frame to the CSV file
write.table(DF.LIST[[i]], file = paste(Tmp.Dir, "/", i, ".csv", sep = ""),
Expand Down
2 changes: 1 addition & 1 deletion inst/Perl/WriteXLS.pl
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ sub string_width {

$Column = 0;

# The row with comments with be 0 if the column names are not
# The row with comments will be 0 if the column names are not
# output in the CSV file, 1 otherwise.
if ($Row <= 1) {
if (index($Fields[0], "WRITEXLS COMMENT: ") != -1) {
Expand Down
2 changes: 1 addition & 1 deletion inst/Perl/WriteXLSX.pl
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ sub string_width {

$Column = 0;

# The row with comments with be 0 if the column names are not
# The row with comments will be 0 if the column names are not
# output in the CSV file, 1 otherwise.
if ($Row <= 1) {
if (index($Fields[0], "WRITEXLS COMMENT: ") != -1) {
Expand Down
8 changes: 7 additions & 1 deletion man/WriteXLS.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,20 @@
comment(iris$Petal.Length) <- "Length of the petals (cm)"
comment(iris$Petal.Width) <- "Width of the petals (cm)"
comment(iris$Species) <- "Species of the flowers"
WriteXLS("iris", "iriscomments.xlsx", AdjWidth = TRUE, BoldHeaderRow = TRUE)
WriteXLS("iris", "iriscomments.xlsx",
AdjWidth = TRUE, BoldHeaderRow = TRUE)

# Add row names
WriteXLS("iris", "irisrownames.xlsx",
AdjWidth = TRUE, BoldHeaderRow = TRUE, row.names = TRUE)

# Clean up and delete XLS files
rm(iris.split)
unlink("iris.xls")
unlink("Example.xls")
unlink("irissplit.xls")
unlink("iriscomments.xlsx")
unlink("irisrownames.xlsx")
}
}
\keyword{file}

0 comments on commit 310bf5a

Please sign in to comment.