'scan' in a script?

8 messages Options
Embed this post
Permalink
Mark Knecht

'scan' in a script?

Reply Threaded More More options
Print post
Permalink
When I use the scan function in the Rgui console it works as expected.
However it seems that when I put the same command in a script file it
doesn't wait for input.

Is there an option to scan to make it wait for input when used in a
script? Or is there possibly a different function that will do in a
script the same sort of thing as scan does in the console?

Thanks,
Mark

______________________________________________
[hidden email] 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.
jholtman

Re: 'scan' in a script?

Reply Threaded More More options
Print post
Permalink
Exactly how are you using it?  How you executing your script?  Are you
using cut/paste or source?  Have you tried 'readline'?

More details on what you mean by it does not work as expected.

On Thu, Jul 9, 2009 at 2:24 PM, Mark Knecht<[hidden email]> wrote:

> When I use the scan function in the Rgui console it works as expected.
> However it seems that when I put the same command in a script file it
> doesn't wait for input.
>
> Is there an option to scan to make it wait for input when used in a
> script? Or is there possibly a different function that will do in a
> script the same sort of thing as scan does in the console?
>
> Thanks,
> Mark
>
> ______________________________________________
> [hidden email] 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.
>



--
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?

______________________________________________
[hidden email] 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.
Mark Knecht

Re: 'scan' in a script?

Reply Threaded More More options
Print post
Permalink
Jim,
   In Rgui there is a feature to create a new script so I open that
and get a blank editor. I put these commands in the script

MyNames = scan(what="")
MyNames

   Whether I ask Rgui to run the whole script as a script - not
sourced - it runs the first command, doesn't wait for input and then
runs the second command. I see

> MyNames
character(0)
>

   If I right click the first line and ask it to run the selection or
line then R waits for the input.

   If I source the file then the opposite happens. It does the scan
correctly, waiting for me to make entries. When I finish it doesn't
print what's in MyNames, as shown here where I typed in happy sad test
go and hit enter.

> source("C:\\Users\\Mark\\Documents\\R\\Test Cases\\scan from script.R")
1: happy sad test go
5:
Read 4 items
>

   Possibly (probably!) this is a misunderstanding on my part, but
it's something I need to start looking at as the reason for me
checking out R at all was as a potential way to develop a complete
program, as opposed to writing it in C, so I need to start looking at
how to give it input (file names, directory paths, user requests)
other than changing source code.

   I hope this is clear enough and I appreciate your inputs.

Cheers,
Mark

On Thu, Jul 9, 2009 at 1:02 PM, jim holtman<[hidden email]> wrote:

> Exactly how are you using it?  How you executing your script?  Are you
> using cut/paste or source?  Have you tried 'readline'?
>
> More details on what you mean by it does not work as expected.
>
> On Thu, Jul 9, 2009 at 2:24 PM, Mark Knecht<[hidden email]> wrote:
>> When I use the scan function in the Rgui console it works as expected.
>> However it seems that when I put the same command in a script file it
>> doesn't wait for input.
>>
>> Is there an option to scan to make it wait for input when used in a
>> script? Or is there possibly a different function that will do in a
>> script the same sort of thing as scan does in the console?
>>
>> Thanks,
>> Mark
>>
>> ______________________________________________
>> [hidden email] 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.
>>
>
>
>
> --
> Jim Holtman
> Cincinnati, OH
> +1 513 646 9390
>
> What is the problem that you are trying to solve?
>

______________________________________________
[hidden email] 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.
Gabor Grothendieck

Re: 'scan' in a script?

Reply Threaded More More options
Print post
Permalink
In reply to this post by Mark Knecht
Check out:

http://tolstoy.newcastle.edu.au/R/help/03a/6855.html

On Thu, Jul 9, 2009 at 2:24 PM, Mark Knecht<[hidden email]> wrote:

> When I use the scan function in the Rgui console it works as expected.
> However it seems that when I put the same command in a script file it
> doesn't wait for input.
>
> Is there an option to scan to make it wait for input when used in a
> script? Or is there possibly a different function that will do in a
> script the same sort of thing as scan does in the console?
>
> Thanks,
> Mark
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
jholtman

Re: 'scan' in a script?

Reply Threaded More More options
Print post
Permalink
In reply to this post by Mark Knecht
If you want the value printed in the script, then use the 'print' function:

print(MyNames)

There is an implicit 'print' when you type an object's name at the
command line, but with you have it within a script, you have to
explicitly print it.

On Thu, Jul 9, 2009 at 5:08 PM, Mark Knecht<[hidden email]> wrote:

