GithubHelp home page GithubHelp logo

marcschwartz / writexls Goto Github PK

View Code? Open in Web Editor NEW
20.0 5.0 9.0 1.74 MB

CRAN Package WriteXLS: 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 written to a separate named worksheet in the Excel spreadsheet. The worksheet name will be the name of the data frame it contains or can be specified by the user.

License: GNU General Public License v2.0

R 0.38% Perl 99.62%

writexls's Introduction

WriteXLS is a contributed package for R (http://www.r-project.org/)

CRAN URL: http://cran.r-project.org/web/packages/WriteXLS/index.html

GitHub URL: https://github.com/marcschwartz/WriteXLS

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 written to a separate named worksheet in the Excel spreadsheet.
             The worksheet name will be the name of the data frame it contains
             or can be specified by the user. 

License: This package is licensed under GPL version 2. See the COPYING file.

Note: This package includes Perl source code modules that may or may not be part
      of typical Perl installations in order to ease the installation and use of
      this package in R.

Author: Marc Schwartz and various authors for Perl modules listed in each .pm file.

Copyright: Marc Schwartz and additional copyright holders of the Perl files are
           listed in each .pm file under the Perl directory.

For general installation information, please review:

  https://github.com/marcschwartz/WriteXLS/blob/master/INSTALL

This package was moved from R-Forge to GitHub on May 17, 2012

writexls's People

Contributors

marcschwartz avatar

Stargazers

 avatar Ryan avatar  avatar Kazuki Yoshida avatar Maciej Motyka avatar François Joly avatar Xiangyun Huang avatar quanquan avatar Juri Grabowski avatar Rodrigo avatar Martín González Gómez avatar INX avatar Mikael Elenius avatar Thomas J. Leeper avatar  avatar Kun Ren avatar timelyportfolio avatar Gerit Wagner avatar Benjamin Tyner avatar Omar avatar

Watchers

James Cloos avatar Thomas Zumbrunn avatar Juri Grabowski avatar  avatar Rodrigo avatar

writexls's Issues

Numeric values are written as strings with WriteXLS 6.0.0

With WriteXLS 6.0.0, some values in numeric columns are written as strings instead of numbers. This did not happen with WriteXLS 5.0.0.

WriteXLS 5.0.0 is running with perl v5.26.1 on Ubuntu 18.04.5, WriteXLS 6.0.0 with perl v5.30.0 on Ubuntu 20.04.1.

Reproducible example

The following code was used to write an Excel-file with WriteXLS 5.0.0 and 6.0.0:

data <- tibble::tibble(
    x = sample(letters, 1000, replace = TRUE),
    y = sample(1000:9999, 1000, replace = TRUE))
WriteXLS::WriteXLS(data, "/media/exchange/tmp/test_writexls.xlsx")

The resulting files are attached:
test_writexls5.xlsx
test_writexls6.xlsx

A screenshot shows the problem:

writexls

Problem with missing Perl modules

Hi Marc,

I have installed the latest version of Strawberry Pearl (see PearlVersion.jpg), but when I run testPerl in R it is reported that two mandatory Perl modules are missing (see testPerl.jpg). I have used cpanm to check that they are installed, but no luck in making them available. Can you offer any suggestions.

Cheers ,

Chris Brien
[email protected]

PerlVersion
testPerl

WRITEXLS COMMENT: shows up as first row when row.names=T

The current comment row detection method is faulty because it only examines the first field. But if row.names=T, the first field is the row name, even for comment columns. A better solution might be to grep through the fields in the row for the "WRITEXLS COMMENT:" string and check if the resulting array size is > 0.

apply AdjWidth after AutoFilter?

Hi,
is there a way to apply AdjWidth after AutoFilter? when AutoFilter is applied it takes away from the column name. readjusting in excel is possible. it would be nice if WriteXLS did that.

Issue with "\n" inside characters

Hello,

When data.frames contain character vectors that contain the element "\n" it breaks the table in excel instead of creating a cell with multiple rows of text.

library(WriteXLS)

x <- data.frame(stringsAsFactors=FALSE,
                v1 = 1:3,
                v2 = c("Hello", "This will \n break", "World"))

WriteXLS(x, "test.xlsx")

Thanks

WriteXLS is now calling make.names, causing header to be mangled

Greetings,

Starting with version 3.2.1, it does:

DF.LIST[[i]] <- as.data.frame(lapply(DF.LIST[[i]], as.character), 
        stringsAsFactors = FALSE, row.names = rownames(DF.LIST[[i]]))

I believe it would be good to add optional=TRUE to this call, in order to preserve the existing names.

Regards
Ben

Special sequence generates error

I came across an imported xlsx file, as dataframe, that showed the sequence:
\r\r\n inside several cells (together with other text). In R it is shown without problem and also saving as .csv is normal.

When writing a .xls, this causes in several rows a loss of information - several columns completely lost * but in some rows the problem seems not to appear.-.

I solved it eliminating that string:
df[] <- lapply(df, gsub, pattern = "\r\r\n", replacement = " ", fixed = TRUE)

testPerl() returns no errors, but perl binaries are mismatched

Good day,

I'm running R (3.6.1) and WriteXLS in a conda environment on CentOS7 and I receive the following error:

ListUtil.c: loadable library and perl binaries are mismatched (got handshake key 
0xdb80080, needed 0xde00080)
The Perl script 'WriteXLSX.pl' failed to run successfully.

However, if I run testPerl() I get

Perl found.

All required Perl modules were found.

I've installed perl v5.26.2 via conda. .libPaths() points to /miniconda3/envs/R-env/bin/perl and my perl installation is located at/miniconda3/envs/R-env/bin/perl, so I assume my paths are in order and the correct version of perl is called. Please let me know if there is something I am missing.

Cheers,

Daniel

newlines in text

Hi,

Newlines looks not handled within text. AS a result for each newline in text, a new line in excel

Issue reading xls written with WriteXLS

Hello,

I have found that if I write an XLS with WriteXLS and then read it with readxl::read_excel I lose a column even though the actual XLS document has all the columns. Here is a reproducible example:

z <- tempdir()
ncol(mtcars) # should be 11
WriteXLS::WriteXLS(mtcars,paste0(z,"/mtcars.xls)
mtcars.xls <- readxl::read_excel(paste0(z,"/mtcars.xls"))
ncol(mtcars.xls) # is 10 for some reason

The rightmost column is not read in.

I have no idea whether this is an issue with WriteXLS or readxl but I will file an issue with readxl as well to be safe.

Thanks for the wonderful package.

Regards

WriteXLS doesn't work on R 3.6

WriteXLS installs on R 3.6 without error, however doesn't work as it needs some dependencies that don't install on R 3.6 (Compress).
Here's the error I get when I try to use the WriteXLS function:

Can't locate Compress/Raw/Zlib.pm in @INC (you may need to install the Compress::Raw::Zlib module) (@INC contains: /usr/lib64/R/library/WriteXLS/Perl /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5) at /usr/lib64/R/library/WriteXLS/Perl/Archive/Zip.pm line 9.
BEGIN failed--compilation aborted at /usr/lib64/R/library/WriteXLS/Perl/Archive/Zip.pm line 9.
Compilation failed in require at /usr/lib64/R/library/WriteXLS/Perl/Excel/Writer/XLSX/Workbook.pm line 25.
BEGIN failed--compilation aborted at /usr/lib64/R/library/WriteXLS/Perl/Excel/Writer/XLSX/Workbook.pm line 25.
Compilation failed in require at /usr/lib64/R/library/WriteXLS/Perl/Excel/Writer/XLSX.pm line 17.
BEGIN failed--compilation aborted at /usr/lib64/R/library/WriteXLS/Perl/Excel/Writer/XLSX.pm line 17.
Compilation failed in require at /usr/lib64/R/library/WriteXLS/Perl/WriteXLSX.pl line 36.
BEGIN failed--compilation aborted at /usr/lib64/R/library/WriteXLS/Perl/WriteXLSX.pl line 36.
The Perl script 'WriteXLSX.pl' failed to run successfully.

Any way to encode columns?

I've written a script that exports two sheets. Each sheet has a column of dates that, in the data frame, are stored as date, but when written to Excel are formatted as text. Is there any way around this?

write on xlsm

Hello,
Would it be possible to write on xlsm files?

Thanks for the answer :-)

Giovanni

error when writing a file in Windows

Hi, I'm using Windows 10
R version 4.4.1 (2024-06-14 ucrt)
WriteXLS 6.6.0

WriteXLS::testPerl() runs fine and reports that all modules were found.
yet when attempting to write any file i get this error:

> WriteXLS::WriteXLS(iris, "test.xlsx", verbose=T)
Creating Temporary Directory for CSV Files:  C:\Users\Kleinbub\AppData\Local\Temp\RtmpuO4911/WriteXLS 

Creating CSV File: 1.csv
Creating SheetNames.txt

perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LC_ALL = (unset),
	LANG = "en"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
. No such file or directory\Kleinbub\AppData\Local\Temp\RtmpuO4911/WriteXLS/1.csv
Creating Excel File: C:\Users\Kleinbub\Documents\test.xlsx



Reading: C:\Users\Kleinbub\AppData\Local\Temp\RtmpuO4911/WriteXLS/1.csv
The Perl script 'WriteXLSX.pl' failed to run successfully.
Cleaning Up Temporary Files and Directory

This issue seems to be the same as the one reported here: https://stackoverflow.com/questions/75749758/r-writexls-doesnt-work-on-newer-r-version

WriteXLS 6.2.0 requires much more memory than 6.1.0

Hi Mark

I have noticed that writing files with WriteXLS 6.2.0 uses much more memory than before with 6.1.0. Writing this data frame gives an Excel file of about 3 MB:

library(dplyr)
data <- tibble(
  col1 = paste("ABC", 100001:300000),
  col2 = paste(sample(1:1000, length(col1), replace = TRUE), "A")
)
WriteXLS(data, "test.xlsx")

Since the actual processing takes place in Perl, I have used top to look at the memory consumption of the perl process, which is of course not perfectly accurate. On my Machine with Ubuntu 20.04 and R 4.0.4, writing the Excel files uses about 260 MB of memory with WriteXLS 6.1.0, but 1.9 GB with WriteXLS 6.2.0. This is really an issue because it means that you need a strong and expensive Machine to write a small Excel-file.

Do you have an idea where this is coming from and whether it could be fixed?

Thanks and best regards
Stefan

Does not work on Perl >= 5.20 because of double decoding

Looks like there is a breaking change in Perl 5.20, example

> library(WriteXLS)
> test <- data.frame(foo='čebula')
> WriteXLS('test', Encoding="UTF-8", verbose=1)

Cannot decode string with wide characters at /usr/lib64/perl5/5.20.2/x86_64-linux-thread-multi/Encode.pm line 215, <CSVFILE> line 3.
 at /usr/lib64/perl5/5.20.2/x86_64-linux-thread-multi/Encode.pm line 215, <CSVFILE> line 3.
    Encode::decode_utf8("\x{10d}ebula") called at /usr/lib64/R/library/WriteXLS/Perl/WriteXLS.pl line 295

Proposed patch:

diff --git a/inst/Perl/WriteXLS.pl b/inst/Perl/WriteXLS.pl
index 7164b78..89924c3 100755
--- a/inst/Perl/WriteXLS.pl
+++ b/inst/Perl/WriteXLS.pl
@@ -38,8 +38,16 @@ use File::Basename;
 use Text::CSV_PP;
 use Encode;

-
-
+# double decoding safety measure (breaking change in perl 5.20)
+sub safe_decode_utf8 ($) {
+    my $octets = shift;
+    if (!Encode::is_utf8($octets)) {
+        return decode_utf8($octets);
+    }
+    else {
+        return $octets;
+    }
+}

 ###############################################################################
 # Initialize and get command line arguments
@@ -277,7 +285,7 @@ foreach my $FileName (@FileNames) {
             $Fld = substr $Fld, 18;
             if ($Fld ne "") {
               if ($Encoding eq "UTF-8") {
-                $WorkSheet->write_comment(0, $Column, decode_utf8($Fld));
+                $WorkSheet->write_comment(0, $Column, safe_decode_utf8($Fld));
              } else {
                 $WorkSheet->write_comment(0, $Column, decode("iso-8859-1", $Fld));
               }
@@ -291,7 +299,7 @@ foreach my $FileName (@FileNames) {
       if ($CommentRow != 1) {
         foreach my $Fld (@Fields) {
           if ($Encoding eq "UTF-8") {
-            $WorkSheet->write($Row, $Column, decode_utf8($Fld));
+            $WorkSheet->write($Row, $Column, safe_decode_utf8($Fld));
           } else {
             $WorkSheet->write($Row, $Column, decode("iso-8859-1", $Fld));
          }

Exporting with Excel formulas

Encountered a strange (semi) problem when exporting a data table with excel functions in it. If the formulas use capitalized excel cell notation ("B2") it works fine. But if the cells are called "b2" or "g3", the package complains about an "Unknown defined name SUM in formula at..." The error points to line 298 in WriteXLS.pl, but nothing there is obvious (to me).

The code below should recreate the problem. Isn't serious, but it took a while for me to catch that the upper and lower cases were my problem. (Then again, Excel auto-converts cases in cell references to upper and maybe no one else will get tripped up by this)

library(data.table)
library(WriteXLS)

DT = data.table(x=c("b","b","b","a","a"),v=rnorm(5))

DT[, test := "=SUM(B2:B3)"]

#this works
WriteXLS("DT", ExcelFileName = "test.xls")

DT[, test := "=SUM(b2:b3)"]

#this doesn't
WriteXLS("DT", ExcelFileName = "test.xls")

Best,
James

(BTW, super useful package!)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.