Pause script at input from terminal (interactive use)

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

Pause script at input from terminal (interactive use)

Luigi
Dear all,
I am trying to write an interactive script where the user type some
input from the terminal. I used readline() but when I launch the file
with Rscript, the function is overwritten directly, there is no
waiting for the user's input. For instance, this example:

VAR1 = as.numeric(readline(prompt = "Enter something -> "))
VAR2 = as.numeric(readline(prompt = "Enter something else -> "))
if(is.na(VAR1)) VAR1 = 0
if(is.na(VAR2)) VAR2 = "empty"
cat("Input was: ", VAR1, " - ", VAR2, "\n")

is executed till the end without typing anything on terminal :

$ Rscript test.R
Enter something ->
Enter something else ->
Input was:  0  -  empty

I also tried with ',1' at the end of readline, but the effect is the
same. I should use the interactive() function but I am confused on its
use.
It is possible to launch R scritps in the interactive mode in the
first place? and if yes, how? Or would python or julia be better
choices in this case?
Thank you.
--
Best regards,
Luigi

______________________________________________
[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: Pause script at input from terminal (interactive use)

glsnow
I am not an expert on Rscript, but I don't think that an actual
terminal is ever used when using Rscript.  And `interactive()` will
probably always be false.

So if you want the script to pause for input, you need to have some
form of user interface to work with.

One option is to use the tcltk package (this works on all OS's to my
knowledge, but not if you are accessing the computer remotely).  This
answer on stack overflow shows some code that may help:
https://stackoverflow.com/questions/16847621/get-data-out-of-a-tcltk-function/16847918#16847918


On Thu, Apr 18, 2019 at 8:11 AM Luigi Marongiu <[hidden email]> wrote:

>
> Dear all,
> I am trying to write an interactive script where the user type some
> input from the terminal. I used readline() but when I launch the file
> with Rscript, the function is overwritten directly, there is no
> waiting for the user's input. For instance, this example:
>
> VAR1 = as.numeric(readline(prompt = "Enter something -> "))
> VAR2 = as.numeric(readline(prompt = "Enter something else -> "))
> if(is.na(VAR1)) VAR1 = 0
> if(is.na(VAR2)) VAR2 = "empty"
> cat("Input was: ", VAR1, " - ", VAR2, "\n")
>
> is executed till the end without typing anything on terminal :
>
> $ Rscript test.R
> Enter something ->
> Enter something else ->
> Input was:  0  -  empty
>
> I also tried with ',1' at the end of readline, but the effect is the
> same. I should use the interactive() function but I am confused on its
> use.
> It is possible to launch R scritps in the interactive mode in the
> first place? and if yes, how? Or would python or julia be better
> choices in this case?
> Thank you.
> --
> Best regards,
> Luigi
>
> ______________________________________________
> [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.



--
Gregory (Greg) L. Snow Ph.D.
[hidden email]

______________________________________________
[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: Pause script at input from terminal (interactive use)

Luigi
I am realizing as well that R is not the best option for an
interactive session. I changed the script to get the input from a
config file; it is less elegant because the procedure now requires
double the files than with CLI input, but at the end of the day is
more practical when most of the answer remains the same between
sessions. Thanks.

On Thu, Apr 18, 2019 at 8:47 PM Greg Snow <[hidden email]> wrote:

>
> I am not an expert on Rscript, but I don't think that an actual
> terminal is ever used when using Rscript.  And `interactive()` will
> probably always be false.
>
> So if you want the script to pause for input, you need to have some
> form of user interface to work with.
>
> One option is to use the tcltk package (this works on all OS's to my
> knowledge, but not if you are accessing the computer remotely).  This
> answer on stack overflow shows some code that may help:
> https://stackoverflow.com/questions/16847621/get-data-out-of-a-tcltk-function/16847918#16847918
>
>
> On Thu, Apr 18, 2019 at 8:11 AM Luigi Marongiu <[hidden email]> wrote:
> >
> > Dear all,
> > I am trying to write an interactive script where the user type some
> > input from the terminal. I used readline() but when I launch the file
> > with Rscript, the function is overwritten directly, there is no
> > waiting for the user's input. For instance, this example:
> >
> > VAR1 = as.numeric(readline(prompt = "Enter something -> "))
> > VAR2 = as.numeric(readline(prompt = "Enter something else -> "))
> > if(is.na(VAR1)) VAR1 = 0
> > if(is.na(VAR2)) VAR2 = "empty"
> > cat("Input was: ", VAR1, " - ", VAR2, "\n")
> >
> > is executed till the end without typing anything on terminal :
> >
> > $ Rscript test.R
> > Enter something ->
> > Enter something else ->
> > Input was:  0  -  empty
> >
> > I also tried with ',1' at the end of readline, but the effect is the
> > same. I should use the interactive() function but I am confused on its
> > use.
> > It is possible to launch R scritps in the interactive mode in the
> > first place? and if yes, how? Or would python or julia be better
> > choices in this case?
> > Thank you.
> > --
> > Best regards,
> > Luigi
> >
> > ______________________________________________
> > [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.
>
>
>
> --
> Gregory (Greg) L. Snow Ph.D.
> [hidden email]



--
Best regards,
Luigi

______________________________________________
[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: Pause script at input from terminal (interactive use)

Ivan Krylov
In reply to this post by Luigi
On Thu, 18 Apr 2019 16:10:41 +0200
Luigi Marongiu <[hidden email]> wrote:

> It is possible to launch R scritps in the interactive mode in the
> first place? and if yes, how?

One option would be to use littler
<http://dirk.eddelbuettel.com/code/littler.html> with its -i
(--interactive) option.

--
Best regards,
Ivan

______________________________________________
[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: Pause script at input from terminal (interactive use)

Bernard McGarvey
In reply to this post by Luigi
I have used the shiny package to create a web page user interface and it works well.

Bernard
Sent from my iPhone so please excuse the spelling!"

> On Apr 19, 2019, at 1:12 AM, Luigi Marongiu <[hidden email]> wrote:
>
> I am realizing as well that R is not the best option for an
> interactive session. I changed the script to get the input from a
> config file; it is less elegant because the procedure now requires
> double the files than with CLI input, but at the end of the day is
> more practical when most of the answer remains the same between
> sessions. Thanks.
>
>> On Thu, Apr 18, 2019 at 8:47 PM Greg Snow <[hidden email]> wrote:
>>
>> I am not an expert on Rscript, but I don't think that an actual
>> terminal is ever used when using Rscript.  And `interactive()` will
>> probably always be false.
>>
>> So if you want the script to pause for input, you need to have some
>> form of user interface to work with.
>>
>> One option is to use the tcltk package (this works on all OS's to my
>> knowledge, but not if you are accessing the computer remotely).  This
>> answer on stack overflow shows some code that may help:
>> https://stackoverflow.com/questions/16847621/get-data-out-of-a-tcltk-function/16847918#16847918
>>
>>
>>> On Thu, Apr 18, 2019 at 8:11 AM Luigi Marongiu <[hidden email]> wrote:
>>>
>>> Dear all,
>>> I am trying to write an interactive script where the user type some
>>> input from the terminal. I used readline() but when I launch the file
>>> with Rscript, the function is overwritten directly, there is no
>>> waiting for the user's input. For instance, this example:
>>>
>>> VAR1 = as.numeric(readline(prompt = "Enter something -> "))
>>> VAR2 = as.numeric(readline(prompt = "Enter something else -> "))
>>> if(is.na(VAR1)) VAR1 = 0
>>> if(is.na(VAR2)) VAR2 = "empty"
>>> cat("Input was: ", VAR1, " - ", VAR2, "\n")
>>>
>>> is executed till the end without typing anything on terminal :
>>>
>>> $ Rscript test.R
>>> Enter something ->
>>> Enter something else ->
>>> Input was:  0  -  empty
>>>
>>> I also tried with ',1' at the end of readline, but the effect is the
>>> same. I should use the interactive() function but I am confused on its
>>> use.
>>> It is possible to launch R scritps in the interactive mode in the
>>> first place? and if yes, how? Or would python or julia be better
>>> choices in this case?
>>> Thank you.
>>> --
>>> Best regards,
>>> Luigi
>>>
>>> ______________________________________________
>>> [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.
>>
>>
>>
>> --
>> Gregory (Greg) L. Snow Ph.D.
>> [hidden email]
>
>
>
> --
> Best regards,
> Luigi
>
> ______________________________________________
> [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.

______________________________________________
[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: Pause script at input from terminal (interactive use)

PIKAL Petr
In reply to this post by Luigi
Hi

Maybe you could think about transfering **script** to **function**.

In function your construction seems to be OK.

Cheers
Petr

> -----Original Message-----
> From: R-help <[hidden email]> On Behalf Of Luigi Marongiu
> Sent: Thursday, April 18, 2019 4:11 PM
> To: r-help <[hidden email]>
> Subject: [R] Pause script at input from terminal (interactive use)
>
> Dear all,
> I am trying to write an interactive script where the user type some input from
> the terminal. I used readline() but when I launch the file with Rscript, the
> function is overwritten directly, there is no waiting for the user's input. For
> instance, this example:
>
> VAR1 = as.numeric(readline(prompt = "Enter something -> "))
> VAR2 = as.numeric(readline(prompt = "Enter something else -> "))
> if(is.na(VAR1)) VAR1 = 0
> if(is.na(VAR2)) VAR2 = "empty"
> cat("Input was: ", VAR1, " - ", VAR2, "\n")
>
> is executed till the end without typing anything on terminal :
>
> $ Rscript test.R
> Enter something ->
> Enter something else ->
> Input was:  0  -  empty
>
> I also tried with ',1' at the end of readline, but the effect is the same. I should
> use the interactive() function but I am confused on its use.
> It is possible to launch R scritps in the interactive mode in the first place? and if
> yes, how? Or would python or julia be better choices in this case?
> Thank you.
> --
> Best regards,
> Luigi
>
> ______________________________________________
> [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.
Osobní údaje: Informace o zpracování a ochraně osobních údajů obchodních partnerů PRECHEZA a.s. jsou zveřejněny na: https://www.precheza.cz/zasady-ochrany-osobnich-udaju/ | Information about processing and protection of business partner’s personal data are available on website: https://www.precheza.cz/en/personal-data-protection-principles/
Důvěrnost: Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a podléhají tomuto právně závaznému prohláąení o vyloučení odpovědnosti: https://www.precheza.cz/01-dovetek/ | This email and any documents attached to it may be confidential and are subject to the legally binding disclaimer: https://www.precheza.cz/en/01-disclaimer/

______________________________________________
[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: Pause script at input from terminal (interactive use)

Luigi
Thank you, I'll try that!

On Tue, Apr 23, 2019 at 9:22 AM PIKAL Petr <[hidden email]> wrote:

>
> Hi
>
> Maybe you could think about transfering **script** to **function**.
>
> In function your construction seems to be OK.
>
> Cheers
> Petr
>
> > -----Original Message-----
> > From: R-help <[hidden email]> On Behalf Of Luigi Marongiu
> > Sent: Thursday, April 18, 2019 4:11 PM
> > To: r-help <[hidden email]>
> > Subject: [R] Pause script at input from terminal (interactive use)
> >
> > Dear all,
> > I am trying to write an interactive script where the user type some input from
> > the terminal. I used readline() but when I launch the file with Rscript, the
> > function is overwritten directly, there is no waiting for the user's input. For
> > instance, this example:
> >
> > VAR1 = as.numeric(readline(prompt = "Enter something -> "))
> > VAR2 = as.numeric(readline(prompt = "Enter something else -> "))
> > if(is.na(VAR1)) VAR1 = 0
> > if(is.na(VAR2)) VAR2 = "empty"
> > cat("Input was: ", VAR1, " - ", VAR2, "\n")
> >
> > is executed till the end without typing anything on terminal :
> >
> > $ Rscript test.R
> > Enter something ->
> > Enter something else ->
> > Input was:  0  -  empty
> >
> > I also tried with ',1' at the end of readline, but the effect is the same. I should
> > use the interactive() function but I am confused on its use.
> > It is possible to launch R scritps in the interactive mode in the first place? and if
> > yes, how? Or would python or julia be better choices in this case?
> > Thank you.
> > --
> > Best regards,
> > Luigi
> >
> > ______________________________________________
> > [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.
> Osobní údaje: Informace o zpracování a ochraně osobních údajů obchodních partnerů PRECHEZA a.s. jsou zveřejněny na: https://www.precheza.cz/zasady-ochrany-osobnich-udaju/ | Information about processing and protection of business partner’s personal data are available on website: https://www.precheza.cz/en/personal-data-protection-principles/
> Důvěrnost: Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a podléhají tomuto právně závaznému prohláąení o vyloučení odpovědnosti: https://www.precheza.cz/01-dovetek/ | This email and any documents attached to it may be confidential and are subject to the legally binding disclaimer: https://www.precheza.cz/en/01-disclaimer/
>


--
Best regards,
Luigi

______________________________________________
[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.