How to assign output of cat to an object?

前端 未结 5 828
萌比男神i
萌比男神i 2020-12-15 04:34

How would it be possible in the example below to skip the step of writing to file \"test.txt\", i.e. assign the cat-result to an object, and still achieve the same end resul

相关标签:
5条回答
  • 2020-12-15 04:56

    Try

    > f <- textConnection("test3", "w")
    > cat(test, "\n", file=f)
    > test3
    [1] "V 1 x 1 2 3 y 3 5 8 V 2 x y V 3 y 7 2 1 V 4 x 9 3 7 y "
    > close(f)
    
    0 讨论(0)
  • 2020-12-15 04:59

    Instead of cating to a file, why not use the paste command to generate a string instead?

    > paste(test, collapse="\n")
    [1] "V 1\nx\n1 2 3\ny\n3 5 8\nV 2\nx\ny\nV 3\ny\n7 2 1\nV 4\nx\n9 3 7\ny"
    

    Now instead of doing a cat then readlines you can just pass this string directly into strsplit.

    0 讨论(0)
  • 2020-12-15 05:02

    As a more general solution, you can use the capture output function. It results in a character vector with elements corresponding to each line of the output.

    your example:

    test2<-capture.output(cat(test))
    

    here is a multi-line example:

    > out<-capture.output(summary(lm(hwy~cyl*drv,data=mpg)))
    > out
     [1] ""                                                               
     [2] "Call:"                                                          
     [3] "lm(formula = hwy ~ cyl * drv, data = mpg)"                      
     [4] ""                                                               
     [5] "Residuals:"                                                     
     [6] "    Min      1Q  Median      3Q     Max "                       
     [7] "-8.3315 -1.4139 -0.1382  1.6479 13.5861 "                       
     [8] ""                                                               
     [9] "Coefficients:"                                                  
    [10] "            Estimate Std. Error t value Pr(>|t|)    "           
    [11] "(Intercept)  32.1776     1.2410  25.930  < 2e-16 ***"           
    [12] "cyl          -2.0049     0.1859 -10.788  < 2e-16 ***"           
    [13] "drvf          8.4009     1.8965   4.430 1.47e-05 ***"           
    [14] "drvr          8.2509     6.4243   1.284    0.200    "           
    [15] "cyl:drvf     -0.5362     0.3422  -1.567    0.119    "           
    [16] "cyl:drvr     -0.5248     0.8379  -0.626    0.532    "           
    [17] "---"                                                            
    [18] "Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 "
    [19] ""                                                               
    [20] "Residual standard error: 2.995 on 228 degrees of freedom"       
    [21] "Multiple R-squared: 0.7524,\tAdjusted R-squared: 0.747 "         
    [22] "F-statistic: 138.6 on 5 and 228 DF,  p-value: < 2.2e-16 "       
    [23] ""    
    
    0 讨论(0)
  • 2020-12-15 05:09

    There's also the assign statement which allows you to build a name and set an object to it. Very useful if you want to iterate a bunch of tests and name them with dynamic values.

    assign("mary", paste(test,sep= "\n"))

    will assign the paste statement to mary. However say you were running a bunch of regressions and wanted your regression objects named by predictor. You could do something like

    assign(paste("myRegression",names(dataframe)[2],sep=""), lm(dataframe$response~dataframe[,2]))
    

    which would give you the object

    myRegressionPredictorName as you linear model.

    0 讨论(0)
  • 2020-12-15 05:16

    Try the following codes:

    writeLines(capture.out((summary(lm(hwy~cyl*drv,data=mpg)),con="summary.txt",sep="\n")

    Then you can open the txt file "summary.txt" to see your results.

    0 讨论(0)
提交回复
热议问题