Re: [R] Problem using the source-function within R-functions

2009-10-21 Thread Johan Lassen
Hi Giovanni,

Thanks for your reply. I can make the function work after parsing the code
directly into R. The problem arise after compiling the function into a
package and then calling the function, because the files inside source()
seems to be missing.

I tried to include the sourced files in the argument code_files of
the function package.skeleton. The files are brought correctly to the
package but when running the generated package then the files produce an
error due to variables that are not defined.

There may be no way around other than copying the content of the sourced
files into the file where the function is defined?
- in this way the definition of the function is all written in one file, but
the organization of the code-sections is worse than when using the
source()-function...

Best regards,
Johan


2009/10/20 Giovanni Petris gpet...@uark.edu


 The problem probably lies in the source-ing part: look at

 getwd()
 setwd()

 HTH,
 Giovanni

  Date: Tue, 20 Oct 2009 13:00:02 +0200
  From: Johan Lassen jle...@gmail.com
  Sender: r-help-boun...@r-project.org
  Precedence: list
  DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com;
 s=gamma;
  DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma;
 
  --===0554064772==
  Content-Type: text/plain
  Content-Disposition: inline
  Content-Transfer-Encoding: quoted-printable
  Content-length: 1477
 
  Dear R community,
 
  You may have the solution to how to construct a function using the
 function
  source() to build the function; i.e.
 
  myfunction - function(...){
  source('file1.r')
  source('file2.r')
  }
 
  After compiling and installing the myfunction in R, then calling the
  myfunction gives an error because the content of 'file1.r' and
 'file2.r'
  seems to be missing.
 
  Anyone has the trick to overcome this problem?
 
  Thanks in advance!
 
  best wishes, Johan
 
 
  PS: My function is:
 
 
  run_accumm_value - function(ind_noder_0,
  ind_loc_val,ind_retention,downstream){
  ## Preprocessing of looping calculations:
  koersel_uden_ret - length(unique(ind_noder_0$oplid))
  opsaml_b_0_2 - numeric(koersel_uden_ret)
  opsaml_b_0_2_1 - numeric(koersel_uden_ret)
  opsaml_b_0_2_2 - seq(1:koersel_uden_ret)
  ## Preprocessing of topology and local values to be summed:
  source('preproces_topology.r', local =3D T)
  source('preproces_loc_val.r', local =3D T)
  # Loop for each grouping factor (column in ind_noder_0: oplid):
  for(j in 1:koersel_uden_ret){
  source('matrix_0.r', local =3D T)
  source('matrix.r', local =3D T)
  source('local_value.r', local =3D T)
  source('fordeling.r', local =3D T)
  source('fordeling_manuel.r', local =3D T)
  source('local_ret.r', local =3D T)
  source('Ax=3Db.r', local =3D T)
  source('opsamling_x_0_acc.r', local =3D T)
  }
  source('opsamling_b_1.r', local =3D T)
  opsaml_b_2
  }
 
 
 
 
 
  --=20
  Johan Lassen
  Environment Center Nyk=F8bing F
  Denmark
 
[[alternative HTML version deleted]]
 
 
  --===0554064772==
  Content-Type: text/plain; charset=us-ascii
  MIME-Version: 1.0
  Content-Transfer-Encoding: 7bit
  Content-Disposition: inline
 
  __
  R-help@r-project.org mailing list
  https://stat.ethz.ch/mailman/listinfo/r-help
  PLEASE do read the posting guide
 http://www.R-project.org/posting-guide.htmlhttp://www.r-project.org/posting-guide.html
  and provide commented, minimal, self-contained, reproducible code.
 
  --===0554064772==--
 
 

 --

 Giovanni Petris  gpet...@uark.edu
 Associate Professor
 Department of Mathematical Sciences
 University of Arkansas - Fayetteville, AR 72701
 Ph: (479) 575-6324, 575-8630 (fax)
 http://definetti.uark.edu/~gpetris/




