Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7586869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:32:01+00:00 2026-05-30T19:32:01+00:00

I am trying to sweave a report and my problem is that every time

  • 0

I am trying to sweave a report and my problem is that every time i compile R loads the packages I use in the Report (like ggplot2, MASS, cubature..). This is very time consuming. Is there a way to chache the packages?

I found cacheSweave but it does not work.

This is the chunk i added in the sweave file:

<<cacheSweave, eval=TRUE, echo=FALSE, term=FALSE, cache=TRUE>>=
library(cacheSweave) 
 lapply(c("ghyp","MASS","nloptr","cubature","ggplot2"), require, character.only=T)
@

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T19:32:02+00:00Added an answer on May 30, 2026 at 7:32 pm

    Since you showed interest in the knitr package, I spent some time implementing this feature, and you can download the development version from https://github.com/yihui/knitr. As I said, cacheSweave does not preserve any side effects; the current stable version of knitr on CRAN only preserves the side effects of printing, and the side effects of loading packages are preserved in the development version (>= 0.3.3) on GitHub. When you run a cached chunk, all the package names are cached in a file __packages. Next time when this chunk is to be rebuilt, all the packages will be loaded before executing the code in the chunk, otherwise this chunk will be skipped. In other words, packages are only loaded when they are really needed.

    The other way to do this is to use chunk hooks, which does not require you to install the development version. For example, you can add a chunk option named packages, and design a chunk hook like:

    <<setup, include=FALSE, cache=FALSE>>=
    knit_hooks$set(packages = function(before, options, envir) {
      if (before) {
        ## load packages before a chunk is executed
        for (p in options$packages) library(p, character.only = TRUE)
      }
    })
    @
    

    Then you can use this chunk option like

    <<test, packages=c('MASS', 'ggplot2')>>=
    qplot(rnorm(100))
    @
    

    where the option packages is a character vector of package names, which are used by the chunk hook defined above. The disadvantage of this approach is you may have to specify this packages vector for many chunks, whereas the first approach is automatic. You may need to spend a few minutes on learning how chunk hooks work in knitr: http://yihui.name/knitr/hooks

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use Sweave to generate a statistical report, and am trying to
I'm trying to use Sweave and R to report the results of my statistical
Trying to honor a feature request from our customers, I'd like that my application,
I'm trying to make a presentation on ggplot2 graphics using beamer + sweave. Some
Trying to get my css / C# functions to look like this: body {
Trying to find some simple SQL Server PIVOT examples. Most of the examples that
Trying to make a make generic select control that I can dynamically add elements
Trying to do this sort of thing... WHERE username LIKE '%$str%' ...but using bound
Trying to perform a single boolean NOT operation, it appears that under MS SQL
I'm trying to incorporate some plots from R in my LaTeX document through Sweave.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.