cap.out: captures and wraps the output of an expression

cap.outR Documentation

captures and wraps the output of an expression

Description

captures and wraps the output of an expression

Usage

cap.out(
  cmd,
  lines = NULL,
  numlines_only = F,
  se = NULL,
  width = getOption("width", 110) - 3,
  keep_empty = F,
  fixed_wrap = T,
  line_numbering = F,
  abbr_ind = T,
  file = "",
  append = FALSE
)

Arguments

cmd

An expression or character vector

lines

Line numbers to select from output. Default: all lines

numlines_only

Boolean indicating no wrapping takes place and only the number of lines and the captured output are returned (in a list). Default: FALSE

se

Start and End of each line (see details). Default: NULL (whole line)

width

Position in each line where wrapping takes place. Default: getOption('width',110)-3

keep_empty

Boolean indicating if empty should be kept. Default: FALSE

fixed_wrap

Boolean indicating if wrapping takes place at a fixed position or takes into account word boundaries. Default: TRUE

line_numbering

Boolen indicating if line are prefixed with the line number. Default: FALSE (no numbering). Line numbers are prefixed only after the selection of parts of the line by argument se.

abbr_ind

Boolean indicating if abbreviations will be indicated with ... . Default: TRUE

file

A connection, or a character string naming the file to print to. If "" (the default), cat prints to the standard output connection, the console unless redirected by sink.

append

Boolean. Only used if the argument file is the name of file (and not a connection or "|cmd"). If TRUE output will be appended to file; otherwise, it will overwrite the contents of file.

Value

NULL (invisible)

details

Prints of XML documents can be very lengthy. Therefore the function cap.out is defined as a cover function of util::capture.output. With the argument lines one can specify the numbers of the lines that will be kept. For each line of output it can specified which part will be displayed. When the argument se is a 2-column matrix it specifies the start and end of the part. When se is an integer vector it gives the start position of the part when a number is negative and the end position when a number is postive. In those cases the part extends to the end or from the start of the line. When a number is 0, the line is not displayed. When se is not specified, all characters for all selected lines will be displayed. To ensure that the specification of the lines and the positions match the position specification is recycled when necessary.

acknowledgements

I was glad to be able to use the following (idea for) code :

  • the StackOverflow code to recycle argument by baptiste.

  • the idea to split long strings in parts I saw in a StackOverflow article by akrun.

example description

In the example I format an xml document. Here I specify that I want to retrieve at most 1700 characters of these lines and in fact only will output the first 175 characters of each line with exception of the 6th one where I show 1650. I do not specify lines= but I know that the print of this document has 7 lines, so I could have added the argument lines=1:7. I also do not specify width and therefore use the default system option getOption('width'). Both the print and the cap.out outputs indicate with ... that a line is truncated; I ensure that the width of the print statement is a little greater than the greatest se to avoid outputs such as '.. ...' .

Examples

## Not run: 
# see description in details
cap.out( print(xml_doc,width=1700),se=c(175,175,175,175,175,1650,175) )

## End(Not run)

HanOostdijk/HOQCutil documentation built on July 28, 2023, 5:56 p.m.