-- 
Johan Lassen
Environment Center Nykøbing F
Denmark

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Problem using the source-function within R-functions

2009-10-21 Thread Duncan Murdoch

On 10/21/2009 3:53 AM, Johan Lassen wrote:

Hi Giovanni,

Thanks for your reply. I can make the function work after parsing the code
directly into R. The problem arise after compiling the function into a
package and then calling the function, because the files inside source()
seems to be missing.

I tried to include the sourced files in the argument code_files of
the function package.skeleton. The files are brought correctly to the
package but when running the generated package then the files produce an
error due to variables that are not defined.

There may be no way around other than copying the content of the sourced
files into the file where the function is defined?
- in this way the definition of the function is all written in one file, but
the organization of the code-sections is worse than when using the
source()-function...


R is not a macro language, and doesn't work well as one (though Thomas 
Lumley has written some code to make it act that way; see R-news 
http://www.r-project.org/doc/Rnews/Rnews_2001-3.pdf).


I don't know the details of what you are trying to do, but the best way 
to handle the generic sort of problem you are describing is to take 
those source'd files, and rewrite their content as functions to be 
called from your other functions.  Then their source can still be in 
separate files, with one line in each location where you need to call it.


The difficulty in doing this is that you need to think carefully about 
what arguments those functions should have, and that may need some 
reorganization.  (Macro languages let you refer to every local variable; 
called functions can only see what you pass them.)  However, in general 
this will make your code easier to read and maintain, so the extra work 
is a good investment.


Duncan Murdoch




Best regards,
Johan


2009/10/20 Giovanni Petris gpet...@uark.edu



The problem probably lies in the source-ing part: look at

getwd()
setwd()

HTH,
Giovanni

 Date: Tue, 20 Oct 2009 13:00:02 +0200
 From: Johan Lassen jle...@gmail.com
 Sender: r-help-boun...@r-project.org
 Precedence: list
 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com;
s=gamma;
 DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma;

 --===0554064772==
 Content-Type: text/plain
 Content-Disposition: inline
 Content-Transfer-Encoding: quoted-printable
 Content-length: 1477

 Dear R community,

 You may have the solution to how to construct a function using the
function
 source() to build the function; i.e.

 myfunction - function(...){
 source('file1.r')
 source('file2.r')
 }

 After compiling and installing the myfunction in R, then calling the
 myfunction gives an error because the content of 'file1.r' and
'file2.r'
 seems to be missing.

 Anyone has the trick to overcome this problem?

 Thanks in advance!

 best wishes, Johan


 PS: My function is:


 run_accumm_value - function(ind_noder_0,
 ind_loc_val,ind_retention,downstream){
 ## Preprocessing of looping calculations:
 koersel_uden_ret - length(unique(ind_noder_0$oplid))
 opsaml_b_0_2 - numeric(koersel_uden_ret)
 opsaml_b_0_2_1 - numeric(koersel_uden_ret)
 opsaml_b_0_2_2 - seq(1:koersel_uden_ret)
 ## Preprocessing of topology and local values to be summed:
 source('preproces_topology.r', local =3D T)
 source('preproces_loc_val.r', local =3D T)
 # Loop for each grouping factor (column in ind_noder_0: oplid):
 for(j in 1:koersel_uden_ret){
 source('matrix_0.r', local =3D T)
 source('matrix.r', local =3D T)
 source('local_value.r', local =3D T)
 source('fordeling.r', local =3D T)
 source('fordeling_manuel.r', local =3D T)
 source('local_ret.r', local =3D T)
 source('Ax=3Db.r', local =3D T)
 source('opsamling_x_0_acc.r', local =3D T)
 }
 source('opsamling_b_1.r', local =3D T)
 opsaml_b_2
 }





 --=20
 Johan Lassen
 Environment Center Nyk=F8bing F
 Denmark

   [[alternative HTML version deleted]]


 --===0554064772==
 Content-Type: text/plain; charset=us-ascii
 MIME-Version: 1.0
 Content-Transfer-Encoding: 7bit
 Content-Disposition: inline

 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide
