685 lines
43 KiB
HTML
685 lines
43 KiB
HTML
<!DOCTYPE html>
|
||
|
||
<html>
|
||
|
||
<head>
|
||
|
||
<meta charset="utf-8" />
|
||
<meta name="generator" content="pandoc" />
|
||
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
|
||
|
||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||
|
||
|
||
|
||
<title>Custom formatting</title>
|
||
|
||
<script>// Pandoc 2.9 adds attributes on both header and div. We remove the former (to
|
||
// be compatible with the behavior of Pandoc < 2.8).
|
||
document.addEventListener('DOMContentLoaded', function(e) {
|
||
var hs = document.querySelectorAll("div.section[class*='level'] > :first-child");
|
||
var i, h, a;
|
||
for (i = 0; i < hs.length; i++) {
|
||
h = hs[i];
|
||
if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6
|
||
a = h.attributes;
|
||
while (a.length > 0) h.removeAttribute(a[0].name);
|
||
}
|
||
});
|
||
</script>
|
||
|
||
<style type="text/css">
|
||
code{white-space: pre-wrap;}
|
||
span.smallcaps{font-variant: small-caps;}
|
||
span.underline{text-decoration: underline;}
|
||
div.column{display: inline-block; vertical-align: top; width: 50%;}
|
||
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
|
||
ul.task-list{list-style: none;}
|
||
</style>
|
||
|
||
|
||
|
||
<style type="text/css">
|
||
code {
|
||
white-space: pre;
|
||
}
|
||
.sourceCode {
|
||
overflow: visible;
|
||
}
|
||
</style>
|
||
<style type="text/css" data-origin="pandoc">
|
||
pre > code.sourceCode { white-space: pre; position: relative; }
|
||
pre > code.sourceCode > span { line-height: 1.25; }
|
||
pre > code.sourceCode > span:empty { height: 1.2em; }
|
||
.sourceCode { overflow: visible; }
|
||
code.sourceCode > span { color: inherit; text-decoration: inherit; }
|
||
div.sourceCode { margin: 1em 0; }
|
||
pre.sourceCode { margin: 0; }
|
||
@media screen {
|
||
div.sourceCode { overflow: auto; }
|
||
}
|
||
@media print {
|
||
pre > code.sourceCode { white-space: pre-wrap; }
|
||
pre > code.sourceCode > span { display: inline-block; text-indent: -5em; padding-left: 5em; }
|
||
}
|
||
pre.numberSource code
|
||
{ counter-reset: source-line 0; }
|
||
pre.numberSource code > span
|
||
{ position: relative; left: -4em; counter-increment: source-line; }
|
||
pre.numberSource code > span > a:first-child::before
|
||
{ content: counter(source-line);
|
||
position: relative; left: -1em; text-align: right; vertical-align: baseline;
|
||
border: none; display: inline-block;
|
||
-webkit-touch-callout: none; -webkit-user-select: none;
|
||
-khtml-user-select: none; -moz-user-select: none;
|
||
-ms-user-select: none; user-select: none;
|
||
padding: 0 4px; width: 4em;
|
||
color: #aaaaaa;
|
||
}
|
||
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
|
||
div.sourceCode
|
||
{ }
|
||
@media screen {
|
||
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
|
||
}
|
||
code span.al { color: #ff0000; font-weight: bold; }
|
||
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; }
|
||
code span.at { color: #7d9029; }
|
||
code span.bn { color: #40a070; }
|
||
code span.bu { color: #008000; }
|
||
code span.cf { color: #007020; font-weight: bold; }
|
||
code span.ch { color: #4070a0; }
|
||
code span.cn { color: #880000; }
|
||
code span.co { color: #60a0b0; font-style: italic; }
|
||
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; }
|
||
code span.do { color: #ba2121; font-style: italic; }
|
||
code span.dt { color: #902000; }
|
||
code span.dv { color: #40a070; }
|
||
code span.er { color: #ff0000; font-weight: bold; }
|
||
code span.ex { }
|
||
code span.fl { color: #40a070; }
|
||
code span.fu { color: #06287e; }
|
||
code span.im { color: #008000; font-weight: bold; }
|
||
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; }
|
||
code span.kw { color: #007020; font-weight: bold; }
|
||
code span.op { color: #666666; }
|
||
code span.ot { color: #007020; }
|
||
code span.pp { color: #bc7a00; }
|
||
code span.sc { color: #4070a0; }
|
||
code span.ss { color: #bb6688; }
|
||
code span.st { color: #4070a0; }
|
||
code span.va { color: #19177c; }
|
||
code span.vs { color: #4070a0; }
|
||
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; }
|
||
</style>
|
||
<script>
|
||
// apply pandoc div.sourceCode style to pre.sourceCode instead
|
||
(function() {
|
||
var sheets = document.styleSheets;
|
||
for (var i = 0; i < sheets.length; i++) {
|
||
if (sheets[i].ownerNode.dataset["origin"] !== "pandoc") continue;
|
||
try { var rules = sheets[i].cssRules; } catch (e) { continue; }
|
||
var j = 0;
|
||
while (j < rules.length) {
|
||
var rule = rules[j];
|
||
// check if there is a div.sourceCode rule
|
||
if (rule.type !== rule.STYLE_RULE || rule.selectorText !== "div.sourceCode") {
|
||
j++;
|
||
continue;
|
||
}
|
||
var style = rule.style.cssText;
|
||
// check if color or background-color is set
|
||
if (rule.style.color === '' && rule.style.backgroundColor === '') {
|
||
j++;
|
||
continue;
|
||
}
|
||
// replace div.sourceCode by a pre.sourceCode rule
|
||
sheets[i].deleteRule(j);
|
||
sheets[i].insertRule('pre.sourceCode{' + style + '}', j);
|
||
}
|
||
}
|
||
})();
|
||
</script>
|
||
|
||
|
||
|
||
|
||
<style type="text/css">body {
|
||
background-color: #fff;
|
||
margin: 1em auto;
|
||
max-width: 700px;
|
||
overflow: visible;
|
||
padding-left: 2em;
|
||
padding-right: 2em;
|
||
font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
|
||
font-size: 14px;
|
||
line-height: 1.35;
|
||
}
|
||
#TOC {
|
||
clear: both;
|
||
margin: 0 0 10px 10px;
|
||
padding: 4px;
|
||
width: 400px;
|
||
border: 1px solid #CCCCCC;
|
||
border-radius: 5px;
|
||
background-color: #f6f6f6;
|
||
font-size: 13px;
|
||
line-height: 1.3;
|
||
}
|
||
#TOC .toctitle {
|
||
font-weight: bold;
|
||
font-size: 15px;
|
||
margin-left: 5px;
|
||
}
|
||
#TOC ul {
|
||
padding-left: 40px;
|
||
margin-left: -1.5em;
|
||
margin-top: 5px;
|
||
margin-bottom: 5px;
|
||
}
|
||
#TOC ul ul {
|
||
margin-left: -2em;
|
||
}
|
||
#TOC li {
|
||
line-height: 16px;
|
||
}
|
||
table {
|
||
margin: 1em auto;
|
||
border-width: 1px;
|
||
border-color: #DDDDDD;
|
||
border-style: outset;
|
||
border-collapse: collapse;
|
||
}
|
||
table th {
|
||
border-width: 2px;
|
||
padding: 5px;
|
||
border-style: inset;
|
||
}
|
||
table td {
|
||
border-width: 1px;
|
||
border-style: inset;
|
||
line-height: 18px;
|
||
padding: 5px 5px;
|
||
}
|
||
table, table th, table td {
|
||
border-left-style: none;
|
||
border-right-style: none;
|
||
}
|
||
table thead, table tr.even {
|
||
background-color: #f7f7f7;
|
||
}
|
||
p {
|
||
margin: 0.5em 0;
|
||
}
|
||
blockquote {
|
||
background-color: #f6f6f6;
|
||
padding: 0.25em 0.75em;
|
||
}
|
||
hr {
|
||
border-style: solid;
|
||
border: none;
|
||
border-top: 1px solid #777;
|
||
margin: 28px 0;
|
||
}
|
||
dl {
|
||
margin-left: 0;
|
||
}
|
||
dl dd {
|
||
margin-bottom: 13px;
|
||
margin-left: 13px;
|
||
}
|
||
dl dt {
|
||
font-weight: bold;
|
||
}
|
||
ul {
|
||
margin-top: 0;
|
||
}
|
||
ul li {
|
||
list-style: circle outside;
|
||
}
|
||
ul ul {
|
||
margin-bottom: 0;
|
||
}
|
||
pre, code {
|
||
background-color: #f7f7f7;
|
||
border-radius: 3px;
|
||
color: #333;
|
||
white-space: pre-wrap;
|
||
}
|
||
pre {
|
||
border-radius: 3px;
|
||
margin: 5px 0px 10px 0px;
|
||
padding: 10px;
|
||
}
|
||
pre:not([class]) {
|
||
background-color: #f7f7f7;
|
||
}
|
||
code {
|
||
font-family: Consolas, Monaco, 'Courier New', monospace;
|
||
font-size: 85%;
|
||
}
|
||
p > code, li > code {
|
||
padding: 2px 0px;
|
||
}
|
||
div.figure {
|
||
text-align: center;
|
||
}
|
||
img {
|
||
background-color: #FFFFFF;
|
||
padding: 2px;
|
||
border: 1px solid #DDDDDD;
|
||
border-radius: 3px;
|
||
border: 1px solid #CCCCCC;
|
||
margin: 0 5px;
|
||
}
|
||
h1 {
|
||
margin-top: 0;
|
||
font-size: 35px;
|
||
line-height: 40px;
|
||
}
|
||
h2 {
|
||
border-bottom: 4px solid #f7f7f7;
|
||
padding-top: 10px;
|
||
padding-bottom: 2px;
|
||
font-size: 145%;
|
||
}
|
||
h3 {
|
||
border-bottom: 2px solid #f7f7f7;
|
||
padding-top: 10px;
|
||
font-size: 120%;
|
||
}
|
||
h4 {
|
||
border-bottom: 1px solid #f7f7f7;
|
||
margin-left: 8px;
|
||
font-size: 105%;
|
||
}
|
||
h5, h6 {
|
||
border-bottom: 1px solid #ccc;
|
||
font-size: 105%;
|
||
}
|
||
a {
|
||
color: #0033dd;
|
||
text-decoration: none;
|
||
}
|
||
a:hover {
|
||
color: #6666ff; }
|
||
a:visited {
|
||
color: #800080; }
|
||
a:visited:hover {
|
||
color: #BB00BB; }
|
||
a[href^="http:"] {
|
||
text-decoration: underline; }
|
||
a[href^="https:"] {
|
||
text-decoration: underline; }
|
||
|
||
code > span.kw { color: #555; font-weight: bold; }
|
||
code > span.dt { color: #902000; }
|
||
code > span.dv { color: #40a070; }
|
||
code > span.bn { color: #d14; }
|
||
code > span.fl { color: #d14; }
|
||
code > span.ch { color: #d14; }
|
||
code > span.st { color: #d14; }
|
||
code > span.co { color: #888888; font-style: italic; }
|
||
code > span.ot { color: #007020; }
|
||
code > span.al { color: #ff0000; font-weight: bold; }
|
||
code > span.fu { color: #900; font-weight: bold; }
|
||
code > span.er { color: #a61717; background-color: #e3d2d2; }
|
||
</style>
|
||
|
||
|
||
|
||
|
||
</head>
|
||
|
||
<body>
|
||
|
||
|
||
|
||
|
||
<h1 class="title toc-ignore">Custom formatting</h1>
|
||
|
||
|
||
|
||
<div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" tabindex="-1"></a><span class="fu">library</span>(pillar)</span></code></pre></div>
|
||
<p>How to customize the printed output of a <code>"tbl"</code> subclass?
|
||
This vignette shows the various customization options. Customizing the
|
||
formatting of a vector class in a tibble is described in
|
||
<code>vignette("pillar", package = "vctrs")</code>. An overview over the
|
||
control and data flow is given in <code>vignette("printing")</code>.</p>
|
||
<p>This vignette assumes that the reader is familiar with S3 classes,
|
||
methods, and inheritance. The <a href="https://adv-r.hadley.nz/s3.html">“S3” chapter</a> of Hadley
|
||
Wickham’s “Advanced R” is a good start.</p>
|
||
<p>To make use of pillar’s printing capabilities, create a class that
|
||
inherits from <code>"tbl"</code>, like tibble (classes
|
||
<code>"tbl_df"</code> and <code>"tbl"</code>), dbplyr lazy tables
|
||
(<code>"tbl_lazy"</code> and <code>"tbl"</code>) and sf spatial data
|
||
frames (<code>"sf"</code>, <code>"tbl_df"</code> and
|
||
<code>"tbl"</code>). Because we are presenting various customization
|
||
options, we create a constructor for an example data frame with
|
||
arbitrary subclass.</p>
|
||
<div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" tabindex="-1"></a>example_tbl <span class="ot"><-</span> <span class="cf">function</span>(class) {</span>
|
||
<span id="cb2-2"><a href="#cb2-2" tabindex="-1"></a> vctrs<span class="sc">::</span><span class="fu">new_data_frame</span>(</span>
|
||
<span id="cb2-3"><a href="#cb2-3" tabindex="-1"></a> <span class="fu">list</span>(</span>
|
||
<span id="cb2-4"><a href="#cb2-4" tabindex="-1"></a> <span class="at">a =</span> letters[<span class="dv">1</span><span class="sc">:</span><span class="dv">3</span>],</span>
|
||
<span id="cb2-5"><a href="#cb2-5" tabindex="-1"></a> <span class="at">b =</span> <span class="fu">data.frame</span>(<span class="at">c =</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">3</span>, <span class="at">d =</span> <span class="dv">4</span><span class="sc">:</span><span class="dv">6</span> <span class="sc">+</span> <span class="fl">0.5</span>)</span>
|
||
<span id="cb2-6"><a href="#cb2-6" tabindex="-1"></a> ),</span>
|
||
<span id="cb2-7"><a href="#cb2-7" tabindex="-1"></a> <span class="at">class =</span> <span class="fu">c</span>(class, <span class="st">"tbl"</span>)</span>
|
||
<span id="cb2-8"><a href="#cb2-8" tabindex="-1"></a> )</span>
|
||
<span id="cb2-9"><a href="#cb2-9" tabindex="-1"></a>}</span></code></pre></div>
|
||
<p>The <code>"default"</code> class doesn’t have any customizations yet,
|
||
and prints like a regular tibble.</p>
|
||
<div class="sourceCode" id="cb3"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" tabindex="-1"></a><span class="fu">example_tbl</span>(<span class="st">"default"</span>)</span>
|
||
<span id="cb3-2"><a href="#cb3-2" tabindex="-1"></a><span class="co">#> $a</span></span>
|
||
<span id="cb3-3"><a href="#cb3-3" tabindex="-1"></a><span class="co">#> [1] "a" "b" "c"</span></span>
|
||
<span id="cb3-4"><a href="#cb3-4" tabindex="-1"></a><span class="co">#> </span></span>
|
||
<span id="cb3-5"><a href="#cb3-5" tabindex="-1"></a><span class="co">#> $b</span></span>
|
||
<span id="cb3-6"><a href="#cb3-6" tabindex="-1"></a><span class="co">#> c d</span></span>
|
||
<span id="cb3-7"><a href="#cb3-7" tabindex="-1"></a><span class="co">#> 1 1 4.5</span></span>
|
||
<span id="cb3-8"><a href="#cb3-8" tabindex="-1"></a><span class="co">#> 2 2 5.5</span></span>
|
||
<span id="cb3-9"><a href="#cb3-9" tabindex="-1"></a><span class="co">#> 3 3 6.5</span></span>
|
||
<span id="cb3-10"><a href="#cb3-10" tabindex="-1"></a><span class="co">#> </span></span>
|
||
<span id="cb3-11"><a href="#cb3-11" tabindex="-1"></a><span class="co">#> attr(,"class")</span></span>
|
||
<span id="cb3-12"><a href="#cb3-12" tabindex="-1"></a><span class="co">#> [1] "default" "tbl" "data.frame"</span></span></code></pre></div>
|
||
<div id="header" class="section level2">
|
||
<h2>Header</h2>
|
||
<div id="tweak-header" class="section level3">
|
||
<h3>Tweak header</h3>
|
||
<p>The easiest customization consists of tweaking the header. Implement
|
||
a <code>tbl_sum()</code> method to extend or replace the information
|
||
shown in the header, keeping the original formatting.</p>
|
||
<div class="sourceCode" id="cb4"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" tabindex="-1"></a>tbl_sum.default_header_extend <span class="ot"><-</span> <span class="cf">function</span>(x, ...) {</span>
|
||
<span id="cb4-2"><a href="#cb4-2" tabindex="-1"></a> default_header <span class="ot"><-</span> <span class="fu">NextMethod</span>()</span>
|
||
<span id="cb4-3"><a href="#cb4-3" tabindex="-1"></a> <span class="fu">c</span>(default_header, <span class="st">"New"</span> <span class="ot">=</span> <span class="st">"A new header"</span>)</span>
|
||
<span id="cb4-4"><a href="#cb4-4" tabindex="-1"></a>}</span>
|
||
<span id="cb4-5"><a href="#cb4-5" tabindex="-1"></a></span>
|
||
<span id="cb4-6"><a href="#cb4-6" tabindex="-1"></a><span class="fu">example_tbl</span>(<span class="st">"default_header_extend"</span>)</span>
|
||
<span id="cb4-7"><a href="#cb4-7" tabindex="-1"></a><span class="co">#> # A data frame: 3 × 2</span></span>
|
||
<span id="cb4-8"><a href="#cb4-8" tabindex="-1"></a><span class="co">#> # New: A new header</span></span>
|
||
<span id="cb4-9"><a href="#cb4-9" tabindex="-1"></a><span class="co">#> a b$c $d</span></span>
|
||
<span id="cb4-10"><a href="#cb4-10" tabindex="-1"></a><span class="co">#> <chr> <int> <dbl></span></span>
|
||
<span id="cb4-11"><a href="#cb4-11" tabindex="-1"></a><span class="co">#> 1 a 1 4.5</span></span>
|
||
<span id="cb4-12"><a href="#cb4-12" tabindex="-1"></a><span class="co">#> 2 b 2 5.5</span></span>
|
||
<span id="cb4-13"><a href="#cb4-13" tabindex="-1"></a><span class="co">#> 3 c 3 6.5</span></span>
|
||
<span id="cb4-14"><a href="#cb4-14" tabindex="-1"></a></span>
|
||
<span id="cb4-15"><a href="#cb4-15" tabindex="-1"></a>tbl_sum.default_header_replace <span class="ot"><-</span> <span class="cf">function</span>(x, ...) {</span>
|
||
<span id="cb4-16"><a href="#cb4-16" tabindex="-1"></a> <span class="fu">c</span>(<span class="st">"Override"</span> <span class="ot">=</span> <span class="st">"Replace all headers"</span>)</span>
|
||
<span id="cb4-17"><a href="#cb4-17" tabindex="-1"></a>}</span>
|
||
<span id="cb4-18"><a href="#cb4-18" tabindex="-1"></a></span>
|
||
<span id="cb4-19"><a href="#cb4-19" tabindex="-1"></a><span class="fu">example_tbl</span>(<span class="st">"default_header_replace"</span>)</span>
|
||
<span id="cb4-20"><a href="#cb4-20" tabindex="-1"></a><span class="co">#> # Override: Replace all headers</span></span>
|
||
<span id="cb4-21"><a href="#cb4-21" tabindex="-1"></a><span class="co">#> a b$c $d</span></span>
|
||
<span id="cb4-22"><a href="#cb4-22" tabindex="-1"></a><span class="co">#> <chr> <int> <dbl></span></span>
|
||
<span id="cb4-23"><a href="#cb4-23" tabindex="-1"></a><span class="co">#> 1 a 1 4.5</span></span>
|
||
<span id="cb4-24"><a href="#cb4-24" tabindex="-1"></a><span class="co">#> 2 b 2 5.5</span></span>
|
||
<span id="cb4-25"><a href="#cb4-25" tabindex="-1"></a><span class="co">#> 3 c 3 6.5</span></span></code></pre></div>
|
||
</div>
|
||
<div id="restyle-header" class="section level3">
|
||
<h3>Restyle header</h3>
|
||
<p>To style the header in a different way, implement a
|
||
<code>tbl_format_header()</code> method. The implementation is
|
||
responsible for the entire formatting and styling, including the leading
|
||
hash.</p>
|
||
<div class="sourceCode" id="cb5"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" tabindex="-1"></a>tbl_format_header.custom_header_replace <span class="ot"><-</span> <span class="cf">function</span>(x, setup, ...) {</span>
|
||
<span id="cb5-2"><a href="#cb5-2" tabindex="-1"></a> cli<span class="sc">::</span><span class="fu">style_italic</span>(<span class="fu">names</span>(setup<span class="sc">$</span>tbl_sum), <span class="st">" = "</span>, setup<span class="sc">$</span>tbl_sum)</span>
|
||
<span id="cb5-3"><a href="#cb5-3" tabindex="-1"></a>}</span>
|
||
<span id="cb5-4"><a href="#cb5-4" tabindex="-1"></a></span>
|
||
<span id="cb5-5"><a href="#cb5-5" tabindex="-1"></a><span class="fu">example_tbl</span>(<span class="st">"custom_header_replace"</span>)</span>
|
||
<span id="cb5-6"><a href="#cb5-6" tabindex="-1"></a><span class="co">#> A data frame = 3 × 2</span></span>
|
||
<span id="cb5-7"><a href="#cb5-7" tabindex="-1"></a><span class="co">#> a b$c $d</span></span>
|
||
<span id="cb5-8"><a href="#cb5-8" tabindex="-1"></a><span class="co">#> <chr> <int> <dbl></span></span>
|
||
<span id="cb5-9"><a href="#cb5-9" tabindex="-1"></a><span class="co">#> 1 a 1 4.5</span></span>
|
||
<span id="cb5-10"><a href="#cb5-10" tabindex="-1"></a><span class="co">#> 2 b 2 5.5</span></span>
|
||
<span id="cb5-11"><a href="#cb5-11" tabindex="-1"></a><span class="co">#> 3 c 3 6.5</span></span></code></pre></div>
|
||
</div>
|
||
</div>
|
||
<div id="footer" class="section level2">
|
||
<h2>Footer</h2>
|
||
<div id="restyle-footer" class="section level3">
|
||
<h3>Restyle footer</h3>
|
||
<p>Similarly, to add information the footer, or to replace it entirely,
|
||
implement a <code>tbl_format_footer()</code> method. Here, as in all
|
||
<code>tbl_format_*()</code> methods, you can use the information
|
||
contained in the setup object, see <code>?new_tbl_format_setup</code>
|
||
for the available fields. Again, the implementation is responsible for
|
||
the entire formatting and styling, including the leading hash if
|
||
needed.</p>
|
||
<div class="sourceCode" id="cb6"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" tabindex="-1"></a>tbl_format_footer.custom_footer_extend <span class="ot"><-</span> <span class="cf">function</span>(x, setup, ...) {</span>
|
||
<span id="cb6-2"><a href="#cb6-2" tabindex="-1"></a> default_footer <span class="ot"><-</span> <span class="fu">NextMethod</span>()</span>
|
||
<span id="cb6-3"><a href="#cb6-3" tabindex="-1"></a></span>
|
||
<span id="cb6-4"><a href="#cb6-4" tabindex="-1"></a> extra_info <span class="ot"><-</span> <span class="st">"and with extra info in the footer"</span></span>
|
||
<span id="cb6-5"><a href="#cb6-5" tabindex="-1"></a> extra_footer <span class="ot"><-</span> <span class="fu">style_subtle</span>(<span class="fu">paste0</span>(<span class="st">"# "</span>, cli<span class="sc">::</span>symbol<span class="sc">$</span>ellipsis, <span class="st">" "</span>, extra_info))</span>
|
||
<span id="cb6-6"><a href="#cb6-6" tabindex="-1"></a></span>
|
||
<span id="cb6-7"><a href="#cb6-7" tabindex="-1"></a> <span class="fu">c</span>(default_footer, extra_footer)</span>
|
||
<span id="cb6-8"><a href="#cb6-8" tabindex="-1"></a>}</span>
|
||
<span id="cb6-9"><a href="#cb6-9" tabindex="-1"></a></span>
|
||
<span id="cb6-10"><a href="#cb6-10" tabindex="-1"></a><span class="fu">print</span>(<span class="fu">example_tbl</span>(<span class="st">"custom_footer_extend"</span>), <span class="at">n =</span> <span class="dv">2</span>)</span>
|
||
<span id="cb6-11"><a href="#cb6-11" tabindex="-1"></a><span class="co">#> # A data frame: 3 × 2</span></span>
|
||
<span id="cb6-12"><a href="#cb6-12" tabindex="-1"></a><span class="co">#> a b$c $d</span></span>
|
||
<span id="cb6-13"><a href="#cb6-13" tabindex="-1"></a><span class="co">#> <chr> <int> <dbl></span></span>
|
||
<span id="cb6-14"><a href="#cb6-14" tabindex="-1"></a><span class="co">#> 1 a 1 4.5</span></span>
|
||
<span id="cb6-15"><a href="#cb6-15" tabindex="-1"></a><span class="co">#> 2 b 2 5.5</span></span>
|
||
<span id="cb6-16"><a href="#cb6-16" tabindex="-1"></a><span class="co">#> # ℹ 1 more row</span></span>
|
||
<span id="cb6-17"><a href="#cb6-17" tabindex="-1"></a><span class="co">#> # … and with extra info in the footer</span></span>
|
||
<span id="cb6-18"><a href="#cb6-18" tabindex="-1"></a></span>
|
||
<span id="cb6-19"><a href="#cb6-19" tabindex="-1"></a>tbl_format_footer.custom_footer_replace <span class="ot"><-</span> <span class="cf">function</span>(x, setup, ...) {</span>
|
||
<span id="cb6-20"><a href="#cb6-20" tabindex="-1"></a> <span class="fu">paste0</span>(<span class="st">"The table has "</span>, setup<span class="sc">$</span>rows_total, <span class="st">" rows in total."</span>)</span>
|
||
<span id="cb6-21"><a href="#cb6-21" tabindex="-1"></a>}</span>
|
||
<span id="cb6-22"><a href="#cb6-22" tabindex="-1"></a></span>
|
||
<span id="cb6-23"><a href="#cb6-23" tabindex="-1"></a><span class="fu">print</span>(<span class="fu">example_tbl</span>(<span class="st">"custom_footer_replace"</span>), <span class="at">n =</span> <span class="dv">2</span>)</span>
|
||
<span id="cb6-24"><a href="#cb6-24" tabindex="-1"></a><span class="co">#> # A data frame: 3 × 2</span></span>
|
||
<span id="cb6-25"><a href="#cb6-25" tabindex="-1"></a><span class="co">#> a b$c $d</span></span>
|
||
<span id="cb6-26"><a href="#cb6-26" tabindex="-1"></a><span class="co">#> <chr> <int> <dbl></span></span>
|
||
<span id="cb6-27"><a href="#cb6-27" tabindex="-1"></a><span class="co">#> 1 a 1 4.5</span></span>
|
||
<span id="cb6-28"><a href="#cb6-28" tabindex="-1"></a><span class="co">#> 2 b 2 5.5</span></span>
|
||
<span id="cb6-29"><a href="#cb6-29" tabindex="-1"></a><span class="co">#> The table has 3 rows in total.</span></span></code></pre></div>
|
||
</div>
|
||
<div id="compute-additional-info-beforehand" class="section level3">
|
||
<h3>Compute additional info beforehand</h3>
|
||
<p>If the same information needs to be displayed in several parts (e.g.,
|
||
in both header and footer), it is useful to compute it in
|
||
<code>tbl_format_setup()</code> and store it in the setup object. New
|
||
elements may be added to the setup object, existing elements should not
|
||
be overwritten. Exception: the <code>tbl_sum</code> element contains the
|
||
output of <code>tbl_sum()</code> and can be enhanced with additional
|
||
elements.</p>
|
||
<div class="sourceCode" id="cb7"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" tabindex="-1"></a>tbl_format_setup.extra_info <span class="ot"><-</span> <span class="cf">function</span>(x, width, ...) {</span>
|
||
<span id="cb7-2"><a href="#cb7-2" tabindex="-1"></a> setup <span class="ot"><-</span> <span class="fu">NextMethod</span>()</span>
|
||
<span id="cb7-3"><a href="#cb7-3" tabindex="-1"></a> cells <span class="ot"><-</span> <span class="fu">prod</span>(<span class="fu">dim</span>(x))</span>
|
||
<span id="cb7-4"><a href="#cb7-4" tabindex="-1"></a> setup<span class="sc">$</span>cells <span class="ot"><-</span> cells</span>
|
||
<span id="cb7-5"><a href="#cb7-5" tabindex="-1"></a> setup<span class="sc">$</span>tbl_sum <span class="ot"><-</span> <span class="fu">c</span>(setup<span class="sc">$</span>tbl_sum, <span class="st">"Cells"</span> <span class="ot">=</span> <span class="fu">as.character</span>(cells))</span>
|
||
<span id="cb7-6"><a href="#cb7-6" tabindex="-1"></a> setup</span>
|
||
<span id="cb7-7"><a href="#cb7-7" tabindex="-1"></a>}</span>
|
||
<span id="cb7-8"><a href="#cb7-8" tabindex="-1"></a></span>
|
||
<span id="cb7-9"><a href="#cb7-9" tabindex="-1"></a>tbl_format_footer.extra_info <span class="ot"><-</span> <span class="cf">function</span>(x, setup, ...) {</span>
|
||
<span id="cb7-10"><a href="#cb7-10" tabindex="-1"></a> <span class="fu">paste0</span>(<span class="st">"The table has "</span>, setup<span class="sc">$</span>cells, <span class="st">" cells in total."</span>)</span>
|
||
<span id="cb7-11"><a href="#cb7-11" tabindex="-1"></a>}</span>
|
||
<span id="cb7-12"><a href="#cb7-12" tabindex="-1"></a></span>
|
||
<span id="cb7-13"><a href="#cb7-13" tabindex="-1"></a><span class="fu">example_tbl</span>(<span class="st">"extra_info"</span>)</span>
|
||
<span id="cb7-14"><a href="#cb7-14" tabindex="-1"></a><span class="co">#> # A data frame: 3 × 2</span></span>
|
||
<span id="cb7-15"><a href="#cb7-15" tabindex="-1"></a><span class="co">#> # Cells: 6</span></span>
|
||
<span id="cb7-16"><a href="#cb7-16" tabindex="-1"></a><span class="co">#> a b$c $d</span></span>
|
||
<span id="cb7-17"><a href="#cb7-17" tabindex="-1"></a><span class="co">#> <chr> <int> <dbl></span></span>
|
||
<span id="cb7-18"><a href="#cb7-18" tabindex="-1"></a><span class="co">#> 1 a 1 4.5</span></span>
|
||
<span id="cb7-19"><a href="#cb7-19" tabindex="-1"></a><span class="co">#> 2 b 2 5.5</span></span>
|
||
<span id="cb7-20"><a href="#cb7-20" tabindex="-1"></a><span class="co">#> 3 c 3 6.5</span></span>
|
||
<span id="cb7-21"><a href="#cb7-21" tabindex="-1"></a><span class="co">#> The table has 6 cells in total.</span></span></code></pre></div>
|
||
</div>
|
||
</div>
|
||
<div id="row-ids" class="section level2">
|
||
<h2>Row IDs</h2>
|
||
<p>By implementing the generic <code>ctl_new_rowid_pillar()</code>,
|
||
printing of the row ID column can be customized. In order to print Roman
|
||
instead of Arabic numerals, one could use <code>utils::as.roman()</code>
|
||
to generate the corresponding sequence and build up a row ID pillar
|
||
using <code>new_pillar()</code> and associated methods as has been
|
||
introduced previously.</p>
|
||
<div class="sourceCode" id="cb8"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1" tabindex="-1"></a>ctl_new_rowid_pillar.pillar_roman <span class="ot"><-</span> <span class="cf">function</span>(controller, x, width, ...) {</span>
|
||
<span id="cb8-2"><a href="#cb8-2" tabindex="-1"></a> out <span class="ot"><-</span> <span class="fu">NextMethod</span>()</span>
|
||
<span id="cb8-3"><a href="#cb8-3" tabindex="-1"></a> rowid <span class="ot"><-</span> utils<span class="sc">::</span><span class="fu">as.roman</span>(<span class="fu">seq_len</span>(<span class="fu">nrow</span>(x)))</span>
|
||
<span id="cb8-4"><a href="#cb8-4" tabindex="-1"></a> width <span class="ot"><-</span> <span class="fu">max</span>(<span class="fu">nchar</span>(<span class="fu">as.character</span>(rowid)))</span>
|
||
<span id="cb8-5"><a href="#cb8-5" tabindex="-1"></a> <span class="fu">new_pillar</span>(</span>
|
||
<span id="cb8-6"><a href="#cb8-6" tabindex="-1"></a> <span class="fu">list</span>(</span>
|
||
<span id="cb8-7"><a href="#cb8-7" tabindex="-1"></a> <span class="at">title =</span> out<span class="sc">$</span>title,</span>
|
||
<span id="cb8-8"><a href="#cb8-8" tabindex="-1"></a> <span class="at">type =</span> out<span class="sc">$</span>type,</span>
|
||
<span id="cb8-9"><a href="#cb8-9" tabindex="-1"></a> <span class="at">data =</span> <span class="fu">pillar_component</span>(</span>
|
||
<span id="cb8-10"><a href="#cb8-10" tabindex="-1"></a> <span class="fu">new_pillar_shaft</span>(<span class="fu">list</span>(<span class="at">row_ids =</span> rowid),</span>
|
||
<span id="cb8-11"><a href="#cb8-11" tabindex="-1"></a> <span class="at">width =</span> width,</span>
|
||
<span id="cb8-12"><a href="#cb8-12" tabindex="-1"></a> <span class="at">class =</span> <span class="st">"pillar_rif_shaft"</span></span>
|
||
<span id="cb8-13"><a href="#cb8-13" tabindex="-1"></a> )</span>
|
||
<span id="cb8-14"><a href="#cb8-14" tabindex="-1"></a> )</span>
|
||
<span id="cb8-15"><a href="#cb8-15" tabindex="-1"></a> ),</span>
|
||
<span id="cb8-16"><a href="#cb8-16" tabindex="-1"></a> <span class="at">width =</span> width</span>
|
||
<span id="cb8-17"><a href="#cb8-17" tabindex="-1"></a> )</span>
|
||
<span id="cb8-18"><a href="#cb8-18" tabindex="-1"></a>}</span>
|
||
<span id="cb8-19"><a href="#cb8-19" tabindex="-1"></a></span>
|
||
<span id="cb8-20"><a href="#cb8-20" tabindex="-1"></a><span class="fu">example_tbl</span>(<span class="st">"pillar_roman"</span>)</span>
|
||
<span id="cb8-21"><a href="#cb8-21" tabindex="-1"></a><span class="co">#> # A data frame: 3 × 2</span></span>
|
||
<span id="cb8-22"><a href="#cb8-22" tabindex="-1"></a><span class="co">#> a b$c $d</span></span>
|
||
<span id="cb8-23"><a href="#cb8-23" tabindex="-1"></a><span class="co">#> <chr> <int> <dbl></span></span>
|
||
<span id="cb8-24"><a href="#cb8-24" tabindex="-1"></a><span class="co">#> I a 1 4.5</span></span>
|
||
<span id="cb8-25"><a href="#cb8-25" tabindex="-1"></a><span class="co">#> II b 2 5.5</span></span>
|
||
<span id="cb8-26"><a href="#cb8-26" tabindex="-1"></a><span class="co">#> III c 3 6.5</span></span></code></pre></div>
|
||
</div>
|
||
<div id="body" class="section level2">
|
||
<h2>Body</h2>
|
||
<div id="tweak-pillar-composition" class="section level3">
|
||
<h3>Tweak pillar composition</h3>
|
||
<p>Pillars consist of components, see <code>?new_pillar_component</code>
|
||
for details. Extend or override the <code>ctl_new_pillar()</code> method
|
||
to alter the appearance. The example below adds table rules of constant
|
||
width to the output.</p>
|
||
<div class="sourceCode" id="cb9"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" tabindex="-1"></a>ctl_new_pillar.pillar_rule <span class="ot"><-</span> <span class="cf">function</span>(controller, x, width, ..., <span class="at">title =</span> <span class="cn">NULL</span>) {</span>
|
||
<span id="cb9-2"><a href="#cb9-2" tabindex="-1"></a> out <span class="ot"><-</span> <span class="fu">NextMethod</span>()</span>
|
||
<span id="cb9-3"><a href="#cb9-3" tabindex="-1"></a> <span class="fu">new_pillar</span>(<span class="fu">list</span>(</span>
|
||
<span id="cb9-4"><a href="#cb9-4" tabindex="-1"></a> <span class="at">top_rule =</span> <span class="fu">new_pillar_component</span>(<span class="fu">list</span>(<span class="st">"========"</span>), <span class="at">width =</span> <span class="dv">8</span>),</span>
|
||
<span id="cb9-5"><a href="#cb9-5" tabindex="-1"></a> <span class="at">title =</span> out<span class="sc">$</span>title,</span>
|
||
<span id="cb9-6"><a href="#cb9-6" tabindex="-1"></a> <span class="at">type =</span> out<span class="sc">$</span>type,</span>
|
||
<span id="cb9-7"><a href="#cb9-7" tabindex="-1"></a> <span class="at">mid_rule =</span> <span class="fu">new_pillar_component</span>(<span class="fu">list</span>(<span class="st">"--------"</span>), <span class="at">width =</span> <span class="dv">8</span>),</span>
|
||
<span id="cb9-8"><a href="#cb9-8" tabindex="-1"></a> <span class="at">data =</span> out<span class="sc">$</span>data,</span>
|
||
<span id="cb9-9"><a href="#cb9-9" tabindex="-1"></a> <span class="at">bottom_rule =</span> <span class="fu">new_pillar_component</span>(<span class="fu">list</span>(<span class="st">"========"</span>), <span class="at">width =</span> <span class="dv">8</span>)</span>
|
||
<span id="cb9-10"><a href="#cb9-10" tabindex="-1"></a> ))</span>
|
||
<span id="cb9-11"><a href="#cb9-11" tabindex="-1"></a>}</span>
|
||
<span id="cb9-12"><a href="#cb9-12" tabindex="-1"></a></span>
|
||
<span id="cb9-13"><a href="#cb9-13" tabindex="-1"></a><span class="fu">example_tbl</span>(<span class="st">"pillar_rule"</span>)</span>
|
||
<span id="cb9-14"><a href="#cb9-14" tabindex="-1"></a><span class="co">#> # A data frame: 3 × 2</span></span>
|
||
<span id="cb9-15"><a href="#cb9-15" tabindex="-1"></a><span class="co">#> ======== ======== ========</span></span>
|
||
<span id="cb9-16"><a href="#cb9-16" tabindex="-1"></a><span class="co">#> a b$c $d</span></span>
|
||
<span id="cb9-17"><a href="#cb9-17" tabindex="-1"></a><span class="co">#> <chr> <int> <dbl></span></span>
|
||
<span id="cb9-18"><a href="#cb9-18" tabindex="-1"></a><span class="co">#> -------- -------- --------</span></span>
|
||
<span id="cb9-19"><a href="#cb9-19" tabindex="-1"></a><span class="co">#> 1 a 1 4.5</span></span>
|
||
<span id="cb9-20"><a href="#cb9-20" tabindex="-1"></a><span class="co">#> 2 b 2 5.5</span></span>
|
||
<span id="cb9-21"><a href="#cb9-21" tabindex="-1"></a><span class="co">#> 3 c 3 6.5</span></span>
|
||
<span id="cb9-22"><a href="#cb9-22" tabindex="-1"></a><span class="co">#> ======== ======== ========</span></span></code></pre></div>
|
||
<p>To make the width adaptive, we implement a <code>"rule"</code> class
|
||
with a <code>format()</code> method that formats rules to prespecified
|
||
widths.</p>
|
||
<div class="sourceCode" id="cb10"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" tabindex="-1"></a>rule <span class="ot"><-</span> <span class="cf">function</span>(<span class="at">char =</span> <span class="st">"-"</span>) {</span>
|
||
<span id="cb10-2"><a href="#cb10-2" tabindex="-1"></a> <span class="fu">stopifnot</span>(<span class="fu">nchar</span>(char) <span class="sc">==</span> <span class="dv">1</span>)</span>
|
||
<span id="cb10-3"><a href="#cb10-3" tabindex="-1"></a> <span class="fu">structure</span>(char, <span class="at">class =</span> <span class="st">"rule"</span>)</span>
|
||
<span id="cb10-4"><a href="#cb10-4" tabindex="-1"></a>}</span>
|
||
<span id="cb10-5"><a href="#cb10-5" tabindex="-1"></a></span>
|
||
<span id="cb10-6"><a href="#cb10-6" tabindex="-1"></a>format.rule <span class="ot"><-</span> <span class="cf">function</span>(x, width, ...) {</span>
|
||
<span id="cb10-7"><a href="#cb10-7" tabindex="-1"></a> <span class="fu">paste</span>(<span class="fu">rep</span>(x, width), <span class="at">collapse =</span> <span class="st">""</span>)</span>
|
||
<span id="cb10-8"><a href="#cb10-8" tabindex="-1"></a>}</span>
|
||
<span id="cb10-9"><a href="#cb10-9" tabindex="-1"></a></span>
|
||
<span id="cb10-10"><a href="#cb10-10" tabindex="-1"></a>ctl_new_pillar.pillar_rule_adaptive <span class="ot"><-</span> <span class="cf">function</span>(controller, x, width, ..., <span class="at">title =</span> <span class="cn">NULL</span>) {</span>
|
||
<span id="cb10-11"><a href="#cb10-11" tabindex="-1"></a> out <span class="ot"><-</span> <span class="fu">NextMethod</span>()</span>
|
||
<span id="cb10-12"><a href="#cb10-12" tabindex="-1"></a> <span class="cf">if</span> (<span class="fu">is.null</span>(out)) {</span>
|
||
<span id="cb10-13"><a href="#cb10-13" tabindex="-1"></a> <span class="fu">return</span>(<span class="cn">NULL</span>)</span>
|
||
<span id="cb10-14"><a href="#cb10-14" tabindex="-1"></a> }</span>
|
||
<span id="cb10-15"><a href="#cb10-15" tabindex="-1"></a></span>
|
||
<span id="cb10-16"><a href="#cb10-16" tabindex="-1"></a> <span class="fu">new_pillar</span>(<span class="fu">list</span>(</span>
|
||
<span id="cb10-17"><a href="#cb10-17" tabindex="-1"></a> <span class="at">top_rule =</span> <span class="fu">new_pillar_component</span>(<span class="fu">list</span>(<span class="fu">rule</span>(<span class="st">"="</span>)), <span class="at">width =</span> <span class="dv">1</span>),</span>
|
||
<span id="cb10-18"><a href="#cb10-18" tabindex="-1"></a> <span class="at">title =</span> out<span class="sc">$</span>title,</span>
|
||
<span id="cb10-19"><a href="#cb10-19" tabindex="-1"></a> <span class="at">type =</span> out<span class="sc">$</span>type,</span>
|
||
<span id="cb10-20"><a href="#cb10-20" tabindex="-1"></a> <span class="at">mid_rule =</span> <span class="fu">new_pillar_component</span>(<span class="fu">list</span>(<span class="fu">rule</span>(<span class="st">"-"</span>)), <span class="at">width =</span> <span class="dv">1</span>),</span>
|
||
<span id="cb10-21"><a href="#cb10-21" tabindex="-1"></a> <span class="at">data =</span> out<span class="sc">$</span>data,</span>
|
||
<span id="cb10-22"><a href="#cb10-22" tabindex="-1"></a> <span class="at">bottom_rule =</span> <span class="fu">new_pillar_component</span>(<span class="fu">list</span>(<span class="fu">rule</span>(<span class="st">"="</span>)), <span class="at">width =</span> <span class="dv">1</span>)</span>
|
||
<span id="cb10-23"><a href="#cb10-23" tabindex="-1"></a> ))</span>
|
||
<span id="cb10-24"><a href="#cb10-24" tabindex="-1"></a>}</span>
|
||
<span id="cb10-25"><a href="#cb10-25" tabindex="-1"></a></span>
|
||
<span id="cb10-26"><a href="#cb10-26" tabindex="-1"></a><span class="fu">example_tbl</span>(<span class="st">"pillar_rule_adaptive"</span>)</span>
|
||
<span id="cb10-27"><a href="#cb10-27" tabindex="-1"></a><span class="co">#> # A data frame: 3 × 2</span></span>
|
||
<span id="cb10-28"><a href="#cb10-28" tabindex="-1"></a><span class="co">#> = = =</span></span>
|
||
<span id="cb10-29"><a href="#cb10-29" tabindex="-1"></a><span class="co">#> a b$c $d</span></span>
|
||
<span id="cb10-30"><a href="#cb10-30" tabindex="-1"></a><span class="co">#> <chr> <int> <dbl></span></span>
|
||
<span id="cb10-31"><a href="#cb10-31" tabindex="-1"></a><span class="co">#> - - -</span></span>
|
||
<span id="cb10-32"><a href="#cb10-32" tabindex="-1"></a><span class="co">#> 1 a 1 4.5</span></span>
|
||
<span id="cb10-33"><a href="#cb10-33" tabindex="-1"></a><span class="co">#> 2 b 2 5.5</span></span>
|
||
<span id="cb10-34"><a href="#cb10-34" tabindex="-1"></a><span class="co">#> 3 c 3 6.5</span></span>
|
||
<span id="cb10-35"><a href="#cb10-35" tabindex="-1"></a><span class="co">#> = = =</span></span></code></pre></div>
|
||
</div>
|
||
<div id="tweak-display-of-compound-pillars" class="section level3">
|
||
<h3>Tweak display of compound pillars</h3>
|
||
<p>Compound pillars are created by <code>ctl_new_pillar_list()</code>
|
||
for columns that contain a data frame, a matrix or an array. The default
|
||
implementation also calls <code>ctl_new_pillar()</code> shown above. The
|
||
(somewhat artificial) example hides all data frame columns in a column
|
||
with the type <code>"<hidden>"</code>.</p>
|
||
<div class="sourceCode" id="cb11"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1" tabindex="-1"></a>ctl_new_pillar_list.hide_df <span class="ot"><-</span> <span class="cf">function</span>(controller, x, width, ..., <span class="at">title =</span> <span class="cn">NULL</span>) {</span>
|
||
<span id="cb11-2"><a href="#cb11-2" tabindex="-1"></a> <span class="cf">if</span> (<span class="sc">!</span><span class="fu">is.data.frame</span>(x)) {</span>
|
||
<span id="cb11-3"><a href="#cb11-3" tabindex="-1"></a> <span class="fu">return</span>(<span class="fu">NextMethod</span>())</span>
|
||
<span id="cb11-4"><a href="#cb11-4" tabindex="-1"></a> }</span>
|
||
<span id="cb11-5"><a href="#cb11-5" tabindex="-1"></a></span>
|
||
<span id="cb11-6"><a href="#cb11-6" tabindex="-1"></a> <span class="cf">if</span> (width <span class="sc"><</span> <span class="dv">8</span>) {</span>
|
||
<span id="cb11-7"><a href="#cb11-7" tabindex="-1"></a> <span class="fu">return</span>(<span class="cn">NULL</span>)</span>
|
||
<span id="cb11-8"><a href="#cb11-8" tabindex="-1"></a> }</span>
|
||
<span id="cb11-9"><a href="#cb11-9" tabindex="-1"></a></span>
|
||
<span id="cb11-10"><a href="#cb11-10" tabindex="-1"></a> <span class="fu">list</span>(<span class="fu">new_pillar</span>(</span>
|
||
<span id="cb11-11"><a href="#cb11-11" tabindex="-1"></a> <span class="fu">list</span>(</span>
|
||
<span id="cb11-12"><a href="#cb11-12" tabindex="-1"></a> <span class="at">title =</span> <span class="fu">pillar_component</span>(<span class="fu">new_pillar_title</span>(title)),</span>
|
||
<span id="cb11-13"><a href="#cb11-13" tabindex="-1"></a> <span class="at">type =</span> <span class="fu">new_pillar_component</span>(<span class="fu">list</span>(<span class="st">"<hidden>"</span>), <span class="at">width =</span> <span class="dv">8</span>),</span>
|
||
<span id="cb11-14"><a href="#cb11-14" tabindex="-1"></a> <span class="at">data =</span> <span class="fu">new_pillar_component</span>(<span class="fu">list</span>(<span class="st">""</span>), <span class="at">width =</span> <span class="dv">1</span>)</span>
|
||
<span id="cb11-15"><a href="#cb11-15" tabindex="-1"></a> ),</span>
|
||
<span id="cb11-16"><a href="#cb11-16" tabindex="-1"></a> <span class="at">width =</span> <span class="dv">8</span></span>
|
||
<span id="cb11-17"><a href="#cb11-17" tabindex="-1"></a> ))</span>
|
||
<span id="cb11-18"><a href="#cb11-18" tabindex="-1"></a>}</span>
|
||
<span id="cb11-19"><a href="#cb11-19" tabindex="-1"></a></span>
|
||
<span id="cb11-20"><a href="#cb11-20" tabindex="-1"></a><span class="fu">example_tbl</span>(<span class="st">"hide_df"</span>)</span>
|
||
<span id="cb11-21"><a href="#cb11-21" tabindex="-1"></a><span class="co">#> # A data frame: 3 × 2</span></span>
|
||
<span id="cb11-22"><a href="#cb11-22" tabindex="-1"></a><span class="co">#> <hidden></span></span>
|
||
<span id="cb11-23"><a href="#cb11-23" tabindex="-1"></a><span class="co">#> </span></span>
|
||
<span id="cb11-24"><a href="#cb11-24" tabindex="-1"></a><span class="co">#> 1 <hidden></span></span>
|
||
<span id="cb11-25"><a href="#cb11-25" tabindex="-1"></a><span class="co">#> 2 </span></span>
|
||
<span id="cb11-26"><a href="#cb11-26" tabindex="-1"></a><span class="co">#> 3 <hidden></span></span></code></pre></div>
|
||
</div>
|
||
<div id="restyle-body" class="section level3">
|
||
<h3>Restyle body</h3>
|
||
<p>Last but not least, it is also possible to completely alter the
|
||
display of the body by overriding <code>tbl_format_body()</code>. The
|
||
example below uses plain data frame output for a tibble.</p>
|
||
<div class="sourceCode" id="cb12"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1" tabindex="-1"></a>tbl_format_body.oldskool <span class="ot"><-</span> <span class="cf">function</span>(x, setup, ...) {</span>
|
||
<span id="cb12-2"><a href="#cb12-2" tabindex="-1"></a> <span class="fu">capture.output</span>(<span class="fu">print.data.frame</span>(setup<span class="sc">$</span>df))</span>
|
||
<span id="cb12-3"><a href="#cb12-3" tabindex="-1"></a>}</span>
|
||
<span id="cb12-4"><a href="#cb12-4" tabindex="-1"></a></span>
|
||
<span id="cb12-5"><a href="#cb12-5" tabindex="-1"></a><span class="fu">print</span>(<span class="fu">example_tbl</span>(<span class="st">"oldskool"</span>), <span class="at">n =</span> <span class="dv">2</span>)</span>
|
||
<span id="cb12-6"><a href="#cb12-6" tabindex="-1"></a><span class="co">#> # A data frame: 3 × 2</span></span>
|
||
<span id="cb12-7"><a href="#cb12-7" tabindex="-1"></a><span class="co">#> a b.c b.d</span></span>
|
||
<span id="cb12-8"><a href="#cb12-8" tabindex="-1"></a><span class="co">#> 1 a 1 4.5</span></span>
|
||
<span id="cb12-9"><a href="#cb12-9" tabindex="-1"></a><span class="co">#> 2 b 2 5.5</span></span>
|
||
<span id="cb12-10"><a href="#cb12-10" tabindex="-1"></a><span class="co">#> # ℹ 1 more row</span></span></code></pre></div>
|
||
<p>Note that default printed output is computed in
|
||
<code>tbl_format_setup()</code>, this takes a considerable amount of
|
||
time. If you really need to change the output for the entire body,
|
||
consider providing your own <code>tbl_format_setup()</code> method.</p>
|
||
</div>
|
||
</div>
|
||
|
||
|
||
|
||
<!-- code folding -->
|
||
|
||
|
||
<!-- dynamically load mathjax for compatibility with self-contained -->
|
||
<script>
|
||
(function () {
|
||
var script = document.createElement("script");
|
||
script.type = "text/javascript";
|
||
script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
|
||
document.getElementsByTagName("head")[0].appendChild(script);
|
||
})();
|
||
</script>
|
||
|
||
</body>
|
||
</html>
|