unabel to locate ggplot2

chettyvk's Avatar

chettyvk

22 Jul, 2012 01:41 PM

Hi,
When I compile as an R-program, Rstudio locates ggplot2. But when I use knitr, I get the following error message: I have installed all the packages. I think it is not in the default location. How do I idnetify whrere it is and how do I redirect RStudio to look there?
Thanks.

Chetty

library(ggplot2)

Error: package 'reshape' required by 'ggplot2' could not be found

names(cars)

[1] "speed" "dist"

mean(cars$speed)

[1] 15.4

qplot(speed, dist, data = cars)

Error: could not find function "qplot"

  1. Support Staff 2 Posted by Josh Paulson on 23 Jul, 2012 12:11 AM

    Josh Paulson's Avatar

    Hello,

    Sweave and knitr use a separate environment and you'll need to load the library in your script such as library(ggplot2). See the following documentation:

    http://rstudio.org/docs/authoring/overview

    You can compile the Sweave document into a PDF using the Compile PDF button on the toolbar. Note that for reproducibility purposes, this compile runs in a separate process and environment (rather than using the current workspace). This is to ensure the script will always produce the same result and not be effected by (or pollute) your regular R environment.

    Josh

  2. Josh Paulson closed this discussion on 23 Jul, 2012 12:11 AM.

  3. chettyvk re-opened this discussion on 23 Jul, 2012 02:10 AM

  4. 3 Posted by chettyvk on 23 Jul, 2012 02:15 AM

    chettyvk's Avatar

    Thanks Josh. Please note that there is a statement library(ggplot2) and the error message came in spite of that. My memory is that when I installed packages I got a warning that the packages cannot be installed in the default location ant they will be installed in some personal library. SO the package "ggplot2" is there but RStudio is not getting it. How to point to that location?
    Thanks.
    Chetty

  5. Support Staff 4 Posted by Josh Paulson on 23 Jul, 2012 02:53 PM

    Josh Paulson's Avatar

    Chetty,

    Sorry for the confusion and are you saying that when call library(ggplot2) from the console it works fine, but not inside an Rnw document? We're not doing anything special, but perhaps try putting a call to .libPaths() inside your .Rnw document and looking at the output. This will tell you the path that R is looking for packages. See more at:

    > ?.libPaths
    

    Josh

  6. 5 Posted by chettyvk on 23 Jul, 2012 03:21 PM

    chettyvk's Avatar

    Here is the output with .libPaths(). Thanks for any help.

    Chetty

    library(ggplot2)

    Loading required package: reshape

    Loading required package: plyr

    Attaching package: 'reshape'

    The following object(s) are masked from 'package:plyr':

    #

    rename, round_any

    Loading required package: grid

    Loading required package: proto

    Warning: there is no package called 'proto'

    Error: package 'proto' could not be loaded

    .libPaths()

    [1] "C:/Program Files/R/R-2.15.1/library"

    qplot(speed, dist, data = cars)

    Error: could not find function "qplot"

  7. Support Staff 6 Posted by Josh Paulson on 23 Jul, 2012 09:46 PM

    Josh Paulson's Avatar

    Chetty,

    This seems to be a dependency problem as it says that ggplot2 was able to load, but it's dependencies (proto) failed. Did you recently upgrade versions of R and now the packages are installed into a new directory? The directory you've displayed is a standard location for packages on Windows. If you are still having trouble, you'll need to add the location to your .libpaths for R to pick this up.

    Josh

  8. 7 Posted by chettyvk on 23 Jul, 2012 10:14 PM

    chettyvk's Avatar

    Yes. I upgraded R to the latest version a few days a week ago. How do I add the location to .libpaths?
    Is easier to uninstall Rstudion, R etc and reinstall one or more?
    Thanks.

  9. Support Staff 8 Posted by Josh Paulson on 24 Jul, 2012 01:03 PM

    Josh Paulson's Avatar

    Chetty,

    See the following R documentation:

    > ?.libPaths
    

    .libPaths is used for getting or setting the library trees that R knows about (and hence uses when looking for packages). If called with argument new, the library search path is set to the existing directories in unique(c(new, .Library.site, .Library)) and this is returned. If given no argument, a character vector with the currently active library trees is returned.

    The library search path is initialized at startup from the environment variable R_LIBS (which should be a colon-separated list of directories at which R library trees are rooted) followed by those in environment variable R_LIBS_USER. Only directories which exist at the time will be included.

    If you'd rather use environment variables, see the following:

    > ?Startup
    

    Josh

  10. 9 Posted by chettyvk on 25 Jul, 2012 02:04 PM

    chettyvk's Avatar

    Thanks. First, I have no problems in my netbook PC with windows7. It works beautifully. My problem is in my desktop with windows XP. I tried the following line listing all my libraries!:

    .libPaths("C:\Program Files\R\R-2.15.1\library;C:\Program Files\RStudio\R\library;c:\R\R-2.12.2\library")

    Same problem. Will greatly appreciate help!
    Thanks again.
    Chetty

  11. chettyvk closed this discussion on 27 Jul, 2012 04:18 PM.

Comments are currently closed for this discussion. You can start a new one.