Debian Codegraph @

inst/doc/introducing-the-snakecase-package.html

raw
1 <!DOCTYPE html>
2
3 <html>
4
5 <head>
6
7 <meta charset="utf-8" />
8 <meta name="generator" content="pandoc" />
9 <meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
10
11 <meta name="viewport" content="width=device-width, initial-scale=1" />
12
13 <meta name="author" content="Malte Grosser" />
14
15 <meta name="date" content="2023-08-27" />
16
17 <title>Introducing the snakecase package</title>
18
19 <script>// Pandoc 2.9 adds attributes on both header and div. We remove the former (to
20 // be compatible with the behavior of Pandoc < 2.8).
21 document.addEventListener('DOMContentLoaded', function(e) {
22 var hs = document.querySelectorAll("div.section[class*='level'] > :first-child");
23 var i, h, a;
24 for (i = 0; i < hs.length; i++) {
25 h = hs[i];
26 if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6
27 a = h.attributes;
28 while (a.length > 0) h.removeAttribute(a[0].name);
29 }
30 });
31 </script>
32
33 <style type="text/css">
34 code{white-space: pre-wrap;}
35 span.smallcaps{font-variant: small-caps;}
36 span.underline{text-decoration: underline;}
37 div.column{display: inline-block; vertical-align: top; width: 50%;}
38 div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
39 ul.task-list{list-style: none;}
40 </style>
41
42
43
44 <style type="text/css">
45 code {
46 white-space: pre;
47 }
48 .sourceCode {
49 overflow: visible;
50 }
51 </style>
52 <style type="text/css" data-origin="pandoc">
53 pre > code.sourceCode { white-space: pre; position: relative; }
54 pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
55 pre > code.sourceCode > span:empty { height: 1.2em; }
56 .sourceCode { overflow: visible; }
57 code.sourceCode > span { color: inherit; text-decoration: inherit; }
58 div.sourceCode { margin: 1em 0; }
59 pre.sourceCode { margin: 0; }
60 @media screen {
61 div.sourceCode { overflow: auto; }
62 }
63 @media print {
64 pre > code.sourceCode { white-space: pre-wrap; }
65 pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
66 }
67 pre.numberSource code
68 { counter-reset: source-line 0; }
69 pre.numberSource code > span
70 { position: relative; left: -4em; counter-increment: source-line; }
71 pre.numberSource code > span > a:first-child::before
72 { content: counter(source-line);
73 position: relative; left: -1em; text-align: right; vertical-align: baseline;
74 border: none; display: inline-block;
75 -webkit-touch-callout: none; -webkit-user-select: none;
76 -khtml-user-select: none; -moz-user-select: none;
77 -ms-user-select: none; user-select: none;
78 padding: 0 4px; width: 4em;
79 color: #aaaaaa;
80 }
81 pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
82 div.sourceCode
83 { }
84 @media screen {
85 pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
86 }
87 code span.al { color: #ff0000; font-weight: bold; }
88 code span.an { color: #60a0b0; font-weight: bold; font-style: italic; }
89 code span.at { color: #7d9029; }
90 code span.bn { color: #40a070; }
91 code span.bu { color: #008000; }
92 code span.cf { color: #007020; font-weight: bold; }
93 code span.ch { color: #4070a0; }
94 code span.cn { color: #880000; }
95 code span.co { color: #60a0b0; font-style: italic; }
96 code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; }
97 code span.do { color: #ba2121; font-style: italic; }
98 code span.dt { color: #902000; }
99 code span.dv { color: #40a070; }
100 code span.er { color: #ff0000; font-weight: bold; }
101 code span.ex { }
102 code span.fl { color: #40a070; }
103 code span.fu { color: #06287e; }
104 code span.im { color: #008000; font-weight: bold; }
105 code span.in { color: #60a0b0; font-weight: bold; font-style: italic; }
106 code span.kw { color: #007020; font-weight: bold; }
107 code span.op { color: #666666; }
108 code span.ot { color: #007020; }
109 code span.pp { color: #bc7a00; }
110 code span.sc { color: #4070a0; }
111 code span.ss { color: #bb6688; }
112 code span.st { color: #4070a0; }
113 code span.va { color: #19177c; }
114 code span.vs { color: #4070a0; }
115 code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; }
116 </style>
117 <script>
118 // apply pandoc div.sourceCode style to pre.sourceCode instead
119 (function() {
120 var sheets = document.styleSheets;
121 for (var i = 0; i < sheets.length; i++) {
122 if (sheets[i].ownerNode.dataset["origin"] !== "pandoc") continue;
123 try { var rules = sheets[i].cssRules; } catch (e) { continue; }
124 var j = 0;
125 while (j < rules.length) {
126 var rule = rules[j];
127 // check if there is a div.sourceCode rule
128 if (rule.type !== rule.STYLE_RULE || rule.selectorText !== "div.sourceCode") {
129 j++;
130 continue;
131 }
132 var style = rule.style.cssText;
133 // check if color or background-color is set
134 if (rule.style.color === '' && rule.style.backgroundColor === '') {
135 j++;
136 continue;
137 }
138 // replace div.sourceCode by a pre.sourceCode rule
139 sheets[i].deleteRule(j);
140 sheets[i].insertRule('pre.sourceCode{' + style + '}', j);
141 }
142 }
143 })();
144 </script>
145
146
147
148
149 <style type="text/css">body {
150 background-color: #fff;
151 margin: 1em auto;
152 max-width: 700px;
153 overflow: visible;
154 padding-left: 2em;
155 padding-right: 2em;
156 font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
157 font-size: 14px;
158 line-height: 1.35;
159 }
160 #TOC {
161 clear: both;
162 margin: 0 0 10px 10px;
163 padding: 4px;
164 width: 400px;
165 border: 1px solid #CCCCCC;
166 border-radius: 5px;
167 background-color: #f6f6f6;
168 font-size: 13px;
169 line-height: 1.3;
170 }
171 #TOC .toctitle {
172 font-weight: bold;
173 font-size: 15px;
174 margin-left: 5px;
175 }
176 #TOC ul {
177 padding-left: 40px;
178 margin-left: -1.5em;
179 margin-top: 5px;
180 margin-bottom: 5px;
181 }
182 #TOC ul ul {
183 margin-left: -2em;
184 }
185 #TOC li {
186 line-height: 16px;
187 }
188 table {
189 margin: 1em auto;
190 border-width: 1px;
191 border-color: #DDDDDD;
192 border-style: outset;
193 border-collapse: collapse;
194 }
195 table th {
196 border-width: 2px;
197 padding: 5px;
198 border-style: inset;
199 }
200 table td {
201 border-width: 1px;
202 border-style: inset;
203 line-height: 18px;
204 padding: 5px 5px;
205 }
206 table, table th, table td {
207 border-left-style: none;
208 border-right-style: none;
209 }
210 table thead, table tr.even {
211 background-color: #f7f7f7;
212 }
213 p {
214 margin: 0.5em 0;
215 }
216 blockquote {
217 background-color: #f6f6f6;
218 padding: 0.25em 0.75em;
219 }
220 hr {
221 border-style: solid;
222 border: none;
223 border-top: 1px solid #777;
224 margin: 28px 0;
225 }
226 dl {
227 margin-left: 0;
228 }
229 dl dd {
230 margin-bottom: 13px;
231 margin-left: 13px;
232 }
233 dl dt {
234 font-weight: bold;
235 }
236 ul {
237 margin-top: 0;
238 }
239 ul li {
240 list-style: circle outside;
241 }
242 ul ul {
243 margin-bottom: 0;
244 }
245 pre, code {
246 background-color: #f7f7f7;
247 border-radius: 3px;
248 color: #333;
249 white-space: pre-wrap;
250 }
251 pre {
252 border-radius: 3px;
253 margin: 5px 0px 10px 0px;
254 padding: 10px;
255 }
256 pre:not([class]) {
257 background-color: #f7f7f7;
258 }
259 code {
260 font-family: Consolas, Monaco, 'Courier New', monospace;
261 font-size: 85%;
262 }
263 p > code, li > code {
264 padding: 2px 0px;
265 }
266 div.figure {
267 text-align: center;
268 }
269 img {
270 background-color: #FFFFFF;
271 padding: 2px;
272 border: 1px solid #DDDDDD;
273 border-radius: 3px;
274 border: 1px solid #CCCCCC;
275 margin: 0 5px;
276 }
277 h1 {
278 margin-top: 0;
279 font-size: 35px;
280 line-height: 40px;
281 }
282 h2 {
283 border-bottom: 4px solid #f7f7f7;
284 padding-top: 10px;
285 padding-bottom: 2px;
286 font-size: 145%;
287 }
288 h3 {
289 border-bottom: 2px solid #f7f7f7;
290 padding-top: 10px;
291 font-size: 120%;
292 }
293 h4 {
294 border-bottom: 1px solid #f7f7f7;
295 margin-left: 8px;
296 font-size: 105%;
297 }
298 h5, h6 {
299 border-bottom: 1px solid #ccc;
300 font-size: 105%;
301 }
302 a {
303 color: #0033dd;
304 text-decoration: none;
305 }
306 a:hover {
307 color: #6666ff; }
308 a:visited {
309 color: #800080; }
310 a:visited:hover {
311 color: #BB00BB; }
312 a[href^="http:"] {
313 text-decoration: underline; }
314 a[href^="https:"] {
315 text-decoration: underline; }
316
317 code > span.kw { color: #555; font-weight: bold; }
318 code > span.dt { color: #902000; }
319 code > span.dv { color: #40a070; }
320 code > span.bn { color: #d14; }
321 code > span.fl { color: #d14; }
322 code > span.ch { color: #d14; }
323 code > span.st { color: #d14; }
324 code > span.co { color: #888888; font-style: italic; }
325 code > span.ot { color: #007020; }
326 code > span.al { color: #ff0000; font-weight: bold; }
327 code > span.fu { color: #900; font-weight: bold; }
328 code > span.er { color: #a61717; background-color: #e3d2d2; }
329 </style>
330
331
332
333
334 </head>
335
336 <body>
337
338
339
340
341 <h1 class="title toc-ignore">Introducing the snakecase package</h1>
342 <h4 class="author">Malte Grosser</h4>
343 <h4 class="date">2023-08-27</h4>
344
345
346
347 <p>There are many style guides out there which recommend specific naming
348 conventions for programming languages. At 2017’s useR conference Rasmus
349 Bååth showed quite impressively the variety of cases which even exist
350 within base R in his talk <a href="https://www.youtube.com/watch?v=Pv5dfsHBBKE">“The current state of
351 naming conventions in R”</a>.</p>
352 <p>However, consistent style is not only about naming new objects.</p>
353 <div id="import" class="section level2">
354 <h2>Import</h2>
355 <p>When you do a data analysis, most of the data already exists and you
356 import it from disk, an API or a database. Here is the first moment in
357 your data analysis when you have to decide if you want to rename your
358 data or leave it as it is.</p>
359 <p>Let’s say you have some data named in any of the following
360 conventions</p>
361 <div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" tabindex="-1"></a>string <span class="ot">&lt;-</span> <span class="fu">c</span>(<span class="st">&quot;lowerCamelCase&quot;</span>, <span class="st">&quot;ALL_CAPS&quot;</span>, <span class="st">&quot;IDontKNOWWhat_thisCASE_is&quot;</span>)</span></code></pre></div>
362 <p>You can now easily convert this string for example to snake case
363 via</p>
364 <div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" tabindex="-1"></a><span class="fu">library</span>(snakecase)</span>
365 <span id="cb2-2"><a href="#cb2-2" tabindex="-1"></a><span class="fu">to_snake_case</span>(string)</span>
366 <span id="cb2-3"><a href="#cb2-3" tabindex="-1"></a><span class="co">#&gt; [1] &quot;lower_camel_case&quot; &quot;all_caps&quot; </span></span>
367 <span id="cb2-4"><a href="#cb2-4" tabindex="-1"></a><span class="co">#&gt; [3] &quot;i_dont_know_what_this_case_is&quot;</span></span></code></pre></div>
368 </div>
369 <div id="graphics" class="section level2">
370 <h2>Graphics</h2>
371 <p>Whenever you want to construct a graphic and you don’t like your
372 conventions to come up in it, you can easily convert strings to a more
373 humanly readable output like</p>
374 <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">to_mixed_case</span>(string, <span class="at">sep_out =</span> <span class="st">&quot; &quot;</span>)</span>
375 <span id="cb3-2"><a href="#cb3-2" tabindex="-1"></a><span class="co">#&gt; [1] &quot;lower Camel Case&quot; &quot;All Caps&quot; </span></span>
376 <span id="cb3-3"><a href="#cb3-3" tabindex="-1"></a><span class="co">#&gt; [3] &quot;I Dont Know What this Case is&quot;</span></span></code></pre></div>
377 <p>You might have noticed the <code>sep_out</code> argument. This allows
378 you to combine any case with any output separator to create other well
379 known cases like</p>
380 <div class="sourceCode" id="cb4"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" tabindex="-1"></a><span class="fu">to_snake_case</span>(string, <span class="at">sep_out =</span> <span class="st">&quot;.&quot;</span>)</span>
381 <span id="cb4-2"><a href="#cb4-2" tabindex="-1"></a><span class="co">#&gt; [1] &quot;lower.camel.case&quot; &quot;all.caps&quot; </span></span>
382 <span id="cb4-3"><a href="#cb4-3" tabindex="-1"></a><span class="co">#&gt; [3] &quot;i.dont.know.what.this.case.is&quot;</span></span>
383 <span id="cb4-4"><a href="#cb4-4" tabindex="-1"></a><span class="fu">to_snake_case</span>(string, <span class="at">sep_out =</span> <span class="st">&quot;-&quot;</span>)</span>
384 <span id="cb4-5"><a href="#cb4-5" tabindex="-1"></a><span class="co">#&gt; [1] &quot;lower-camel-case&quot; &quot;all-caps&quot; </span></span>
385 <span id="cb4-6"><a href="#cb4-6" tabindex="-1"></a><span class="co">#&gt; [3] &quot;i-dont-know-what-this-case-is&quot;</span></span></code></pre></div>
386 <p>or completely new ones like</p>
387 <div class="sourceCode" id="cb5"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" tabindex="-1"></a><span class="fu">to_screaming_snake_case</span>(string, <span class="at">sep_out =</span> <span class="st">&quot;=&quot;</span>)</span>
388 <span id="cb5-2"><a href="#cb5-2" tabindex="-1"></a><span class="co">#&gt; [1] &quot;LOWER=CAMEL=CASE&quot; &quot;ALL=CAPS&quot; </span></span>
389 <span id="cb5-3"><a href="#cb5-3" tabindex="-1"></a><span class="co">#&gt; [3] &quot;I=DONT=KNOW=WHAT=THIS=CASE=IS&quot;</span></span></code></pre></div>
390 </div>
391 <div id="export" class="section level2">
392 <h2>Export</h2>
393 <p>Finally, when you are done with your analysis and want to write data
394 back into a .CSV file or your customers database, which has a camel case
395 convention, you can just use</p>
396 <div class="sourceCode" id="cb6"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" tabindex="-1"></a><span class="fu">to_upper_camel_case</span>(string)</span>
397 <span id="cb6-2"><a href="#cb6-2" tabindex="-1"></a><span class="co">#&gt; [1] &quot;LowerCamelCase&quot; &quot;AllCaps&quot; </span></span>
398 <span id="cb6-3"><a href="#cb6-3" tabindex="-1"></a><span class="co">#&gt; [3] &quot;IDontKnowWhatThisCaseIs&quot;</span></span></code></pre></div>
399 </div>
400 <div id="further-information" class="section level2">
401 <h2>Further information</h2>
402 <p>The snakecase package goes quite deep into the little quirks which
403 arise in automatic case conversion. However, it is well tweaked, to
404 handle almost every edge case in an intuitive and elegant manner.</p>
405 <p>To get a complete overview of its functionality like other cases,
406 handling of abbreviations, special input characters, different parsing
407 options, transliterations and more, I recommend you to have a look into
408 the quite extensive <a href="https://github.com/Tazinho/snakecase">readme on its github
409 repository</a>.</p>
410 <p>As the package is relatively small and basically consists of its
411 workhorse function <code>to_any_case()</code>, I can also react quite
412 fast on new <a href="https://github.com/Tazinho/snakecase/issues">issues</a>.</p>
413 <p>And of course I <a href="https://twitter.com/malte_grosser">tweet</a>
414 occasionally about new functionality.</p>
415 <p>To round this up let me give you one advice about best practices: be
416 aware that automatic case conversion depends on the input string and it
417 is recommended to verify the results. Hence you might want to pipe them
418 into <code>dput()</code> and hard-code name changes instead of blindly
419 trusting the output</p>
420 <div class="sourceCode" id="cb7"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" tabindex="-1"></a><span class="fu">library</span>(magrittr)</span>
421 <span id="cb7-2"><a href="#cb7-2" tabindex="-1"></a><span class="fu">to_any_case</span>(<span class="fu">c</span>(<span class="st">&quot;SomeBAdInput&quot;</span>, <span class="st">&quot;someGoodInput&quot;</span>)) <span class="sc">%&gt;%</span> <span class="fu">dput</span>()</span>
422 <span id="cb7-3"><a href="#cb7-3" tabindex="-1"></a><span class="co">#&gt; c(&quot;some_b_ad_input&quot;, &quot;some_good_input&quot;)</span></span></code></pre></div>
423 <p>Happy snakecasing everyone ;)</p>
424 </div>
425
426
427
428 <!-- code folding -->
429
430
431 <!-- dynamically load mathjax for compatibility with self-contained -->
432 <script>
433 (function () {
434 var script = document.createElement("script");
435 script.type = "text/javascript";
436 script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
437 document.getElementsByTagName("head")[0].appendChild(script);
438 })();
439 </script>
440
441 </body>
442 </html>