45

I'm having a strange problem with loading the xlsx package and using choose.files.

This problem began as soon as I updated RStudio to the newest version (v0.97.237). If I start RStudio and then load xlsx (i.e. library(xlsx)) the package loads just fine. However, if I then try and use choose.files(), RStudio freezes up (actually it continually runs and can't be closed without ending it with Win7 task manager). If I start RStudio and use choose.files first, it works just fine. If I then run the library(xlsx) line, the xlsx package won't load. It gives the error below.

I've tried reinstalling the xlsx package (along with xlsxjars and rJava), but that didn't work. Neither did reinstalling Java.

Any suggestions? I'm using R v2.15.2.

library(xlsx)  
Loading required package: xlsxjars  
Loading required package: rJava  
Error occurred during initialization of VM  
Could not reserve enough space for object heap  
Error : .onLoad failed in loadNamespace() for 'xlsxjars', details:  
  call: .jinit()  
  error: Cannot create Java virtual machine (-4)  
Error: package ‘xlsxjars’ could not be loaded  

EDIT: Here's the problem. Sorry for any confusion. When I enter this:

filename<-file.choose() #select file
library(xlsx)
mydata<-read.xlsx(filename,1) #load datafile

I can successfully select the file name, but the xlsx package fails to load so I can't open the data file. I get the error above. However, if I run this instead (after I've closed and restarted RStudio):

library(xlsx)
filename<-file.choose() #select file
mydata<-read.xlsx(filename,1) #load datafile

The xlsx package loads properly, but file.choose causes R to run indefinitely so the script freezes and I can't load my file. This is also the case using choose.files(). I have no idea why those two things (xlsx, and file.choose/choose.files) would be conflicting but it appears that they are since I can only use one or the other depending on which one I use first.

EDIT 2: I've tried this on a different computer (same OS, same versions of R, RStudio, and Java) and I get the same problem.

EDIT 3: I converted back to RStudio v0.97.90 and the problem went away. I guess it is an RStudio problem.

Kristian Glass
  • 37,325
  • 7
  • 45
  • 73
James
  • 479
  • 4
  • 4
  • Looks like more of a problem with your Java installation than with R. Adding tag. You ought to document the version number for Java. This has no apparent connection to `choose.files`. – IRTFM Dec 04 '12 at 19:19
  • If I don't use choose.files, the xlsx package loads and works perfectly. The only time xlsx doesn't work is if I try and load it after I've used choose.files. I have Java version 1.7.0_09 – James Dec 04 '12 at 19:58
  • What do you mean by "use it after I've used choose.files"? There is a base function named `file.choose`. – IRTFM Dec 04 '12 at 21:49
  • Sorry, I wasn't clear. If I run the line in my script with choose.files before I run the line library(xlsx), then xlsx won't load and gives the error (but choose.files() works properly). If I then close RStudio, reopen it and then run the lines in the opposite order, the xlxs package loads properly, but choose.files freezes up R. – James Dec 05 '12 at 16:15
  • I say again: There is a function `file.choose` in base pkg and I now say in addition that there is NO function `choose.files` in xslx. – IRTFM Dec 05 '12 at 16:40
  • Yes, choose.files is not in xlsx. I know that. choose.files and file.choose are basically the same and both have the problems that I describe above. – James Dec 05 '12 at 17:18
  • When I type `choose.files()` in an RStudio session I get an error. When I type `file.choose()` I get a dialog box. So, at least on one machine, they are not "basically the same". Your error remains not reproducible because you are not describing the environment in which it occurs adequately. You have not described this in enough detail. (I thought maybe you were trying `library(file.choose())`, but that just errors out. – IRTFM Dec 05 '12 at 19:27
  • I can't reproduce this on Windows 7 x64 with R 3.0.2 and RStudio 0.98.501. Do you still have this problem? – nograpes Feb 22 '14 at 13:18
  • 5
    The problem is that the C stack size was increased in R 2.15.2 from 10MB to 64MB (and reduced back to 16MB in R 3.x.x because of errors like this). See your error message "Could not reserve enough space for object heap" after calling file.choose() which seems to use some memory. I would guess that a call `.jinit(parameters=c("-Xrs", "-Xss1m"))` before calling `library(xlsx)` would solve this. – Kornelius Rohmeyer Mar 09 '14 at 21:09
  • Have your reported the bug to the rstudio team ? – Karl Forner Apr 04 '14 at 09:55
  • PS read.xlsx is supposed to read in a sheet library(xlsx) filename = "c:/wheveryourfileishere" mydata = read.xlsx(filename,sheetName='nameofsheetyouwant') – Kyle Balkissoon Sep 16 '14 at 02:49
  • 1
    I would advise you to use the WriteXLS package: [{WriteXLS}](http://cran.r-project.org/web/packages/WriteXLS/WriteXLS.pdf) This package is much more Rstudio friendly imo. – Sander Van der Zeeuw Sep 29 '14 at 15:26

6 Answers6

1

I always use these lines at the top:

options(java.parameters="-Xmx4000m")
options(java.home="C:/Program Files/Java/jre7/")

See if maybe that helps.

Romain
  • 839
  • 10
  • 24
0

Try clearing out your Temp files from

Control Panel > Programs > Java

See if it works.

zyduss
  • 91
  • 2
0

I recently had this issue with an installation of R on a new machine. Make sure you are using the correct version of Java (32-bit vs 64-bit) to match your version of R. Note that the 64-bit version of Java is not found on their main download page but on their "manual" d/l page here.

Leo
  • 1,773
  • 12
  • 19
0

Try some of these packages:

install.packages("XLConnect")
install.packages("xlsxjars")
install.packages("xlsx")

Then load the library()

Use read.xlsx2("blah.xlsx",sheetIndex=NULL)

Don't use the file.choose, just insert the file straight into the read.xlsx2 function

or

read.xlsx2("blah.xlsx",sheetName=NULL")
mikeymike
  • 75
  • 5
  • What is the benefit of installing `XLConnect` when you are calling `read.xlsx2()` from package `xlsx`? – Uwe Dec 01 '16 at 06:44
  • XLConnect is package that allows for reading, writing and manipulating Microsoft Excel files from within R. Its just another way to read excel files. Just offering my advice on reading excel files in others ways. But for xlsx you do need the xlsxjars package – mikeymike Dec 01 '16 at 16:07
  • Both packages aim at the same goal: `XLConnect`: _Provides comprehensive functionality to read, write and format Excel data._ `xlsx`: _Provide R functions to read/write/format Excel 2007 and Excel 97/2000/XP/2003 file formats._ However, also `XLConnect` [depends on other packages `XLConnectJars` and `rJava`](https://cran.r-project.org/web/packages/XLConnect/index.html) which need to be installed alongside. – Uwe Dec 01 '16 at 17:39
  • 2
    i'd recommend `openxlsx` which is quite fully featured and does not depend on java – Stefan F Jan 21 '17 at 08:01
0

One of the newer packages is the rio package. It has solved many of the import and export problems that I was having, and has the added benefit of only needing one library to import/export many different formats. It determines the format based on the extension of the filename. It also seems to run cleaner and faster than some of the other packages, even though it uses basically the same functions. For example, with Excel files, it uses the write.xlsx function, but has worked for me with large datasets, when write.xlsx didn't. the packages documentation can be found here.

install.packages("rio")    
library(rio)

import("myfile.xlsx")
export(df, "myfile.xlsx")
0

I'd recommend Hadley's readxl package, it installs from CRAN without Java dependencies.

library("readxl")
my_data <- read_excel(filename, sheet = 1)
Tom Kelly
  • 1,458
  • 17
  • 25