http://www.R-project.org/posting-guide.htmlhttp://www.r-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.

 --===0554064772==--



--

Giovanni Petris  gpet...@uark.edu
Associate Professor
Department of Mathematical Sciences
University of Arkansas - Fayetteville, AR 72701
Ph: (479) 575-6324, 575-8630 (fax)
http://definetti.uark.edu/~gpetris/








__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



[R] Problem using the source-function within R-functions

2009-10-20 Thread Johan Lassen
Dear R community,

You may have the solution to how to construct a function using the function
source() to build the function; i.e.

myfunction - function(...){
source('file1.r')
source('file2.r')
}

After compiling and installing the myfunction in R, then calling the
myfunction gives an error because the content of 'file1.r' and 'file2.r'
seems to be missing.

Anyone has the trick to overcome this problem?

Thanks in advance!

best wishes, Johan


PS: My function is:


run_accumm_value - function(ind_noder_0,
ind_loc_val,ind_retention,downstream){
## Preprocessing of looping calculations:
koersel_uden_ret - length(unique(ind_noder_0$oplid))
opsaml_b_0_2 - numeric(koersel_uden_ret)
opsaml_b_0_2_1 - numeric(koersel_uden_ret)
opsaml_b_0_2_2 - seq(1:koersel_uden_ret)
## Preprocessing of topology and local values to be summed:
source('preproces_topology.r', local = T)
source('preproces_loc_val.r', local = T)
# Loop for each grouping factor (column in ind_noder_0: oplid):
for(j in 1:koersel_uden_ret){
source('matrix_0.r', local = T)
source('matrix.r', local = T)
source('local_value.r', local = T)
source('fordeling.r', local = T)
source('fordeling_manuel.r', local = T)
source('local_ret.r', local = T)
source('Ax=b.r', local = T)
source('opsamling_x_0_acc.r', local = T)
}
source('opsamling_b_1.r', local = T)
opsaml_b_2
}





-- 
Johan Lassen
Environment Center Nykøbing F
Denmark

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Problem using the source-function within R-functions

2009-10-20 Thread Ista Zahn
Hi Johan,
Although it's not clear to my why you're putting this in a function in
the first place...

The way you've written your function it will only work if your working
directory is the same as the directory where file1.r and file2.r are
stored, and I suspect this is the problem. You can 1) set the working
directory in your function or 2) specify the full path to file1.r and
file2.r in your function. Option 1 will look like

