(console) in R?

I am sure that you all know the "hit return to show next plot" statement when you execute the plot command on the regression object. I wonder how I can independently interact in this? I found several posts on the mailing list, but nothing really is exhaustive. This mainly concerned the menu () and various graphical interfaces of the OS. I just want to create something like:

Please enter sample size n: > 1000 #execution of rnorm(1000) 

Maybe I just skipped some of the documentation and just can't find the right words for google ...

+4
source share
2 answers

Not readLines , but readline .

 n <- as.integer(readline(prompt = "Please enter sample size > ")) 

A slightly more spectacular implementation:

 read_value <- function(prompt_text = "", prompt_suffix = getOption("prompt"), coerce_to = "character") { prompt <- paste(prompt_text, prompt_suffix) as(readline(prompt), coerce_to) } read_value("Please enter sample size", coerce_to = "integer") 
+5
source

You can use readLines , but I'm sure there are other ways ...

 ask = function( prompt ) { cat( paste( prompt, ':' ) ) readLines( n=1 ) } n = as.integer( ask( 'Please enter sample size n' ) ) 
+1
source

Source: https://habr.com/ru/post/1347885/


All Articles