{"id":80143,"date":"2021-03-22T10:20:00","date_gmt":"2021-03-22T14:20:00","guid":{"rendered":"https:\/\/ibkrcampus.com\/?p=80143"},"modified":"2022-11-21T09:47:15","modified_gmt":"2022-11-21T14:47:15","slug":"5-tips-for-writing-clean-r-code","status":"publish","type":"post","link":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/5-tips-for-writing-clean-r-code\/","title":{"rendered":"5 Tips for Writing Clean R Code"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Clean R Code Is Critical<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Over many years of experience delivering successful projects, I&#8217;ve found one common element in each implementation. A clean, readable, and concise codebase is the key to effective collaboration and provides the highest quality value to the client.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Code review is a crucial part of maintaining a high-quality code process. It is also a great way to share best practices and distribute knowledge among team members. At Appsilon, we treat code review as a must for every project. Read more about how we organize our work in&nbsp;<a href=\"https:\/\/appsilon.com\/remote-data-science-team-best-practices-scrum-github-and-docker\/\" target=\"_blank\" rel=\"noreferrer noopener\">Olga\u2019s blog post<\/a>&nbsp;on best practices&nbsp;recommended for all data science teams.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Having a well-established code review process does not change the fact that the developer is responsible for writing good, clean code! Pointing out all of the code\u2019s basic mistakes is painful, time-consuming, and distracts reviewers from going deep into code logic or improving the code\u2019s effectiveness.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Poorly written code can also harm team morale \u2013 code reviewers are frustrated while code creators might feel offended by a huge number of comments. That is why before sending the code to review, developers need to make sure that the code is as clean as possible.&nbsp;Also, note that there is not always a code reviewer that can come to the rescue. Sometimes you are on your own in a project. Even though you think the code is ok for you now, consider rereading it in a few months \u2013 you want it to be clear to avoid wasting your own time later on.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this article, I summarize the most common mistakes to avoid and outline best practices to follow in programming in general. Follow these tips to speed up the code review iteration process and be a rockstar developer in your reviewer\u2019s eyes!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"comments\">Avoid Comments with Comments<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Adding comments to the code is a crucial developer skill. However, a more critical and harder to master skill is knowing when&nbsp;<em>not<\/em>&nbsp;to add comments. Writing good comments is more of an art than a science. It requires a lot of experience, and you can write entire book chapters about it (e.g.,&nbsp;<a href=\"https:\/\/books.google.pl\/books\/about\/Clean_Code.html?id=hjEFCAAAQBAJ\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>).&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There are few simple rules that you should follow, to, well, avoid comments about your comments:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The comments should add external knowledge to the reader: if they\u2019re explaining what is happening in the code itself, it is a red flag that the code is not clean and needs to be refactored. If some hack was used, then comments might be used to explain what is going on. Comment required business logic or exceptions added on purpose. Try to think of what can be surprising to the future reader and preempt their confusion.<\/li><li>Write only crucial comments! Your comments should not be a dictionary of easily searchable information. In general, comments are distracting and do not explain logic as well as the code does. For example, I recently saw a comment like this in the code:\u00a0<code>trimws(.) # this function trims leading\/trailing white spaces<\/code>\u00a0\u2013 which is redundant. If the reader does not know what function\u00a0<code>trimws<\/code>\u00a0is doing, it can be easily checked. A more robust comment here can be helpful, e.g.:\u00a0<code>trimws(.) # TODO(Marcin Dubel): Trimming white spaces is crucial here due to database entries inconsistency; data needs to be cleaned.<\/code><\/li><li>When writing functions in R, I recommend\u00a0<a href=\"https:\/\/cran.r-project.org\/web\/packages\/roxygen2\/vignettes\/roxygen2.html\" target=\"_blank\" rel=\"noreferrer noopener\">using {roxygen2} comments<\/a>\u00a0even if you are not writing a package. It is an excellent tool for organizing the knowledge about the function goal, parameters, and output.<\/li><li>Only write comments (as well as all parts of code) in English. Making it understandable to all readers might save you encoding issues that can appear if you use special characters from your native language.<\/li><li>In case some code needs to be refactored\/modified in the future, mark it with the\u00a0<code># TODO<\/code>\u00a0comment. Also, add some information to identify you as the author of this comment (to contact in case details are needed) and a brief explanation of why the following code is marked as TODO and not modified right away.<\/li><li>Never leave commented-out code un-commented! It is ok to keep some parts for the future or turn them off for a while, but always mark the reason for this action.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Remember that the comments will stay in the code. If there is something that you would like to tell your reviewer, but only once, add a comment to Pull (Merge) Request and not to the code itself.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example<\/strong>: I recently saw removing part of the code with a comment like: \u201cRemoved as the logic changed.\u201d Ok, good to know, but later that comment in the code looks odd and is redundant, as the reader no longer sees the removed code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"strings\">Strings<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A common problem related to texts is the readability of string concatenations. What I encounter a lot is an overuse of the&nbsp;<code>paste<\/code>&nbsp;function. Don\u2019t get me wrong; it is a great function when your string is simple, e.g.&nbsp;<code>paste(\"My name is\", my_name)<\/code>, but for more complicated forms, it is hard to read:&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>paste(\"My name is\", my_name, \"and I live in\", my_city, \"developing in\", language, \"for over\", years_of_coding)<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A better solution is to use&nbsp;<code>sprintf<\/code>&nbsp;functions or&nbsp;<code>glue<\/code>, e.g.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>glue(\u201cMy name is {my_name} and I live in {my_city} developing in {language} for over {years_of_coding}\u201d)<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Isn\u2019t it clearer without all those commas and quotation marks?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When dealing with many code blocks, it would be great to extract them to separate locations, e.g., to a&nbsp;<strong>.yml file<\/strong>. It makes both code and text blocks easier to read and maintain.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The last tip related to texts: one of the debugging techniques, often used in Shiny applications, is adding&nbsp;<code>print()<\/code>&nbsp;statements. Double-check whether the prints are not left in the code \u2013 this can be quite embarrassing during code review!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"loops\">Loops<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Loops are one of the programming building blocks and are a very powerful tool. Nevertheless, they can be computationally heavy and thus need to be used carefully. The rule of thumb that you should follow is: always double-check if looping is a good option. It is hardly a case that you need to loop over rows in&nbsp;<code>data.frame<\/code>: there should be a&nbsp;<code>{dplyr}<\/code>&nbsp;function to deal with the problem more efficiently.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Another common source of issues is looping over elements using the length of the object, e.g.&nbsp;<code>for(i in 1:length(x)) ...<\/code>. But what if the length of x is zero! Yes, the loop will go another way for iterator values 1, 0. That is probably not your plan. Using&nbsp;<code>seq_along<\/code>&nbsp;or&nbsp;<code>seq_len<\/code>&nbsp;functions are much safer.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, remember about the&nbsp;<code>apply<\/code>&nbsp;family of functions for looping. They are great (not to mention&nbsp;<code>{purrr}<\/code>&nbsp;solutions)! Note that using&nbsp;<code>sapply<\/code>&nbsp;might be commented by the reviewer as not stable \u2013 because this function chooses the type of the output itself! So sometimes it will be a list, sometimes a vector. Using&nbsp;<code>vapply<\/code>&nbsp;is safer, as the programmer defines the expected output class.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"sharing\">Code Sharing<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Even if you are working alone, you probably would like your program to run correctly on other machines. And how crucial it is when you are sharing the code with the team! To achieve this, never use absolute paths in your code, e.g. \u201c\/home\/marcin\/my_files\/old_projects\/september\/project_name\/file.txt\u201d. It won\u2019t be accessible for others. Note that any violation of folder structure will crash the code.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As you should already have a project for all coding work, you need to use paths related to the particular project \u2013 in this case; it will be \u201c.\/file.txt\u201d. What is more, I would suggest keeping all the paths as variables in a single place \u2013 so that renaming a file requires one change in code, not, e.g., twenty in six different files.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Sometimes your software needs to use some credentials or tokens, e.g., to a database or private repositories. You should never commit such secrets to the repository! Even if the entries are the same among the team. Usually, the good practice is to keep such values in&nbsp;<code>.Renviron<\/code>&nbsp;file as environmental variables that are loaded on start and the file itself is ignored in the repo. You can read more about it&nbsp;<a href=\"https:\/\/www.dartistics.com\/renviron.html\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"practices\">Good Programming Practices<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Finally, let\u2019s focus on how you can improve your code. First of all, your code should be easily understandable and clean \u2013 even if you are working alone, when you come back to code after a while, it will make your life easier!&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use specific variable names, even if they seem to be lengthy \u2013 the rule of thumb is that you should be able to guess what is inside just by reading the name, so&nbsp;<code>table_cases_per_country<\/code><em>&nbsp;<\/em>is ok, but&nbsp;<code>tbl1<\/code>&nbsp;is not. Avoid abbreviations. Lengthy is preferable to vague. Keep consistent style for object names (like camelCase or snake_case) agreed among the team members.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Do NOT abbreviate logical values&nbsp;<code>T<\/code>&nbsp;for&nbsp;<code>TRUE<\/code>&nbsp;and&nbsp;<code>F<\/code>&nbsp;for&nbsp;<code>FALSE<\/code>&nbsp;\u2013 the code will work, but&nbsp;<code>T<\/code>&nbsp;and&nbsp;<code>F<\/code>&nbsp;are regular objects that can be overwritten while&nbsp;<code>TRUE<\/code>&nbsp;and&nbsp;<code>FALSE<\/code>&nbsp;are special values.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Do not compare logical values using equations, like&nbsp;<code>if(my_logical == TRUE)<\/code>. If you can compare to&nbsp;<code>TRUE<\/code>, it means your value is already logical, so&nbsp;<code>if(my_logical)<\/code>&nbsp;is enough! If you want to double-check that the value is&nbsp;<code>TRUE<\/code>&nbsp;indeed (and not, e.g.,&nbsp;<code>NA<\/code>), you can use the&nbsp;<code>isTRUE()<\/code>&nbsp;function.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Make sure that your logic statements are correct. Check if you understand the difference in R between&nbsp;<a href=\"https:\/\/stat.ethz.ch\/R-manual\/R-devel\/library\/base\/html\/Logic.html\" target=\"_blank\" rel=\"noreferrer noopener\">single and double logical operators<\/a>!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Good spacing is crucial for readability. Make sure that the rules are the same and agreed upon in the team. It will make it easier to follow each other\u2019s code. The simplest solution is to stand on the shoulders of giants and follow the&nbsp;<a href=\"https:\/\/style.tidyverse.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">tidyverse style guide<\/a>.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, checking the style in every line during the review is quite inefficient, so make sure to introduce&nbsp;<strong>linter<\/strong>&nbsp;and&nbsp;<strong>styler<\/strong>&nbsp;in your development workflow, as presented in&nbsp;<a href=\"https:\/\/appsilon.com\/remote-data-science-team-best-practices-scrum-github-and-docker\/\" target=\"_blank\" rel=\"noreferrer noopener\">Olga\u2019s blog post<\/a>. This can be lifesaving! Recently we found an error in some legacy code that would have been automatically recognized by linter:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sum_of_values &lt;- first_element \n  + second_element<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This does not return the sum of the elements as the author was expecting.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Speaking of variable names \u2013 this is known to be one of the hardest things in programming. Thus avoid it when it is unnecessary. Note that R functions return by default the last created element, so you can easily replace that:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sum_elements &lt;- function(first, second) {\n  my_redundant_variable_name &lt;- sum(first, second)\n  return(my_redundant_variable_name)\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">With something shorter (and simpler, you don\u2019t need to think about names):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sum_elements &lt;- function(first, second) {\n  sum(first, second)\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">On the other hand, please DO use additional variables anytime you repeat some function call or calculation! It will make it computationally more effective and easier to be modified in the future. Remember to keep your code&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Don%27t_repeat_yourself\" target=\"_blank\" rel=\"noreferrer noopener\">DRY \u2013 don\u2019t repeat yourself<\/a>. If you copy-paste some code, think twice whether it shouldn\u2019t be saved to a variable, done in a loop, or moved to a function.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">And there you have it \u2013 five strategies to write clean R code and leave your code reviewer commentless. These five alone will ensure you\u2019re writing great-quality code that is easy to understand, even years down the road.&nbsp;Happy coding!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Visit <a href=\"\/campus\/category\/contributors\/appsilon\/\">Appsilon Data Science<\/a> for additional insight on this article: <a href=\"https:\/\/appsilon.com\/write-clean-r-code\/\">https:\/\/appsilon.com\/write-clean-r-code\/<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article showcases how clean and concise codebase is instrumental to achieve effective collaboration and provides insight on how to do so.<\/p>\n","protected":false},"author":602,"featured_media":80184,"comment_status":"closed","ping_status":"open","sticky":true,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[339,343,338,341,351,344,342],"tags":[806,487,6591,508,1045],"contributors-categories":[13652],"class_list":["post-80143","post","type-post","status-publish","format-standard","has-post-thumbnail","category-data-science","category-programing-languages","category-ibkr-quant-news","category-quant-development","category-quant-europe","category-quant-regions","category-r-development","tag-data-science","tag-r","tag-rstats","tag-rstudio","tag-tidyverse","contributors-categories-appsilon"],"pp_statuses_selecting_workflow":false,"pp_workflow_action":"current","pp_status_selection":"publish","acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.9 (Yoast SEO v28.1) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>5 Tips for Writing Clean R Code | IBKR Quant<\/title>\n<meta name=\"description\" content=\"This article showcases how clean and concise codebase is instrumental to achieve effective collaboration and provides insight on how to do so.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.interactivebrokers.com\/campus\/wp-json\/wp\/v2\/posts\/80143\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"5 Tips for Writing Clean R Code | IBKR Quant Blog\" \/>\n<meta property=\"og:description\" content=\"This article showcases how clean and concise codebase is instrumental to achieve effective collaboration and provides insight on how to do so.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/5-tips-for-writing-clean-r-code\/\" \/>\n<meta property=\"og:site_name\" content=\"IBKR Campus US\" \/>\n<meta property=\"article:published_time\" content=\"2021-03-22T14:20:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-11-21T14:47:15+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/03\/R-programing.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"900\" \/>\n\t<meta property=\"og:image:height\" content=\"550\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Marcin Dubel\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Marcin Dubel\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\n\t    \"@context\": \"https:\\\/\\\/schema.org\",\n\t    \"@graph\": [\n\t        {\n\t            \"@type\": \"NewsArticle\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/5-tips-for-writing-clean-r-code\\\/#article\",\n\t            \"isPartOf\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/5-tips-for-writing-clean-r-code\\\/\"\n\t            },\n\t            \"author\": {\n\t                \"name\": \"Marcin Dubel\",\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/person\\\/4e5a07260e384841e501db66c581c22e\"\n\t            },\n\t            \"headline\": \"5 Tips for Writing Clean R Code\",\n\t            \"datePublished\": \"2021-03-22T14:20:00+00:00\",\n\t            \"dateModified\": \"2022-11-21T14:47:15+00:00\",\n\t            \"mainEntityOfPage\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/5-tips-for-writing-clean-r-code\\\/\"\n\t            },\n\t            \"wordCount\": 1860,\n\t            \"publisher\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/5-tips-for-writing-clean-r-code\\\/#primaryimage\"\n\t            },\n\t            \"thumbnailUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2021\\\/03\\\/R-programing.jpg\",\n\t            \"keywords\": [\n\t                \"Data Science\",\n\t                \"R\",\n\t                \"rstats\",\n\t                \"RStudio\",\n\t                \"tidyverse\"\n\t            ],\n\t            \"articleSection\": [\n\t                \"Data Science\",\n\t                \"Programming Languages\",\n\t                \"Quant\",\n\t                \"Quant Development\",\n\t                \"Quant Europe\",\n\t                \"Quant Regions\",\n\t                \"R Development\"\n\t            ],\n\t            \"inLanguage\": \"en-US\"\n\t        },\n\t        {\n\t            \"@type\": \"WebPage\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/5-tips-for-writing-clean-r-code\\\/\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/5-tips-for-writing-clean-r-code\\\/\",\n\t            \"name\": \"5 Tips for Writing Clean R Code | IBKR Quant Blog\",\n\t            \"isPartOf\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#website\"\n\t            },\n\t            \"primaryImageOfPage\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/5-tips-for-writing-clean-r-code\\\/#primaryimage\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/5-tips-for-writing-clean-r-code\\\/#primaryimage\"\n\t            },\n\t            \"thumbnailUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2021\\\/03\\\/R-programing.jpg\",\n\t            \"datePublished\": \"2021-03-22T14:20:00+00:00\",\n\t            \"dateModified\": \"2022-11-21T14:47:15+00:00\",\n\t            \"description\": \"This article showcases how clean and concise codebase is instrumental to achieve effective collaboration and provides insight on how to do so.\",\n\t            \"inLanguage\": \"en-US\",\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"ReadAction\",\n\t                    \"target\": [\n\t                        \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/5-tips-for-writing-clean-r-code\\\/\"\n\t                    ]\n\t                }\n\t            ]\n\t        },\n\t        {\n\t            \"@type\": \"ImageObject\",\n\t            \"inLanguage\": \"en-US\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/5-tips-for-writing-clean-r-code\\\/#primaryimage\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2021\\\/03\\\/R-programing.jpg\",\n\t            \"contentUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2021\\\/03\\\/R-programing.jpg\",\n\t            \"width\": 900,\n\t            \"height\": 550,\n\t            \"caption\": \"R Programming\"\n\t        },\n\t        {\n\t            \"@type\": \"WebSite\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#website\",\n\t            \"url\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/\",\n\t            \"name\": \"IBKR Campus US\",\n\t            \"description\": \"Financial Education from Interactive Brokers\",\n\t            \"publisher\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\"\n\t            },\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"SearchAction\",\n\t                    \"target\": {\n\t                        \"@type\": \"EntryPoint\",\n\t                        \"urlTemplate\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/?s={search_term_string}\"\n\t                    },\n\t                    \"query-input\": {\n\t                        \"@type\": \"PropertyValueSpecification\",\n\t                        \"valueRequired\": true,\n\t                        \"valueName\": \"search_term_string\"\n\t                    }\n\t                }\n\t            ],\n\t            \"inLanguage\": \"en-US\"\n\t        },\n\t        {\n\t            \"@type\": \"Organization\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\",\n\t            \"name\": \"Interactive Brokers\",\n\t            \"alternateName\": \"IBKR\",\n\t            \"url\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/\",\n\t            \"logo\": {\n\t                \"@type\": \"ImageObject\",\n\t                \"inLanguage\": \"en-US\",\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/logo\\\/image\\\/\",\n\t                \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2024\\\/05\\\/ibkr-campus-logo.jpg\",\n\t                \"contentUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2024\\\/05\\\/ibkr-campus-logo.jpg\",\n\t                \"width\": 669,\n\t                \"height\": 669,\n\t                \"caption\": \"Interactive Brokers\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/logo\\\/image\\\/\"\n\t            },\n\t            \"publishingPrinciples\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/about-ibkr-campus\\\/\",\n\t            \"ethicsPolicy\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/cyber-security-notice\\\/\"\n\t        },\n\t        {\n\t            \"@type\": \"Person\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/person\\\/4e5a07260e384841e501db66c581c22e\",\n\t            \"name\": \"Marcin Dubel\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/author\\\/marcindubel\\\/\"\n\t        }\n\t    ]\n\t}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"5 Tips for Writing Clean R Code | IBKR Quant","description":"This article showcases how clean and concise codebase is instrumental to achieve effective collaboration and provides insight on how to do so.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.interactivebrokers.com\/campus\/wp-json\/wp\/v2\/posts\/80143\/","og_locale":"en_US","og_type":"article","og_title":"5 Tips for Writing Clean R Code | IBKR Quant Blog","og_description":"This article showcases how clean and concise codebase is instrumental to achieve effective collaboration and provides insight on how to do so.","og_url":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/5-tips-for-writing-clean-r-code\/","og_site_name":"IBKR Campus US","article_published_time":"2021-03-22T14:20:00+00:00","article_modified_time":"2022-11-21T14:47:15+00:00","og_image":[{"width":900,"height":550,"url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/03\/R-programing.jpg","type":"image\/jpeg"}],"author":"Marcin Dubel","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Marcin Dubel","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"NewsArticle","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/5-tips-for-writing-clean-r-code\/#article","isPartOf":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/5-tips-for-writing-clean-r-code\/"},"author":{"name":"Marcin Dubel","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/person\/4e5a07260e384841e501db66c581c22e"},"headline":"5 Tips for Writing Clean R Code","datePublished":"2021-03-22T14:20:00+00:00","dateModified":"2022-11-21T14:47:15+00:00","mainEntityOfPage":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/5-tips-for-writing-clean-r-code\/"},"wordCount":1860,"publisher":{"@id":"https:\/\/ibkrcampus.com\/campus\/#organization"},"image":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/5-tips-for-writing-clean-r-code\/#primaryimage"},"thumbnailUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/03\/R-programing.jpg","keywords":["Data Science","R","rstats","RStudio","tidyverse"],"articleSection":["Data Science","Programming Languages","Quant","Quant Development","Quant Europe","Quant Regions","R Development"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/5-tips-for-writing-clean-r-code\/","url":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/5-tips-for-writing-clean-r-code\/","name":"5 Tips for Writing Clean R Code | IBKR Quant Blog","isPartOf":{"@id":"https:\/\/ibkrcampus.com\/campus\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/5-tips-for-writing-clean-r-code\/#primaryimage"},"image":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/5-tips-for-writing-clean-r-code\/#primaryimage"},"thumbnailUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/03\/R-programing.jpg","datePublished":"2021-03-22T14:20:00+00:00","dateModified":"2022-11-21T14:47:15+00:00","description":"This article showcases how clean and concise codebase is instrumental to achieve effective collaboration and provides insight on how to do so.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/5-tips-for-writing-clean-r-code\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/5-tips-for-writing-clean-r-code\/#primaryimage","url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/03\/R-programing.jpg","contentUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/03\/R-programing.jpg","width":900,"height":550,"caption":"R Programming"},{"@type":"WebSite","@id":"https:\/\/ibkrcampus.com\/campus\/#website","url":"https:\/\/ibkrcampus.com\/campus\/","name":"IBKR Campus US","description":"Financial Education from Interactive Brokers","publisher":{"@id":"https:\/\/ibkrcampus.com\/campus\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ibkrcampus.com\/campus\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/ibkrcampus.com\/campus\/#organization","name":"Interactive Brokers","alternateName":"IBKR","url":"https:\/\/ibkrcampus.com\/campus\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/logo\/image\/","url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/05\/ibkr-campus-logo.jpg","contentUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/05\/ibkr-campus-logo.jpg","width":669,"height":669,"caption":"Interactive Brokers"},"image":{"@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/logo\/image\/"},"publishingPrinciples":"https:\/\/www.interactivebrokers.com\/campus\/about-ibkr-campus\/","ethicsPolicy":"https:\/\/www.interactivebrokers.com\/campus\/cyber-security-notice\/"},{"@type":"Person","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/person\/4e5a07260e384841e501db66c581c22e","name":"Marcin Dubel","url":"https:\/\/www.interactivebrokers.com\/campus\/author\/marcindubel\/"}]}},"jetpack_featured_media_url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/03\/R-programing.jpg","_links":{"self":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts\/80143","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/users\/602"}],"replies":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/comments?post=80143"}],"version-history":[{"count":0,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts\/80143\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/media\/80184"}],"wp:attachment":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/media?parent=80143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/categories?post=80143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/tags?post=80143"},{"taxonomy":"contributors-categories","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/contributors-categories?post=80143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}