Search code examples
rprintingwidth

Print R data frame one line per row


When using print to print data frames I find that it stops at some width, a bit short of 80 characters, and prints only those columns that can fit. Then it proceeds to print the next so-many columns.

Is is possible to get it to print a whole row, regardless of how wide, without newlines or dots, to the output?


Solution

  • Your comment on another answer suggests that you're doing this specifically to write out to a file - this answer does that, but the display may look different in the console depending on whether you're using R or Rstudio.

    According to ?options there is a hard limit of 10,000 character width for the console, which can only be increased by recompiling R. If this is enough for your use case then you can do:

    oldoptions <- options(width=10000)
    options("width") #check that it's been set
    #$width
    #[1] 10000
    
    sink("test.txt")  #append = TRUE might be useful for subsequent writes
    print(a)  #Print the very wide dataframe
    sink()
    
    options(oldoptions) # reset the changed options
    

    Note that initially I thought that this wasn't working when I inspected the file in Notepad - that was because (my setup of) Notepad has a limit on line length itself, and then wraps onto the next line. Viewing with something more sophisticated (Notepad++) shows that it did work sccessfully.