myfunction - function(...){
setwd(/path/where/files/are/saved)
source('file1.r')
source('file2.r')

and option 2 will look like

 myfunction - function(...){
 source('/path/where/files/are/saved/file1.r')
 source('/path/where/files/are/saved/file2.r')

source() also has a chdir option that you could investigate. See

?source

-Ista

On Tue, Oct 20, 2009 at 7:00 AM, Johan Lassen jle...@gmail.com wrote:
 Dear R community,

 You may have the solution to how to construct a function using the function
 source() to build the function; i.e.

 myfunction - function(...){
 source('file1.r')
 source('file2.r')
 }

 After compiling and installing the myfunction in R, then calling the
 myfunction gives an error because the content of 'file1.r' and 'file2.r'
 seems to be missing.

 Anyone has the trick to overcome this problem?

 Thanks in advance!

 best wishes, Johan


 PS: My function is:


 run_accumm_value - function(ind_noder_0,
 ind_loc_val,ind_retention,downstream){
 ## Preprocessing of looping calculations:
 koersel_uden_ret - length(unique(ind_noder_0$oplid))
 opsaml_b_0_2 - numeric(koersel_uden_ret)
 opsaml_b_0_2_1 - numeric(koersel_uden_ret)
 opsaml_b_0_2_2 - seq(1:koersel_uden_ret)
 ## Preprocessing of topology and local values to be summed:
 source('preproces_topology.r', local = T)
 source('preproces_loc_val.r', local = T)
 # Loop for each grouping factor (column in ind_noder_0: oplid):
 for(j in 1:koersel_uden_ret){
 source('matrix_0.r', local = T)
 source('matrix.r', local = T)
 source('local_value.r', local = T)
 source('fordeling.r', local = T)
 source('fordeling_manuel.r', local = T)
 source('local_ret.r', local = T)
 source('Ax=b.r', local = T)
 source('opsamling_x_0_acc.r', local = T)
 }
 source('opsamling_b_1.r', local = T)
 opsaml_b_2
 }





 --
 Johan Lassen
 Environment Center Nykøbing F
 Denmark

        [[alternative HTML version deleted]]


 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.





-- 
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Problem using the source-function within R-functions

2009-10-20 Thread Giovanni Petris

The problem probably lies in the source-ing part: look at

getwd()
setwd()

HTH,
Giovanni

 Date: Tue, 20 Oct 2009 13:00:02 +0200
 From: Johan Lassen jle...@gmail.com
 Sender: r-help-boun...@r-project.org
 Precedence: list
 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma;
 DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma;
 
 --===0554064772==
 Content-Type: text/plain
 Content-Disposition: inline
 Content-Transfer-Encoding: quoted-printable
 Content-length: 1477
 
 Dear R community,
 
 You may have the solution to how to construct a function using the function
 source() to build the function; i.e.
 
 myfunction - function(...){
 source('file1.r')
 source('file2.r')
 }
 
 After compiling and installing the myfunction in R, then calling the
 myfunction gives an error because the content of 'file1.r' and 'file2.r'
 seems to be missing.
 
 Anyone has the trick to overcome this problem?
 
 Thanks in advance!
 
 best wishes, Johan
 
 
 PS: My function is:
 
 
 run_accumm_value - function(ind_noder_0,
 ind_loc_val,ind_retention,downstream){
 ## Preprocessing of looping calculations:
 koersel_uden_ret - length(unique(ind_noder_0$oplid))
 opsaml_b_0_2 - numeric(koersel_uden_ret)
 opsaml_b_0_2_1 - numeric(koersel_uden_ret)
 opsaml_b_0_2_2 - seq(1:koersel_uden_ret)
 ## Preprocessing of topology and local values to be summed:
 source('preproces_topology.r', local =3D T)
 source('preproces_loc_val.r', local =3D T)
 # Loop for each grouping factor (column in ind_noder_0: oplid):
 for(j in 1:koersel_uden_ret){
 source('matrix_0.r', local =3D T)
 source('matrix.r', local =3D T)
 source('local_value.r', local =3D T)
 source('fordeling.r', local =3D T)
 source('fordeling_manuel.r', local =3D T)
 source('local_ret.r', local =3D T)
 source('Ax=3Db.r', local =3D T)
 source('opsamling_x_0_acc.r', local =3D T)
 }
 source('opsamling_b_1.r', local =3D T)
 opsaml_b_2
 }
 
 
 
 
 
 --=20
 Johan Lassen
 Environment Center Nyk=F8bing F
 Denmark
 
   [[alternative HTML version deleted]]
 
 
 --===0554064772==
 Content-Type: text/plain; charset=us-ascii
 MIME-Version: 1.0
 Content-Transfer-Encoding: 7bit
 Content-Disposition: inline
 
 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.
 
 --===0554064772==--
 
 

-- 

Giovanni Petris  gpet...@uark.edu
Associate Professor
Department of Mathematical Sciences
University of Arkansas - Fayetteville, AR 72701
Ph: (479) 575-6324, 575-8630 (fax)
http://definetti.uark.edu/~gpetris/

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.