38

I am getting a caught segfault error every time I try to run any plotting functions from the ggplot2 package (1.0.0). I have tried this with qplot, geom_dotplot, geom_histogram, etc. Data from the package (e.g. diamonds or economics) work just fine.

I am operating on Mac OS 10.9.4 (the latest version) and on R 3.1.1 (also the latest version). I get the same error with the standard R GUI, RStudio, and when using R from the command line. The command brings up the default graphic device (Quartz for R GUI and command line), but also the terminal error.

library(ggplot2)
qplot(1:10)

gives me the error:

*** caught segfault ***
address 0x18, cause 'memory not mapped'

Traceback:
 1: .Call("plyr_split_indices", PACKAGE = "plyr", group, n)
 2: split_indices(scale_id, n)
 3: scale_apply(layer_data, x_vars, scale_train, SCALE_X, panel$x_scales)
 4: train_position(panel, data, scale_x(), scale_y())
 5: ggplot_build(x)
 6: print.ggplot(list(data = list(), layers = list(<environment>),     scales = <S4 object of class "Scales">, mapping = list(x = 1:3),     theme = list(), coordinates = list(limits = list(x = NULL,         y = NULL)), facet = list(shrink = TRUE), plot_env = <environment>,     labels = list(x = "1:3", y = "count")))
 7: print(list(data = list(), layers = list(<environment>), scales = <S4 object of class "Scales">,     mapping = list(x = 1:3), theme = list(), coordinates = list(        limits = list(x = NULL, y = NULL)), facet = list(shrink = TRUE),     plot_env = <environment>, labels = list(x = "1:3", y = "count")))

Possible actions:

 1: abort (with core dump, if enabled)
 2: normal R exit
 3: exit R without saving workspace
 4: exit R saving workspace

Here is my session info:

R version 3.1.1 (2014-07-10)
Platform: x86_64-apple-darwin13.1.0 (64-bit)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] graphics  grDevices utils     datasets  stats     methods   base     

other attached packages:
[1] ggplot2_1.0.0 marelac_2.1.3 seacarb_3.0   shape_1.4.1   beepr_1.1     birk_1.1     

loaded via a namespace (and not attached):
[1] audio_0.1-5      colorspace_1.2-4 digest_0.6.4     grid_3.1.1       gtable_0.1.2    
[6] MASS_7.3-34      munsell_0.4.2    plyr_1.8.1       proto_0.3-10     Rcpp_0.11.2     
[11] reshape2_1.4     scales_0.2.4     stringr_0.6.2    tools_3.1.1

I've gathered from others that this is a memory issue of some sort, but this error occurs even when I have over 2 GB of free RAM. I know this is a widely used package, so of course this doesn't happen for everyone, but why is it happening for me? Does anyone know what I can do to fix this problem?

11
  • 3
    So if you start a brand new R session and run only those two commands (library(ggplot2); qplot(1:10)) you get that error? Can you add the output of sessionInfo() after running library(ggplot2) to your question as well.
    – MrFlick
    Sep 4, 2014 at 19:01
  • @MrFlick, yes, that is correct. This error occurs during a brand new R session. sessionInfo output is added.
    – CephBirk
    Sep 4, 2014 at 19:18
  • 1
    Where/when are the "marelac_2.1.3, seacarb_3.0, shape_1.4.1, beepr_1.1 birk_1.1" packages being loaded? Do you have an .Rprofile set up somewhere? When you start R is it restoring a saved workspace (is there a .Rdata file in the working directory)?
    – MrFlick
    Sep 4, 2014 at 19:24
  • @MrFlick, all those packages are being loaded from an .Rprofile, yes. However, even if I remove the .Rprofile, the same problem occurs. R is NOT restoring a saved workspace.
    – CephBirk
    Sep 4, 2014 at 19:34
  • 1
    So that's a start. Is it possible that you installed a ggplot2 binary that was developed for the SL version?
    – IRTFM
    Sep 5, 2014 at 15:52

