Outputting variable names and their value bindings

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Outputting variable names and their value bindings

Simon Ellis
Hullo,

I'm writing a piece of scripting glue for a colleague who is doing
computations in several different languages. (It's the most convenient way,
right now.) My system calls the relevant program (e.g. Rstudio, MATLAB)
with a path to a script, captures stdout and parses it for output
variables, which it stores in its own environment for use later on.

This is easy with MATLAB, since it writes back the variable name with its
value, e.g.:

> freq = {somefunction}()
freq =

    <value>

All I have to do is look for lines with '=' on the end, then grab the next
section of non-empty lines as the binding for the variable. Boom.

With Rscript, if I write something like this:

Rscript -e "a = (2 + 2)" -e "a"

it prints

[1] 4

Is there any way to get R to print output similarly to MATLAB, in an x = y
format?

I have other solutions in mind, but they're all kludgy and I'd rather not
have to. Please can someone save me from the kludge? :-D

Thank you,

~Simon Ellis


--
Visiting Assistant Professor,
Department of Computer Science,
Vassar College,
Poughkeepsie, NY 12604

“The whole modern world has divided itself into Conservatives and
Progressives. The business of Progressives is to go on making mistakes. The
business of Conservatives is to prevent mistakes from being corrected.” *--
G. K. Chesterton*

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Outputting variable names and their value bindings

Jeff Newmiller
Yes and no. R does not have a "Matlab-output-compatibility" mode, but you can write your script to output anything you want it to using the "cat" function with various functions like "sprintf" and "as.character". You may want to write some functions that format some common objects that you typically output. Then just make sure to use those functions instead of the standard "put an object alone on a line" method of printing.

On June 24, 2018 11:00:51 AM PDT, Simon Ellis <[hidden email]> wrote:

>Hullo,
>
>I'm writing a piece of scripting glue for a colleague who is doing
>computations in several different languages. (It's the most convenient
>way,
>right now.) My system calls the relevant program (e.g. Rstudio, MATLAB)
>with a path to a script, captures stdout and parses it for output
>variables, which it stores in its own environment for use later on.
>
>This is easy with MATLAB, since it writes back the variable name with
>its
>value, e.g.:
>
>> freq = {somefunction}()
>freq =
>
>    <value>
>
>All I have to do is look for lines with '=' on the end, then grab the
>next
>section of non-empty lines as the binding for the variable. Boom.
>
>With Rscript, if I write something like this:
>
>Rscript -e "a = (2 + 2)" -e "a"
>
>it prints
>
>[1] 4
>
>Is there any way to get R to print output similarly to MATLAB, in an x
>= y
>format?
>
>I have other solutions in mind, but they're all kludgy and I'd rather
>not
>have to. Please can someone save me from the kludge? :-D
>
>Thank you,
>
>~Simon Ellis

--
Sent from my phone. Please excuse my brevity.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Outputting variable names and their value bindings

Simon Ellis
Thank you for your reply.

At the moment, my colleague and her students are just using
zero-dimensional variables for output, no vectors or matrices, which does
make my life easier.

Since my code-glue parses through the scripts' code to substitute variables
as required, I could code a command to cause my system to emit a line to
get R to print something I could use.

I am sure this is a dumb question, but is there a reference manual for R
available online?

On 24 June 2018 at 16:14, Jeff Newmiller <[hidden email]> wrote:

> Yes and no. R does not have a "Matlab-output-compatibility" mode, but you
> can write your script to output anything you want it to using the "cat"
> function with various functions like "sprintf" and "as.character". You may
> want to write some functions that format some common objects that you
> typically output. Then just make sure to use those functions instead of the
> standard "put an object alone on a line" method of printing.
>
> On June 24, 2018 11:00:51 AM PDT, Simon Ellis <[hidden email]> wrote:
> >Hullo,
> >
> >I'm writing a piece of scripting glue for a colleague who is doing
> >computations in several different languages. (It's the most convenient
> >way,
> >right now.) My system calls the relevant program (e.g. Rstudio, MATLAB)
> >with a path to a script, captures stdout and parses it for output
> >variables, which it stores in its own environment for use later on.
> >
> >This is easy with MATLAB, since it writes back the variable name with
> >its
> >value, e.g.:
> >
> >> freq = {somefunction}()
> >freq =
> >
> >    <value>
> >
> >All I have to do is look for lines with '=' on the end, then grab the
> >next
> >section of non-empty lines as the binding for the variable. Boom.
> >
> >With Rscript, if I write something like this:
> >
> >Rscript -e "a = (2 + 2)" -e "a"
> >
> >it prints
> >
> >[1] 4
> >
> >Is there any way to get R to print output similarly to MATLAB, in an x
> >= y
> >format?
> >
> >I have other solutions in mind, but they're all kludgy and I'd rather
> >not
> >have to. Please can someone save me from the kludge? :-D
> >
> >Thank you,
> >
> >~Simon Ellis
>
> --
> Sent from my phone. Please excuse my brevity.
>



