Hi, I have a fairly complex object that I have written a print function for. Thus when I do print(results), the R console shows me a whole bunch of stuff already formatted. What I want to do is to take whatever print(results) shows to console and then put that in a file. I am doing this using the sink command. However, I am unsure as to how to "unsink". Eg, how do I restore output to the normal console? Thanks, Greg
The HELP page for 'sink' is pretty clear about this: sink() or sink(file=NULL) ends the last diversion (of the specified type). There is a stack of diversions for normal output, so output reverts to the previous diversion (if there was one). The stack is of up to 21 connections (20 diversions). On Sat, Nov 13, 2010 at 11:12 PM, Gregory Ryslik <rsaber at comcast.net> wrote:> Hi, > > I have a fairly complex object that I have written a print function for. > > Thus when I do print(results), the R console shows me a whole bunch of stuff already formatted. What I want to do is to take whatever print(results) shows to console and then put that in a file. I am doing this using the sink command. > > However, I am unsure as to how to "unsink". Eg, how do I restore output to the normal console? > > Thanks, > Greg > ______________________________________________ > 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?
From: Thomas Parr [mailto:thomas.parr at maine.edu] Sent: Thursday, December 02, 2010 10:52 PM To: r-help-request at stat.math.ethz.ch Subject: Writing to a file I am trying to get my script to write to a file from the for loop. It is "working", but the problem is at it is outputting to two columns and I want it to output to 5. Current results X2403,0.006049271 X2403,0.000118622 X2403,50.99600705 X2403,7.62E-150 X2419,0.012464215 X2419,9.07E-05 X2419,137.4022573 X2419,6.45E-273 ... Desired/expected results X2403,0.0060492710.000118622,50.99600705,7.62E-150 X2419,0.012464215,9.07E-05,137.4022573,6.45E-273 ... Data is being extracted from "nls" output with "summary", "nls" uses "fit." a<-summary(nls(acoeff ~ aref*exp(-S*(alam-375)), trace=T, start=list(S=0.0015))) cat(sites[v-1],a$coefficients[1,1],a$coefficients[1,2],a$coefficients[1,3],a $coefficients[1,4],sep=",",append=TRUE, file=paste(dirpath,"/results.csv",sep="")) The idea is that it is looping through the data sites and as "nls" generates parameter estimates, "summary" extracts them and "cat" writes them to a CSV file. Note: have tried write.csv, write.table, and write I thing they all call "cat" at some point. Any help would be appreciated and if you have a different solution I am all ears. Thomas
maybe I could keep each line (having the strings) in a file or somewhere and then call a print function that prints them all together from where I saved them? Please let me know as soon as Possible!! thank you! -- View this message in context: http://r.789695.n4.nabble.com/Writing-to-a-file-tp3070617p4362340.html Sent from the R help mailing list archive at Nabble.com.
Hi> > Honestly thank you for the prompt responding > and you are right I will tellyou what I want to do > and not the way ..since I dont know much from R > > > I have a txt with Proteins > > "Prot_10035" "Func_0005874" "Func_0016787" "Func_0003774""Func_0006898"> "Func_0005856" "Func_0005525" "Func_0005737" "Func_0003924""Func_0005515"> "Func_0000166" > "Prot_10036" "Func_0005739" "Func_0003735" "Func_0006412""Func_0005763"> "Func_0005840" > "Prot_10037" "Func_0005739" "Func_0005515" > "Prot_10039" "Func_0005576" "Func_0009615" "Func_0050832""Func_0005615"> "Func_0006955" "Func_0042742" "Func_0031640" "Func_0006935" > "Prot_1004" "Func_0046872" "Func_0003887" "Func_0003684""Func_0016740"> "Func_0006281" "Func_0006260" "Func_0016779" "Func_0005634" > "Prot_10040" "Func_0005886" "Func_0046488" "Func_0016301""Func_0007409"> "Func_0005524" "Func_0016740" "Func_0016308" "Func_0000166" > > which is 8527 lines and 145 columns (not all the proteins have the same > number of proteins)functions? First of all you need to read this file into R properly. I would try readLines with some further polishing to feed list structure with protein names as labels for each part of a list. After that some cycle/lapply checking with regular expression could be a way to populate a data frame with protein names in first column and score in the second. After that you can compare such score with other values in another data frame. However without an example you hardly get detailed help. Regards Petr> What I want is to predict whether those proteins are related to canceror> not > depending on whether they have some functions. I found that there are 3 > functions very often related to cancer > and in case a protein has 2/3 or 3/3 to "label" it (somehow-maybe addingan> extra column) as cancer related > The names of the Proteins are always in the 1st column but the names ofthe> functions can be at any of the next columns > > So what I did is to use this loop, but I cant write properly the way Iwant> it to print the results so to use them again > (I need to know the name of the proteins having the functions in acolumn so> as next step to compare it with another file > -test data set- and conclude to true positive, false positive, true > negative, false negative > > It cant be as hard as I see it :):) > > -- > View this message in context:http://r.789695.n4.nabble.com/Writing-to-a-> file-tp3070617p4363940.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
As I said to you a while back, use append = TRUE. Michael On Tue, Feb 7, 2012 at 4:18 AM, Felicity <felicity-th at hotmail.com> wrote:> Thanks a lot for the interest :) > > My loop is the following > counter = 0 > ?for (i in 1:nrow(y)) > ?{ > > > ?for (j in 1:ncol(y)) > ?{ > ?if (y[i,j]=="Func_0005634") { > ? ? ? ? counter = counter + 1 } > ?if(y[i,j]=="Func_0005737"){ > ? ? ? ? counter = counter + 1 } > ?if(y[i,j]=="Func_0005515"){ > ? ? ? ?counter = counter + 1 } > > } > ?if(counter == 3 ){ > ? ? ? ?cat(y[i,1], ?file = "foo.csv", ?"\n") > ? ? ? ?} > ?counter = 0 > > } > > and after read.table("foo.csv") > > I get > ?V1 > 1 45 > > which is the last result > > why does it overwrite? how can I have all the results? > > Eager to a reply from you! > > -- > View this message in context: http://r.789695.n4.nabble.com/Writing-to-a-file-tp3070617p4364149.html > Sent from the R help mailing list archive at Nabble.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.
Hi now you omitted data, but never mind :-)> My loop is the following > counter = 0 > for (i in 1:nrow(y)) > { > > > for (j in 1:ncol(y)) > { > if (y[i,j]=="Func_0005634") { > counter = counter + 1 } > if(y[i,j]=="Func_0005737"){ > counter = counter + 1 } > if(y[i,j]=="Func_0005515"){ > counter = counter + 1 } > > } > if(counter == 3 ){ > cat(y[i,1], file = "foo.csv", "\n") > } > counter = 0 > > } >If I remember correctly you want to inspect each row if it contains any of "Func" values and how many of them.> dput(y)structure(list(prot = c(1, 2, 3, 4), X1 = structure(c(1L, 1L, 1L, 2L), .Label = c("a", " "), class = "factor"), X2 = structure(c(3L, 2L, 3L, 3L), .Label = c("a", "b", " "), class = "factor"), X3 = structure(c(3L, 3L, 3L, 2L), .Label = c("b", "c", " "), class = "factor"), X4 = structure(c(1L, 1L, 1L, 3L), .Label = c("c", "d", " "), class = "factor"), X5 = structure(c(2L, 1L, 1L, 1L), .Label = c("d", " "), class = "factor")), .Names = c("prot", "X1", "X2", "X3", "X4", "X5"), row.names = c(NA, 4L), class = "data.frame")>So> rowSums((y=="a") | (y=="b") | (y=="d"))1 2 3 4 1 3 2 1 gives you number of values (a,b,d) in each row. Your construction comes from some differnt programming world. Regards Petr> and after read.table("foo.csv") > > I get > V1 > 1 45 > > which is the last result > > why does it overwrite? how can I have all the results? > > Eager to a reply from you! > > -- > View this message in context:http://r.789695.n4.nabble.com/Writing-to-a-> file-tp3070617p4364149.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.