I wrote the following code to redirect console output to text file. All three commands' (dim, str, summary) output appears in the text file when I interactively run the code. However, when I place the code in a function and interactively run through a function call, only the str command outuput appears. This may be a buffering problem. Any suggestions?
Operating System : OS X 10.9.5 (Mavericks); R 3.1.1 GUI 1.65 Mavericks build (6784)
This code works...
con <- file("FileInfoLog.txt")
sink(con, append=TRUE)
sink(con, append=TRUE, type="message")
writeLines("\n\n\n===============================================================\n")
writeLines("Dimensions are ")
dim(db)
writeLines("\n\n\n===============================================================\n")
writeLines("Structure is ")
str(db)
writeLines("\n\n\n===============================================================\n")
writeLines("Summary is ")
summary(db)
# Restore output to console
sink(type="message")
sink()
This code does not reliably work...only the str() output appears in the text file.
getFileInfo <- function(db) {
con <- file("FileInfoLog.txt")
sink(con, append=TRUE)
sink(con, append=TRUE, type="message")
writeLines("\n\n\n===============================================================\n")
writeLines("Dimensions are ")
dim(db)
writeLines("\n\n\n===============================================================\n")
writeLines("Structure is ")
str(db)
writeLines("\n\n\n===============================================================\n")
writeLines("Summary is ")
summary(db)
# Restore output to console
sink(type="message")
sink()
}
FileInfoLog.txt from code that works...
===============================================================
Dimensions are [1] 28947 17
===============================================================
Structure is 'data.frame': 28947 obs. of 17 variables: $ store : int 2 2 2 2 2 2 2 2 2 2 ... $ brand : Factor w/ 3 levels "dominicks","minute.maid",..: 3 3 3 3 3 3 3 3 3 3 ... $ week : int 40 46 47 48 50 51 52 53 54 57 ... $ logmove : num 9.02 8.72 8.25 8.99 9.09 ... [...]
===============================================================
Summary is
store brand week logmove feat price AGE60
Min. : 2.00 dominicks :9649 Min. : 40.0 Min. : 4.159 Min. :0.0000 Min. :0.520 Min. :0.05805
1st Qu.: 53.00 minute.maid:9649 1st Qu.: 70.0 1st Qu.: 8.490 1st Qu.:0.0000 1st Qu.:1.790 1st Qu.:0.12210
Median : 86.00 tropicana :9649 Median :101.0 Median : 9.034 Median :0.0000 Median :2.170 Median :0.17065
Mean : 80.88 Mean :100.5 Mean : 9.168 Mean :0.2373 Mean :2.282 Mean :0.17313
3rd Qu.:111.00 3rd Qu.:130.0 3rd Qu.: 9.765 3rd Qu.:0.0000 3rd Qu.:2.730 3rd Qu.:0.21395
Max. :137.00 Max. :160.0 Max. :13.482 Max. :1.0000 Max. :3.870 Max. :0.30740
[...]
FileInfoLog.txt from code that does not reliably work...
===============================================================
Dimensions are
===============================================================
Structure is 'data.frame': 28947 obs. of 17 variables: $ store : int 2 2 2 2 2 2 2 2 2 2 ... $ brand : Factor w/ 3 levels "dominicks","minute.maid",..: 3 3 3 3 3 3 3 3 3 3 ... $ week : int 40 46 47 48 50 51 52 53 54 57 ... $ logmove : num 9.02 8.72 8.25 8.99 9.09 ... [...]
===============================================================
Summary is