--
Visiting Assistant Professor,
Department of Computer Science,
Vassar College,
Poughkeepsie, NY 12604

“The whole modern world has divided itself into Conservatives and
Progressives. The business of Progressives is to go on making mistakes. The
business of Conservatives is to prevent mistakes from being corrected.” *--
G. K. Chesterton*

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Outputting variable names and their value bindings

Jeff Newmiller
Yes [1], though most people use it interactively, e.g.

?cat
?sprintf

[1] https://cran.r-project.org/manuals.html

On June 24, 2018 4:31:40 PM PDT, Simon Ellis <[hidden email]> wrote:

>Thank you for your reply.
>
>At the moment, my colleague and her students are just using
>zero-dimensional variables for output, no vectors or matrices, which
>does
>make my life easier.
>
>Since my code-glue parses through the scripts' code to substitute
>variables
>as required, I could code a command to cause my system to emit a line
>to
>get R to print something I could use.
>
>I am sure this is a dumb question, but is there a reference manual for
>R
>available online?
>
>On 24 June 2018 at 16:14, Jeff Newmiller <[hidden email]>
>wrote:
>
>> Yes and no. R does not have a "Matlab-output-compatibility" mode, but
>you
>> can write your script to output anything you want it to using the
>"cat"
>> function with various functions like "sprintf" and "as.character".
>You may
>> want to write some functions that format some common objects that you
>> typically output. Then just make sure to use those functions instead
>of the
>> standard "put an object alone on a line" method of printing.
>>
>> On June 24, 2018 11:00:51 AM PDT, Simon Ellis <[hidden email]>
>wrote:
>> >Hullo,
>> >
>> >I'm writing a piece of scripting glue for a colleague who is doing
>> >computations in several different languages. (It's the most
>convenient
>> >way,
>> >right now.) My system calls the relevant program (e.g. Rstudio,
>MATLAB)
>> >with a path to a script, captures stdout and parses it for output
>> >variables, which it stores in its own environment for use later on.
>> >
>> >This is easy with MATLAB, since it writes back the variable name
>with
>> >its
>> >value, e.g.:
>> >
>> >> freq = {somefunction}()
>> >freq =
>> >
>> >    <value>
>> >
>> >All I have to do is look for lines with '=' on the end, then grab
>the
>> >next
>> >section of non-empty lines as the binding for the variable. Boom.
>> >
>> >With Rscript, if I write something like this:
>> >
>> >Rscript -e "a = (2 + 2)" -e "a"
>> >
>> >it prints
>> >
>> >[1] 4
>> >
>> >Is there any way to get R to print output similarly to MATLAB, in an
>x
>> >= y
>> >format?
>> >
>> >I have other solutions in mind, but they're all kludgy and I'd
>rather
>> >not
>> >have to. Please can someone save me from the kludge? :-D
>> >
>> >Thank you,
>> >
>> >~Simon Ellis
>>
>> --
>> Sent from my phone. Please excuse my brevity.
>>

--
Sent from my phone. Please excuse my brevity.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Outputting variable names and their value bindings

Jim Lemon-4
In reply to this post by Simon Ellis
Hi Simon,
Easy to do if you call "print" directly:

print<-function(x) cat(deparse(substitute(x)),"=\n",x,"\n")
y<-3
print(y)
y =
 3

Obviously you will want to get rid of your print function when it is
not being used with "rm" or by starting a new session. Getting it to
bypass the default print method is more difficult and I don't have the
time to untangle that one at the moment.

Jim

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Outputting variable names and their value bindings

Simon Ellis
Hullo Jim,

That's wonderful: thank you so much! That makes things even easier. There
are also no problems regarding getting rid of the function as each set of
computations occurs in a new Rstudio process: it's not optimal, but given
the problem it was the best solution. My code will just stick this print
function at the very top of each R script, and then I can get my
collaborator to call it where there are variables to be read out.

This is great. Thank you again! :-)

On 24 June 2018 at 20:37, Jim Lemon <[hidden email]> wrote:

> Hi Simon,
> Easy to do if you call "print" directly:
>
> print<-function(x) cat(deparse(substitute(x)),"=\n",x,"\n")
> y<-3
> print(y)
> y =
>  3
>
> Obviously you will want to get rid of your print function when it is
> not being used with "rm" or by starting a new session. Getting it to
> bypass the default print method is more difficult and I don't have the
> time to untangle that one at the moment.
>
> Jim
>



--
Visiting Assistant Professor,
Department of Computer Science,
Vassar College,
Poughkeepsie, NY 12604

“The whole modern world has divided itself into Conservatives and
Progressives. The business of Progressives is to go on making mistakes. The
business of Conservatives is to prevent mistakes from being corrected.” *--
G. K. Chesterton*

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.