> Jim,
>   In Rgui there is a feature to create a new script so I open that
> and get a blank editor. I put these commands in the script
>
> MyNames = scan(what="")
> MyNames
>
>   Whether I ask Rgui to run the whole script as a script - not
> sourced - it runs the first command, doesn't wait for input and then
> runs the second command. I see
>
>> MyNames
> character(0)
>>
>
>   If I right click the first line and ask it to run the selection or
> line then R waits for the input.
>
>   If I source the file then the opposite happens. It does the scan
> correctly, waiting for me to make entries. When I finish it doesn't
> print what's in MyNames, as shown here where I typed in happy sad test
> go and hit enter.
>
>> source("C:\\Users\\Mark\\Documents\\R\\Test Cases\\scan from script.R")
> 1: happy sad test go
> 5:
> Read 4 items
>>
>
>   Possibly (probably!) this is a misunderstanding on my part, but
> it's something I need to start looking at as the reason for me
> checking out R at all was as a potential way to develop a complete
> program, as opposed to writing it in C, so I need to start looking at
> how to give it input (file names, directory paths, user requests)
> other than changing source code.
>
>   I hope this is clear enough and I appreciate your inputs.
>
> Cheers,
> Mark
>
> On Thu, Jul 9, 2009 at 1:02 PM, jim holtman<[hidden email]> wrote:
>> Exactly how are you using it?  How you executing your script?  Are you
>> using cut/paste or source?  Have you tried 'readline'?
>>
>> More details on what you mean by it does not work as expected.
>>
>> On Thu, Jul 9, 2009 at 2:24 PM, Mark Knecht<[hidden email]> wrote:
>>> When I use the scan function in the Rgui console it works as expected.
>>> However it seems that when I put the same command in a script file it
>>> doesn't wait for input.
>>>
>>> Is there an option to scan to make it wait for input when used in a
>>> script? Or is there possibly a different function that will do in a
>>> script the same sort of thing as scan does in the console?
>>>
>>> Thanks,
>>> Mark
>>>
>>> ______________________________________________
>>> [hidden email] 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.
>>>
>>
>>
>>
>> --
>> Jim Holtman
>> Cincinnati, OH
>> +1 513 646 9390
>>
>> What is the problem that you are trying to solve?
>>
>



--
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?

______________________________________________
[hidden email] 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.
Mark Knecht

Re: 'scan' in a script?

Reply Threaded More More options
Print post
Permalink
On Thu, Jul 9, 2009 at 6:05 PM, jim holtman<[hidden email]> wrote:
> If you want the value printed in the script, then use the 'print' function:
>
> print(MyNames)
>
> There is an implicit 'print' when you type an object's name at the
> command line, but with you have it within a script, you have to
> explicitly print it.
>
<SNIP>

That's good to know in terms of reliably getting the data out but my
bigger problem right now is it doesn't wait for me to type any input
and therefore my program (running as a script) doesn't know what to
do.

I guess this must be me doing somethign jsut plain wrong. I figured
I'd ask and get an answer in 30 seconds. Strange I'm that far off. If
I need to run it in the interpreter instead of source it then for now
I can do that until I learn what I'm doing wrong.

I've got more interesting questions of which the answers are far more
important to me than this one!

Thanks,
Mark

______________________________________________
[hidden email] 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.
Daniel Nordlund-2

Re: 'scan' in a script?

Reply Threaded More More options
Print post
Permalink
> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of Mark Knecht
> Sent: Thursday, July 09, 2009 9:35 PM
> To: jim holtman
> Cc: r-help
> Subject: Re: [R] 'scan' in a script?
>
> On Thu, Jul 9, 2009 at 6:05 PM, jim holtman<[hidden email]> wrote:
> > If you want the value printed in the script, then use the
> 'print' function:
> >
> > print(MyNames)
> >
> > There is an implicit 'print' when you type an object's name at the
> > command line, but with you have it within a script, you have to
> > explicitly print it.
> >
> <SNIP>
>
> That's good to know in terms of reliably getting the data out but my
> bigger problem right now is it doesn't wait for me to type any input
> and therefore my program (running as a script) doesn't know what to
> do.
>
> I guess this must be me doing somethign jsut plain wrong. I figured
> I'd ask and get an answer in 30 seconds. Strange I'm that far off. If
> I need to run it in the interpreter instead of source it then for now
> I can do that until I learn what I'm doing wrong.
>
> I've got more interesting questions of which the answers are far more
> important to me than this one!
>
> Thanks,
> Mark

Mark,

Take a look at the readline() function.

Hope this is helpful,

Dan

Daniel Nordlund
Bothell, WA USA

______________________________________________
[hidden email] 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.
jholtman

Re: 'scan' in a script?

Reply Threaded More More options
Print post
Permalink
In reply to this post by Mark Knecht
I have no problem running a script that uses readline when the script
is 'source'd in.  If you are going a cut/paste to the console, then it
is using the standard input and readline (or any other input that you
would expect to be able to enter from the console) will start reading
your source.  Here is an example:
I have a script consisting of:

x <- readline("Input your data:")
print(x)


I first cut/paste it


> # this is the cut/paste
> x <- readline("Input your data:")
Input your data:print(x)     <<<<== notice it read the next command
>
> x  # output 'x';  entered at the console; look at what is output
[1] "print(x)"

# now 'source' it
> source(file('clipboard'))
Input your data:my input data
[1] "my input data"
>

Notice that the above did print the input I entered "my input data"


So determine how you are invoking your script.  You should be using 'source'


On Fri, Jul 10, 2009 at 12:35 AM, Mark Knecht<[hidden email]> wrote:

> On Thu, Jul 9, 2009 at 6:05 PM, jim holtman<[hidden email]> wrote:
>> If you want the value printed in the script, then use the 'print' function:
>>
>> print(MyNames)
>>
>> There is an implicit 'print' when you type an object's name at the
>> command line, but with you have it within a script, you have to
>> explicitly print it.
>>
> <SNIP>
>
> That's good to know in terms of reliably getting the data out but my
> bigger problem right now is it doesn't wait for me to type any input
> and therefore my program (running as a script) doesn't know what to
> do.
>
> I guess this must be me doing somethign jsut plain wrong. I figured
> I'd ask and get an answer in 30 seconds. Strange I'm that far off. If
> I need to run it in the interpreter instead of source it then for now
> I can do that until I learn what I'm doing wrong.
>
> I've got more interesting questions of which the answers are far more
> important to me than this one!
>
> Thanks,
> Mark
>



--
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?

______________________________________________
[hidden email] 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.