hodgesse1 / rfortran

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

Reval fails to execute multiple R statements within same command #98

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
*** What steps will reproduce the problem?

Hi,

It appears that Reval cannot be used to execute more than a single R command 
per call?

Eg, the single command needed to be executed in R via RFortran is as below 
(note that it consists of two R statements separated by ;):

cmd="a=5; b=7"

This executes correctly when pasted directly into R.

However, when the same command is executed via RFortran, using Reval(cmd), it 
does NOT work. 

There is no error reported by Reval (err=0), but it only executes the first 
statement (a=5), but not the second (b=7).

I did about some debugging with no obvious leads...

Can it be confirmed that Reval cant handle more than a single R statement? I 
think I saw some examples with multiple statements?

If Rval only works for the first statement, an RFortran wrapper should be 
created that parses multiple R statements into a vector and loops through them, 
since a single R-command with several statements is a scenario needed to be 
supported in BATEA, etc. There is a DMSL parsing routine that can help here..

cheers,
d

*** What version of RFortran are you using?

r628

*** On which Windows operating system?

Win XP

*** What Fortran compiler (name and version number) are you using?

IVF 11.0

*** What version of R are you using?

R 2.11.1

*** If you have a coding solution to this defect please include a svn patch
as an attachment

*** Please provide any additional information below.

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

GoogleCodeExporter commented 8 years ago
See solution below. to use it, rename the old 'Reval' to 'Reval_engine' and 
paste the following right after. It resolves the issue in the ticket.

    !----------------------------------------------------
    function Reval(command)result(err)
    ! Purpose: Reval that handles multiple R statements within a single argument.
    ! Programmer: Dmitri Kavetski
    ! Kreated: 13 June 2011 AD, EMPA Accomo
    ! Comments:
    ! 1. Following R conventions, multiple statements must be separated by ";".
    use utilities_dmsl_kit,only:parseStringIntoVector
    implicit none
    ! dummies
    character(*),intent(in)::command
    integer(mik)::err
    ! locals
    integer(mik)::n,i
    character(len(command)),pointer::cmdv(:)
    character(len_DMSLjmsg)::msg
    ! Start procedure here
    call parseStringIntoVector(string=command,delim=";",&
      array=cmdv,err=err,message=msg)
    n=size(cmdv)
    do i=1,n
      err=Reval_eng(trim(cmdv(i)))
      if(err/=EXIT_SUCCESS)exit
    enddo
    ! End procedure here
    endfunction Reval

Original comment by dmitri.k...@gmail.com on 13 Jun 2011 at 3:20

GoogleCodeExporter commented 8 years ago
Hi Mark,

Since some legitimate R statements use the ";" inside (rather than as a 
separator), I modified the code to expect ";;" as a separator. Slightly less 
convenient but seems to work fine.

The instructions and new code are below. Also see the related BATEAU_DK ticket 
#109 for some further details.

In the file 'RFortran_Rput_Rget_Reval_Rcall.F90', do the following

 (i)  rename the function 'Reval' to 'Reval_eng'

 (ii) paste the following code after the function 'Reval_eng'

 !start{\DK}---------------------------------------
     !----------------------------------------------------
     function Reval(command,forceSingle)result(err)
     ! Purpose: Reval that handles multiple R statements within a single argument.
     ! Programmer: Dmitri Kavetski
     ! Kreated: 13 June 2011 AD, EMPA Accomo
     ! Comments:
     ! 1. Following R conventions, multiple statements must be separated by ";",
     !    However, to avoid spurious parsing, a ";;" is used.
     use utilities_dmsl_kit,only:quickif,parseStringIntoVector
     implicit none
     ! dummies
     character(*),intent(in)::command
     logical(mlk),intent(in),optional::forceSingle
     integer(mik)::err
     ! locals
     integer(mik)::n,i
     character(len(command)),pointer::cmdv(:)
     character(len_DMSLjmsg)::msg
     logical(mlk),parameter::forceSingleDef=.false.
     character(*),parameter::delim=";;"
     ! Start procedure here
     if(quickif(forceSingle,forceSingleDef))then
         err=Reval_eng(command)
     else
       call parseStringIntoVector(string=command,delim=delim,&
         array=cmdv,err=err,message=msg)
       if(err/=EXIT_SUCCESS)return
       n=size(cmdv)
       do i=1,n
         err=Reval_eng(trim(cmdv(i)))
         if(err/=EXIT_SUCCESS)exit
       enddo
     endif
     ! End procedure here
     endfunction Reval
 !end{\DK}-----------------------------------------

Original comment by dmitri.k...@gmail.com on 15 Jun 2011 at 11:42

GoogleCodeExporter commented 8 years ago
Hi Mark,

In response to your earlier query:

yes, it appears that R allows the delimiter ";" (which normally separates 
multiple statements on the same line) within some expressions (ie, outside 
strings).

This certainly seems confusing to me, and perhaps error-prone, but thats what R 
seems to allow. Eg, see line 671 of RPlotLibrary in BATEA. Unless I misread 
something, you can see the ";" inside a single R statement.

cheers,
d

Original comment by dmitri.k...@gmail.com on 12 Jul 2011 at 11:51