1

I need to populate a .txt file while running a code with nested functions. I use sink() for this purpose. The output consists of a) text messages, b) dataframe rows. I fail to print dataframes from inside the nested function:

sink("log.txt")

cat("Some message")          # Successfully prints to log.txt
head(some_df)                # Successfully prints to log.txt

some_fun = function(x){
    # ...
    cat("Another message")   # Successfully prints to log.txt
    head(another_df)         # Nothing gets printed to log.txt
    # check that another_df is not empty:
    cat(nrow(another_df))    # Successfully prints to log.txt (>0) 
    # ...
}
some_fun(x=0)

sink()

So what's the right way to do it?

1 Answers1

1

Wrap a print around head inside your function. Like this:

print(head(another_df))
sm925
  • 2,648
  • 1
  • 16
  • 28