Hi guys, My concern is to create an automated process from the beginning to the end. I want to copy all my code together in one piece and paste it to R console and sit back and relax :) except one moment in which the program should ask me to enter a number.. and only then (only after getting a valid number from me) it should continue to read and process the rest of the code. I tried lots of things (readline, readLines, scan, interactive, ask, switch,...) and read manuals and searched help forums.. I found several similar questions but never a satisfying answer.. so now it became a challenge.. any idea how to overcome this problem (R 2.11.1 for Windows)? (an example code is below) Best, Fatih library(gtools) library(YaleToolkit) library(xts) ### start of my wrong function! f<-function(w){ w<-readline("which data? ") w<-as.numeric(w) ifelse(is.numeric(w)=="TRUE", w, f()) } f() ##### end of my wrong function v<- ## and output of my function should be a "v" for example which I can use it in the next line (v<-w or something like that??) ##the rest works fine p<-paste("t", v, ".txt", sep = "") t<-read.table(p, header=FALSE, sep="\t", dec=",", blank.lines.skip=FALSE) rownames(t)<-as.Date(t[,1],"%d.%m.%Y") colnames(t)<-c("date","start","high","low","end","w.average","lot", "volume") x<-as.xts(t) whatis(x) . . [[alternative HTML version deleted]]
Hi Fatih I believe that readLines(n=1) will do the job. It works fine from the Windows RGui, but I noticed that it hangs my Aquamacs/ESS when R runs from there, and a C-g was needed (which may be completely irrelevant to you). Best, Niels On 30/09/10 08.55, Pam wrote:> Hi guys, > > My concern is to create an automated process from the beginning to the end. I > want to copy all my code together in one piece and paste it to R console and sit > back and relax :) except one moment in which the program should ask me to enter > a number.. and only then (only after getting a valid number from me) it should > continue to read and process the rest of the code. I?tried?lots of things > (readline, readLines, scan, interactive, ask, switch,...) and read manuals and > searched help forums.. I found several similar questions but never a satisfying > answer.. so now it became a challenge.. any idea how?to?overcome this problem (R > 2.11.1 for Windows)? (an example?code is below)? > > Best, > Fatih > > > > library(gtools) > library(YaleToolkit) > library(xts) > ? > ### start of my?wrong?function! > f<-function(w){ > ?w<-readline("which data? ") > ?w<-as.numeric(w) > ?ifelse(is.numeric(w)=="TRUE", w, f()) > ?} > f() > ##### end of my?wrong function > > v<- ## and output of my function should be a "v"?for example which I can use it > in the next line?(v<-w? or something like that??) > ? > ##the rest works fine > p<-paste("t", v, ".txt", sep = "") > t<-read.table(p, header=FALSE, sep="\t", dec=",", > blank.lines.skip=FALSE) > rownames(t)<-as.Date(t[,1],"%d.%m.%Y") > colnames(t)<-c("date","start","high","low","end","w.average","lot", > "volume") > x<-as.xts(t) > whatis(x)??? > . > . > > > > [[alternative HTML version deleted]] > > > > > ______________________________________________ > R-help at r-project.org 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.-- Niels Richard Hansen Web: www.math.ku.dk/~richard Associate Professor Email: Niels.R.Hansen at math.ku.dk Department of Mathematical Sciences nielsrichardhansen at gmail.com University of Copenhagen Skype: nielsrichardhansen.dk Universitetsparken 5 Phone: +45 353 20783 (office) 2100 Copenhagen ? +45 2859 0765 (mobile) Denmark
Thanks Niels but it won't do.. please copy and paste the 2 lines below together to your console in order to see what I mean: cat("?"); a<-readLines(n=1) b<-paste("t",a,sep="") anyone / any idea to overcome this problem? Best, Fatih Niels wrote: Hi Fatih I believe that readLines(n=1) will do the job. It works fine from the Windows RGui, but I noticed that it hangs my Aquamacs/ESS when R runs from there, and a C-g was needed (which may be completely irrelevant to you). Best, Niels On 30/09/10 08.55, Pam wrote:>Hi guys, > >My concern is to create an automated process from the beginning to the end. I >want to copy all my code together in one piece and paste it to R console andsit>back and relax :) except one moment in which the program should ask me to enter >a number.. and only then (only after getting a valid number from me) it should >continue to read and process the rest of the code. I�tried�lots of things >(readline, readLines, scan, interactive, ask, switch,...) and read manuals and >searched help forums.. I found several similar questions but never a satisfying >answer.. so now it became a challenge.. any idea how�to�overcome this problem(R>2.11.1 for Windows)? (an example�code is below)� > >Best, >Fatih[[alternative HTML version deleted]]
You might want to use the tcltk package so you can bring up a window in which to input your data. This is better than trying to read from the console especially when 'sourcing' in some files or when cutting/pasting. On Thu, Sep 30, 2010 at 2:55 AM, Pam <fkiraz11 at yahoo.com> wrote:> Hi guys, > > My concern is to create an automated process from the beginning to the end. I > want to copy all my code together in one piece and paste it to R console and sit > back and relax :) except one moment in which the program should ask me to enter > a number.. and only then (only after getting a valid number from me) it should > continue to read and process the rest of the code. I?tried?lots of things > (readline, readLines, scan, interactive, ask, switch,...) and read manuals and > searched help forums.. I found several similar questions but never a satisfying > answer.. so now it became a challenge.. any idea how?to?overcome this problem (R > 2.11.1 for Windows)? (an example?code is below) > > Best, > Fatih > > > > library(gtools) > library(YaleToolkit) > library(xts) > > ### start of my?wrong?function! > f<-function(w){ > ?w<-readline("which data? ") > ?w<-as.numeric(w) > ?ifelse(is.numeric(w)=="TRUE", w, f()) > ?} > f() > ##### end of my?wrong function > > v<- ## and output of my function should be a "v"?for example which I can use it > in the next line?(v<-w? or something like that??) > > ##the rest works fine > p<-paste("t", v, ".txt", sep = "") > t<-read.table(p, header=FALSE, sep="\t", dec=",", > blank.lines.skip=FALSE) > rownames(t)<-as.Date(t[,1],"%d.%m.%Y") > colnames(t)<-c("date","start","high","low","end","w.average","lot", > "volume") > x<-as.xts(t) > whatis(x) > . > . > > > > ? ? ? ?[[alternative HTML version deleted]] > > > ______________________________________________ > R-help at r-project.org 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?
For those that want it all...> {cat("?"); a<-readLines(n=1)+ print("hey") + print(b<-paste("t",a,sep=""))} ?ada [1] "hey" [1] "tada"> b[1] "tada">Steven McKinney> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Peter Dalgaard > Sent: September-30-10 1:23 PM > To: Pam > Cc: r-help at r-project.org > Subject: Re: [R] interactive session > > On 09/30/2010 03:33 PM, Pam wrote: > > Thanks Niels but it won't do.. please copy and paste the? 2 lines? below together > > to your console in order to see what I mean: > > > > cat("?"); a<-readLines(n=1) > > b<-paste("t",a,sep="") > > > > anyone / any idea to overcome this problem? > > > > Best, > > Fatih > > > > You might want to source() a file with those lines rather than pasting > them to the console. There's just no way you can retroactively insert > text between two already submitted lines. > > You can do things like this, though > > {cat("?"); a<-readLines(n=1) > "hey" > b<-paste("t",a,sep="")} > > However, there's a catch > > > {cat("?"); a<-readLines(n=1) > + "hey" > + b<-paste("t",a,sep="")} > ?ada > > b > [1] "tada" > > Notice that the "hey" doesn't print. > > -- > Peter Dalgaard > Center for Statistics, Copenhagen Business School > Phone: (+45)38153501 > Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com > > ______________________________________________ > R-help at r-project.org 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.