hodgesse1 / rfortran

Automatically exported from code.google.com/p/rfortran
0 stars 0 forks source link

RFortran utility to erase (clear) variables in R #96

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
*** Please provide a description of the enhancement you are requesting.

It would be very useful to have an RFortran utility to erase variables from R. 
For example, when cleaning up the name space.

This is especially needed when several Fortran subroutines may create several R 
variables with the same name. In this case, as well as in general, a routine 
must cleanup after itself, otherwise things can get v messy.

The code below illustrates the DMSL routine. However it should really migrate 
somewhere into RFortran.

*** If you have a coding solution to this enhancement please include the
file (or preferably a svn patch) as an attachment

!----------------------------------------------------
subroutine rfortranClearVars(varname,err,message)
! Purpose: Removes variables named 'varname' from the R environment.
! ---
! Programmer: Dmitri Kavetski
! Kreated: 12 June 2011 AD, EMPA Accomo
! ---
! Performance
! IN:
! OUT:
! Comments:
use utilities_dmsl_kit,only:quickConcat
use rfortran,only:Reval
implicit none
! dummies
character(*),intent(in),optional::varname(:)
integer(mik),intent(out)::err
character(*),intent(out)::message
! locals
character(*),parameter::procnam="rfortranClearVars"
character(len_DMSLpath)::rmList
! Start procedure here
if(present(varname))then
  rmList=quickConcat(string=varname,sep=",",trimString=.true.,innerOnly=.true.)
  err=Reval("rm("//trim(rmList)//")")
else
  err=Reval("rm(list=ls())")
endif
if(err/=0)then
  message="f-"//procnam//"/extError[Reval/cmd]/checkRFortranlog"
  err=100; return
endif
! End procedure here
endsubroutine rfortranClearVars
!----------------------------------------------------

Original issue reported on code.google.com by dmitri.k...@gmail.com on 12 Jun 2011 at 12:12

GoogleCodeExporter commented 8 years ago
This is good idea, However it could also be achieved without a seperate Fortran 
function, using a single Rcall function: 

Case 1: Specific vars

ok=Rcall(vars=(/Rput("varname",varname)/),cmd=(/"rm(list=varname)","rm(varname)"
/))

Case 2: All vars

ok=Reval('rm(list=ls())')

Mark

Original comment by mark.th...@gmail.com on 12 Jun 2011 at 5:03

GoogleCodeExporter commented 8 years ago
Hi Mark,

Yes, but the question is do you want to have a mixture of R and Fortran 
throughout the code, or try to wrap them so that the Rputs etc are kept 
(lagely) separate. My own preference is for the latter, especially for common 
tasks.

d

Original comment by dmitri.k...@gmail.com on 13 Jun 2011 at 9:38

GoogleCodeExporter commented 8 years ago
RFortran r700 adds the utility function 'RFortran_clearVars' to 
'RFortran_RFuncs' designed by D for this purpose.

I recommend in the next release of RFortran this function be generalized to 
handle scalar arguments, and be added to the core function module.

This ensures it is possible to use RFortran without adding any R code into the 
Fortran code (Rput -> execute scripts via Rcall -> Rget -> Rclear).

cheers,
d

Original comment by dmitri.k...@gmail.com on 17 Jul 2011 at 9:29