特にカスタムメッセージ(警告、エラーなど)に関しては、文字列に関するすべての印刷余白を 80 文字に抑えるようにしています。
ここで、印刷マージンの制限が自動的に保証/強制されるようにする簡単な方法があるかどうか、私はいつも疑問に思っていました。少しグーグルで検索しましたが、私のニーズに合ったものは本当に見つかりませんでした。
質問
誰かが私がさらに構築できる素敵な「きれいな印刷」機能を思いつきましたか?.
これは私がこれまでに思いついたものです:
関数定義
makePretty <- function(
string,
print.margin=80,
...
){
out <- string
if (nchar(string) > print.margin) {
times <- ceiling(nchar(string)/print.margin)
breaks <- rep(print.margin+1, times)
breaks <- cumsum(breaks)
string.spl <- unlist(strsplit(string, split=" "))
seps <- str_locate_all(string, " ")[[1]][,"start"]
queue <- NA
envir <- environment()
out <- unlist(sapply(1:length(breaks), function(ii) {
idx.next <- breaks[ii]
if (ii < length(breaks)) {
idx <- which(seps <= idx.next)
chunk <- string.spl[idx]
envir$string.spl <- envir$string.spl[-idx]
envir$seps <- envir$seps[-idx]
} else {
chunk <- string.spl
}
chunk <- paste(chunk, collapse=" ")
# Chunk could exceed print margin in case the right hand neighbor
# wasn't a blank >> check again
if (nchar(chunk) > print.margin) {
chunk <- makePretty(string=chunk, print.margin=print.margin)
envir$queue <- chunk[length(chunk)]
chunk <- chunk[-length(chunk)]
} else {
if (!is.na(envir$queue)) {
# Prepend chunk with queued chunk
chunk <- paste(envir$queue, chunk, sep=" ")
# Reset queue
envir$queue <- NA
}
}
# /
out <- chunk
return(out)
}))
}
return(out)
}
機能の適用
string <- "This is just an example of a very long character string that exceeds the default print margin of 80 and therefore needs some pretty printing. In fact it's so long that it needs to be broken down into three parts."
> makePretty(string=string)
[1] "This is just an example of a very long character string that exceeds the default"
[2] "print margin of 80 and therefore needs some pretty printing. In fact it's so"
[3] "long that it needs to be broken down into three parts."
> string <- "This is just an example of a very long character string that exceeds a certain print margin and therefore needs some pretty printing. In fact it's so long that it needs to be broken down into numerous parts."
> makePretty(string=string, print.margin=40)
[1] "This is just an example of a very long"
[2] "character string that exceeds a certain"
[3] "print margin and therefore needs some"
[4] "pretty printing. In fact it's so long"
[5] "that it needs to be broken down into numerous"
[6] "parts."
string <- "This is just an example of a very long character string that exceeds the default print margin of 80 and therefore needs some pretty printing. In fact it's so looooooooooooooooooooooooooooooooong that it needs to be broken down into four parts."
> makePretty(string=string)
[1] "This is just an example of a very long character string that exceeds the default"
[2] "print margin of 80 and therefore needs some pretty printing. In fact it's so"
[3] "looooooooooooooooooooooooooooooooong that it needs to be broken down into four"
[4] "parts."
string <- "This is just an example of a very long character string that exceeds the default print margin of 80 and therefore needs some pretty printing. In fact it's soooooooo looooooooooooooooooooooooooooooooong that it needs to be broken down into four parts."
> makePretty(string=string)
[1] "This is just an example of a very long character string that exceeds the default"
[2] "print margin of 80 and therefore needs some pretty printing. In fact it's"
[3] "soooooooo looooooooooooooooooooooooooooooooong that it needs to be broken down"
[4] "into four parts."
これまでのところ、このアプローチは、どの単語が一緒に属しているかを判断するために空白のみに依存しています。これは、コロン、セミコロンなどの他の「現実世界のシナリオ」をカバーしていない可能性があります.