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

  • Home
  • SEARCH
  • 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 827707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:36:55+00:00 2026-05-15T03:36:55+00:00

I have a function that sets a vector to a string, copies a Sweave

  • 0

I have a function that sets a vector to a string, copies a Sweave document with a new name and then runs that Sweave. Inside the Sweave document I want to use the vector I set in the function, but it doesn’t seem to see it.

(Edit: I changed this function to use tempdir(() as suggested by Dirk)

I created a sweave file test_sweave.rnw;

% 
\documentclass[a4paper]{article}
\usepackage[OT1]{fontenc}
\usepackage{Sweave}
\begin{document}

\title{Test Sweave Document}
\author{gb02413}

\maketitle

<<>>=
ls()
Sys.time()
print(paste("The chosen study was ",chstud,sep=""))
@ 
\end{document}

and I have this function;

onOK <- function(){ 
    chstud<-"test" 
    message(paste("Chosen Study is ",chstud,sep="")) 
    newfile<-paste(chstud,"_report",sep="") 
    mypath<-paste(tempdir(),"\\",sep="")
    setwd(mypath) 
    message(paste("Copying test_sweave.Rnw to ",paste(mypath,newfile,".Rnw",sep=""),sep=""))
    file.copy("c:\\local\\test_sweave.Rnw", 
            paste(mypath,newfile,".Rnw",sep=""), overwrite=TRUE) 
    Sweave(paste(mypath,newfile,".Rnw",sep="")) 
    require(tools) 
    texi2dvi(file = paste(mypath,newfile,".tex",sep=""), pdf = TRUE) 
} 

If I run the code from the function directly, the resulting file has this output for ls();

> ls()
[1] "chstud" "mypath" "newfile" "onOK"

However If I call onOK() I get this output;

> ls()
[1] "onOK"

and the print(…chstud…)) function generates an error.

I suspect this is an environment problem, but I assumed because the call to Sweave occurs within the onOK function, it would be in the same enviroment, and would see all the objects created within the function. How can I get the Sweave process to see the chstud vector ?

Thanks

Paul.

  • 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-15T03:36:55+00:00Added an answer on May 15, 2026 at 3:36 am

    OK, I realise that my initial ideas of a ‘simple, self contained example’ wasn’t particularly simple or usefull. So I redid my example and got a sort of answer for myself, although I’d love someone to exaplain why and even suggest a better solution,
    Here’s my example test_sweave.Rnw file

    % 
    \documentclass[a4paper]{article}
    \usepackage[OT1]{fontenc}
    \usepackage{Sweave}
    \begin{document}
    
    \title{Test Sweave Document}
    \author{Paul Hurley}
    
    \maketitle
    
    <<>>=
    
    if(exists("foo")){print(foo)}
    ls()
    Sys.time()
    @ 
    \end{document}
    

    If I run this code;

    testFoo<-function(){    
      foo<-"My Test String"     
      Sweave("test_sweave.Rnw")     
      require(tools)    
      texi2dvi(file = "test_sweave.tex", pdf = TRUE) 
    }
    
    rm(foo) testFoo()
    

    my resulting file does NOT contain the contents of the string foo.

    > if (exists("foo")) {
    + print(foo)
    + }
    > ls()
    [1] "testFoo"
    

    If I run this code (i.e, the same thing, just run directly)

    rm(foo)
    foo<-"My Test String"
    Sweave("test_sweave.Rnw")
    require(tools) 
    texi2dvi(file = "test_sweave.tex", pdf = TRUE)
    

    my resulting file does contain the foo string

    > if (exists("foo")) {
    + print(foo)
    + }
    [1] "My Test String"
    > ls()
    [1] "foo" "testFoo"
    

    and if I run this code

    testBar<-function(){
        foo<<-"My Test String"
        Sweave("test_sweave.Rnw")
        require(tools) 
        texi2dvi(file = "test_sweave.tex", pdf = TRUE)
    }
    
    rm(foo)
    testBar()
    

    My resulting file also contains the foo string

    > if (exists("foo")) {
    + print(foo)
    + }
    [1] "My Test String"
    > ls()
    [1] "foo" "testBar" "testFoo"
    

    So, it seems that sweave runs in the global environment, not in the environment it was called from. This means the only way to pass variables to sweave when sweave is run from a function is to use the <<- operator to put the variable in the global environment. (I think).

    Anyone else want to comment who knows more about environments ?

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

Sidebar

Ask A Question

Stats

  • Questions 410k
  • Answers 410k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I'd consider option 4: composition. First, define your set of… May 15, 2026 at 7:32 am
  • Editorial Team
    Editorial Team added an answer Install the following file on the device: sqlce.dev.ENU.ppc.wce5.armv4i.CAB It can… May 15, 2026 at 7:32 am
  • Editorial Team
    Editorial Team added an answer what about something like this? Flex! (I've made it a… May 15, 2026 at 7:32 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.