3 Answers 3

46

In case anyone else has this problem or similar in the future, I sent a bug report to the package maintainer and he recommended uninstalling all installed packages and starting over. I took his advice and it worked!

I followed advice from this posting: http://r.789695.n4.nabble.com/Reset-R-s-library-to-base-packages-only-remove-all-installed-contributed-packages-td3596151.html

ip <- installed.packages()
pkgs.to.remove <- ip[!(ip[,"Priority"] %in% c("base", "recommended")), 1]
sapply(pkgs.to.remove, remove.packages)
4
  • 21
    You can then reinstall them again with sapply(pkgs.to.remove, install.packages)
    – slackline
    Jun 2, 2015 at 16:24
  • 4
    I had a similar problem (with IRKernel / rzmq), but uninstalling an reinstalling packages did not help; see: github.com/IRkernel/IRkernel/issues/237. Dec 25, 2015 at 20:39
  • 1
    @CephBirk what about packages that were not installed from CRAN :)? then install.packages doesn't retrive them
    – Marcin
    Jul 6, 2016 at 16:31
  • In my case, it did not work. My error was connected with utils::download.file and failed only in the devtools::test() runs and ONLY when done from rstudio. The solution I implemented was just using system("wget ...") instead of download.file.
    – Garini
    Oct 10, 2018 at 15:01
2

This is not an answer to this question but it might be helpful for someone. (Inspired by user1310503. Thanks!)

I am working on a data.frame df with three cols: col1, col2, col3. Initially,

df =data.frame(col1=character(),col2=numeric(),col3=numeric(),stringsAsFactors = F)

In the process, rbind is used for many times, like:

aList<-list(col1="aaa", col2 = "123", col3 = "234")
dfNew <- as.data.frame(aList)
df <- rbind(df, dfNew)

At last, df is written to file via data.table::fwrite

data.table::fwrite(x = df, file = fileDF, append = FALSE, row.names = F, quote = F, showProgress = T)

df has 5973 rows and 3 cols. The "caught segfault" always occurs:

address 0x1, cause 'memory not mapped'. 

The solution to this problem is:

aList<-list(col1=as.character("aaa"), col2 = as.numeric("123"), col3 = as.numeric("234"))
dfNew <- as.data.frame(aList)
dfNew$col1 <- as.characer(dfNew$col1)
dfNew$col2 <- as.numeric(dfNew$col2)
dfNew$col3 <- as.numeric(dfNew$col3)
df <- rbind(df, dfNew)

Then this problem is solved. Possible reason is that the classes of cols are different.

1
  • This is exactly what I was looking for. I hadn't ensured that my columns appeared in the same order before rbind(). Just reorder columns like in stackoverflow.com/a/7496246/5021307 before rbind() to fix this issue. Feb 16, 2018 at 22:18
1

This is not an answer to this question but it might be useful for someone. I had segfaults when I did pdf to create a PDF graphics device and then used plot. This happened with R 2.15.3, 3.2.4, and one or two other versions, running on Scientific Linux release 6.7. I tried many different things, but the only ways I could get it to work were (a) using png or tiff instead of pdf, or (b) saving large .RData files and then using a completely separate R program to create the graphics.

3
  • I have the same issue and that is the same workaround that I am using. Let me know if you were able to find a fix or a reason for this. May 6, 2016 at 21:14
  • That's interesting. I only got segfaults when there was a very large object in memory (this object was not used in the plot command). Is it the same for you? So another idea I had was to save the large object to disk and remove it from memory, either before doing pdf or between pdf and plot, and then reload it afterwards. But I haven't tried this yet as it would require some rewriting. May 30, 2016 at 8:52
  • In my case I did not have any large objects. I explicitly ran a "gc()" to force garbage collection and checked the free memory just before running a "plot(1:10)" command. It had more than 20GB of free RAM at that point. Jun 3, 2016 at 2:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.