{"id":184466,"date":"2023-02-01T16:15:00","date_gmt":"2023-02-01T21:15:00","guid":{"rendered":"https:\/\/ibkrcampus.com\/traders-insight\/how-to-create-decorators-in-r\/"},"modified":"2023-02-13T16:10:59","modified_gmt":"2023-02-13T21:10:59","slug":"how-to-create-decorators-in-r","status":"publish","type":"post","link":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-create-decorators-in-r\/","title":{"rendered":"How to Create Decorators in R"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"h-introduction\"><strong>Introduction<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">One of the coolest features of&nbsp;<a href=\"https:\/\/theautomatic.net\/category\/python\/\">Python<\/a>&nbsp;is its nice ability to create decorators. In short, decorators allow us to modify how a function behaves without changing the function\u2019s source code. This can often make code cleaner and easier to modify. For instance, decorators are also really useful if you have a collection of functions where each function has similar repeating code. Fortunately, decorators can now also be created in R!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The first example below is in&nbsp;<a href=\"https:\/\/theautomatic.net\/category\/python\/\">Python<\/a>&nbsp;\u2013 we\u2019ll get to R in a moment. If you already know about decorators in Python, feel free to skip below to the R section.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Below we have a function that prints today\u2019s date. In our example, we create two functions. The first \u2013&nbsp;<em>print_start_end<\/em>&nbsp;takes another function as input. It prints \u201cStarting function call\u2026\u201d prior to calling this input function. Secondly, it calls the function. Lastly, it prints \u201cFinished function call\u2026\u201d.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The other function,&nbsp;<em>todays_date<\/em>, simply prints today\u2019s date.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import datetime\n \n# defining a decorator \ndef print_start_end(f): \n     \n    def wrapper(**args): \n        print(\"Starting function call...\") \n   \n        f() \n   \n        print(\"Finished function call...\") \n           \n    return wrapper \n      \ndef todays_date(): \n     print(datetime.datetime.today())<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now, let\u2019s say we want to call our&nbsp;<em>todays_date<\/em>&nbsp;function, but would like to wrap it inside the&nbsp;<em>print_start_end<\/em>&nbsp;function. One way to do that is by having a&nbsp;<em>nested<\/em>&nbsp;function call:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print_start_end(todays_date)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">However, we can also add a&nbsp;<em>decorator<\/em>&nbsp;to the&nbsp;<em>todays_date<\/em>&nbsp;like below. In Python, a decorator is created by adding an \u201c@\u201d followed by a function name (the \u201cdecorating\u201d function).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@print_start_end   \ndef todays_date(): \n     print(datetime.datetime.today())<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The result of running&nbsp;<em>todays_date()<\/em>&nbsp;now is below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2023\/02\/python-decorators-the-automatic-net.jpg\" alt=\" class=\" class=\"wp-image-175172 lazyload\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">We get the same result using a&nbsp;<em>decorator<\/em>&nbsp;as we did using a nested function call above. The nice part of this, however, is that we don\u2019t have to have any nested functions or change the code within&nbsp;<em>todays_date<\/em>. We can also easily comment out the decorator to turn off this functionality if we wanted to.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#@print_start_end   \ndef todays_date(): \n     print(datetime.datetime.today())<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">There\u2019s a whole lot more to decorators. To learn more, check out&nbsp;<a href=\"https:\/\/www.amazon.com\/gp\/product\/0692878971\/ref=as_li_qf_asin_il_tl?ie=UTF8&amp;tag=opensourceaut-20&amp;creative=9325&amp;linkCode=as2&amp;creativeASIN=0692878971&amp;linkId=fc211c8cef8d99e245500b6422f5a784\"><em>Powerful Python<\/em><\/a>, or&nbsp;<a href=\"https:\/\/www.amazon.com\/Guide-Learning-Python-Decorators-Guides\/dp\/1492325619\/ref=as_li_ss_tl?_encoding=UTF8&amp;qid=&amp;sr=&amp;linkCode=ll1&amp;tag=opensourceaut-20&amp;linkId=0d8e55fe24c496f84549256dcd97c67d&amp;language=en_US\"><em>Guide to Learning Python Decorators<\/em><\/a>. The second of these resources focuses solely on decorators, while the first one also covers several other Python features.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Decorators in R<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Now, let\u2019s walk through how to create decorators in R. We can do that using an awesome package called&nbsp;<strong>tinsel<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To get started, we first need to install&nbsp;<strong>tinsel<\/strong>, which requires&nbsp;<strong>devtools<\/strong>. If you don\u2019t have the&nbsp;<strong>devtools<\/strong>&nbsp;package installed, you\u2019ll need to install that first (<strong>install.packages(\u201cdevtools\u201d)<\/strong>). Then, you can install&nbsp;<strong>tinsel<\/strong>&nbsp;by running the command below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>devtools::install_github('nteetor\/tinsel')<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Once you\u2019re setup, you can start decorating functions. Let\u2019s recreate the example above using R. The first function is straightforward to reproduce \u2013 we\u2019re just changing Python syntax to R.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>library(tinsel)\n \nprint_start_end &lt;- function(f)\n{\n  wrapper &lt;- function(...)\n  {\n      print(\"Starting function call...\") \n   \n      f() \n   \n      print(\"Finished function call...\") \n   \n  }\n     \n  return(wrapper) \n   \n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now when it comes to the function we\u2019re decorating \u2013&nbsp;<em>todays_date<\/em>, we add our decorator using \u201c#.\u201d plus the name of our decorator function \u2013 in this case,&nbsp;<em>print_start_end<\/em>. Essentially, the main difference between creating a decorator here in R vs. Python is that we use \u201c#.\u201d rather than an \u201c@\u201d in the line above a function to signify that we want to decorate that function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#. print_start_end\ntodays_date &lt;- function()\n{\n    print(Sys.Date())\n   \n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In order for R to recognize that we want to use decorators, we need to source our R script using&nbsp;<strong>tinsel\u2019s<\/strong>&nbsp;<em>source_decoratees<\/em>&nbsp;function. Here we just need to pass the name of our script.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>source_decoratees(\"test_dec.R\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now when we call&nbsp;<em>todays_date<\/em>, the decorated version of the function gets executed.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2023\/02\/decorators-in-r-the-automatic-net.jpg\" alt=\" class=\" class=\"wp-image-175182 lazyload\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">As mentioned, one key use of decorators is to handle collections of functions that have similar repeating lines of code. For example, let\u2019s apply the&nbsp;<em>print_start_end<\/em>&nbsp;decorator above to two other functions.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#. print_start_end\nyesterday &lt;- function()\n{\n    print(Sys.Date() - 1)\n   \n}\n \n#. print_start_end\ntomorrow &lt;- function()\n{\n  print(Sys.Date() + 1)\n   \n}<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2023\/02\/decorate-functions-in-r-the-automatic-net.jpg\" alt=\" class=\" class=\"wp-image-175188 lazyload\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Timing functions with an R decorator<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Another nice feature of decorators is that if we make changes to the decorator, we can easily apply these to any of the functions being&nbsp;<em>decorated<\/em>. For example, below we can change our&nbsp;<em>print_start_end<\/em>&nbsp;function to time the execution of a function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print_start_end &lt;- function(f)\n{\n  wrapper &lt;- function(...)\n  {\n      start &lt;- proc.time() \n   \n      f() \n   \n      print(proc.time() - start) \n   \n  }\n     \n  return(wrapper) \n   \n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then, we can call our functions like below with our updated decorator. This allows us to make changes once rather than for each function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>yesterday()\n \ntomorrow()<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2023\/02\/change-decorator-r-the-automatic-net.jpg\" alt=\" class=\" class=\"wp-image-175189 lazyload\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s all for now!&nbsp;<a href=\"https:\/\/twitter.com\/AutomaticSource\">Please click to follow my blog on Twitter and keep up with my latest posts<\/a>, or check out some&nbsp;<a href=\"https:\/\/theautomatic.net\/resources\/\">additional resources<\/a>&nbsp;for learning Python and R by&nbsp;<a href=\"https:\/\/theautomatic.net\/resources\/\">clicking here<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Originally posted on <a href=\"https:\/\/theautomatic.net\/2020\/03\/17\/how-to-create-decorators-in-r\/\">TheAutomatic.net<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In short, decorators allow us to modify how a function behaves without changing the function\u2019s source code.<\/p>\n","protected":false},"author":388,"featured_media":184471,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[339,343,349,338,341,352,344,342],"tags":[806,595,487,6591,14547],"contributors-categories":[13695],"class_list":["post-184466","post","type-post","status-publish","format-standard","has-post-thumbnail","category-data-science","category-programing-languages","category-python-development","category-ibkr-quant-news","category-quant-development","category-quant-north-america","category-quant-regions","category-r-development","tag-data-science","tag-python","tag-r","tag-rstats","tag-tinsel-python-package","contributors-categories-theautomatic-net"],"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.5) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>How to Create Decorators in R | IBKR Quant<\/title>\n<meta name=\"description\" content=\"In short, decorators allow us to modify how a function behaves without changing the function\u2019s source code.\" \/>\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\/184466\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Create Decorators in R\" \/>\n<meta property=\"og:description\" content=\"In short, decorators allow us to modify how a function behaves without changing the function\u2019s source code.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-create-decorators-in-r\/\" \/>\n<meta property=\"og:site_name\" content=\"IBKR Campus US\" \/>\n<meta property=\"article:published_time\" content=\"2023-02-01T21:15:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-02-13T21:10:59+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/02\/circuts-energy.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=\"Andrew Treadway\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:title\" content=\"How to Create Decorators in R\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Andrew Treadway\" \/>\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\\\/how-to-create-decorators-in-r\\\/#article\",\n\t            \"isPartOf\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/how-to-create-decorators-in-r\\\/\"\n\t            },\n\t            \"author\": {\n\t                \"name\": \"Andrew Treadway\",\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/person\\\/d4018570a16fb867f1c08412fc9c64bc\"\n\t            },\n\t            \"headline\": \"How to Create Decorators in R\",\n\t            \"datePublished\": \"2023-02-01T21:15:00+00:00\",\n\t            \"dateModified\": \"2023-02-13T21:10:59+00:00\",\n\t            \"mainEntityOfPage\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/how-to-create-decorators-in-r\\\/\"\n\t            },\n\t            \"wordCount\": 749,\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\\\/how-to-create-decorators-in-r\\\/#primaryimage\"\n\t            },\n\t            \"thumbnailUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2023\\\/02\\\/circuts-energy.jpg\",\n\t            \"keywords\": [\n\t                \"Data Science\",\n\t                \"Python\",\n\t                \"R\",\n\t                \"rstats\",\n\t                \"tinsel Python package\"\n\t            ],\n\t            \"articleSection\": [\n\t                \"Data Science\",\n\t                \"Programming Languages\",\n\t                \"Python Development\",\n\t                \"Quant\",\n\t                \"Quant Development\",\n\t                \"Quant North America\",\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\\\/how-to-create-decorators-in-r\\\/\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/how-to-create-decorators-in-r\\\/\",\n\t            \"name\": \"How to Create Decorators in R\",\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\\\/how-to-create-decorators-in-r\\\/#primaryimage\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/how-to-create-decorators-in-r\\\/#primaryimage\"\n\t            },\n\t            \"thumbnailUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2023\\\/02\\\/circuts-energy.jpg\",\n\t            \"datePublished\": \"2023-02-01T21:15:00+00:00\",\n\t            \"dateModified\": \"2023-02-13T21:10:59+00:00\",\n\t            \"description\": \"In short, decorators allow us to modify how a function behaves without changing the function\u2019s source code.\",\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\\\/how-to-create-decorators-in-r\\\/\"\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\\\/how-to-create-decorators-in-r\\\/#primaryimage\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2023\\\/02\\\/circuts-energy.jpg\",\n\t            \"contentUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2023\\\/02\\\/circuts-energy.jpg\",\n\t            \"width\": 900,\n\t            \"height\": 550,\n\t            \"caption\": \"How to Create Decorators in R\"\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\\\/d4018570a16fb867f1c08412fc9c64bc\",\n\t            \"name\": \"Andrew Treadway\",\n\t            \"description\": \"Andrew Treadway currently works as a Senior Data Scientist, and has experience doing analytics, software automation, and ETL. He completed a master\u2019s degree in computer science \\\/ machine learning, and an undergraduate degree in pure mathematics. Connect with him on LinkedIn: https:\\\/\\\/www.linkedin.com\\\/in\\\/andrew-treadway-a3b19b103\\\/In addition to TheAutomatic.net blog, he also teaches in-person courses on Python and R through my NYC meetup: more details.\",\n\t            \"sameAs\": [\n\t                \"https:\\\/\\\/theautomatic.net\\\/about-me\\\/\"\n\t            ],\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/author\\\/andrewtreadway\\\/\"\n\t        }\n\t    ]\n\t}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How to Create Decorators in R | IBKR Quant","description":"In short, decorators allow us to modify how a function behaves without changing the function\u2019s source code.","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\/184466\/","og_locale":"en_US","og_type":"article","og_title":"How to Create Decorators in R","og_description":"In short, decorators allow us to modify how a function behaves without changing the function\u2019s source code.","og_url":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-create-decorators-in-r\/","og_site_name":"IBKR Campus US","article_published_time":"2023-02-01T21:15:00+00:00","article_modified_time":"2023-02-13T21:10:59+00:00","og_image":[{"width":900,"height":550,"url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/02\/circuts-energy.jpg","type":"image\/jpeg"}],"author":"Andrew Treadway","twitter_card":"summary_large_image","twitter_title":"How to Create Decorators in R","twitter_misc":{"Written by":"Andrew Treadway"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"NewsArticle","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-create-decorators-in-r\/#article","isPartOf":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-create-decorators-in-r\/"},"author":{"name":"Andrew Treadway","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/person\/d4018570a16fb867f1c08412fc9c64bc"},"headline":"How to Create Decorators in R","datePublished":"2023-02-01T21:15:00+00:00","dateModified":"2023-02-13T21:10:59+00:00","mainEntityOfPage":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-create-decorators-in-r\/"},"wordCount":749,"publisher":{"@id":"https:\/\/ibkrcampus.com\/campus\/#organization"},"image":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-create-decorators-in-r\/#primaryimage"},"thumbnailUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/02\/circuts-energy.jpg","keywords":["Data Science","Python","R","rstats","tinsel Python package"],"articleSection":["Data Science","Programming Languages","Python Development","Quant","Quant Development","Quant North America","Quant Regions","R Development"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-create-decorators-in-r\/","url":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-create-decorators-in-r\/","name":"How to Create Decorators in R","isPartOf":{"@id":"https:\/\/ibkrcampus.com\/campus\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-create-decorators-in-r\/#primaryimage"},"image":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-create-decorators-in-r\/#primaryimage"},"thumbnailUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/02\/circuts-energy.jpg","datePublished":"2023-02-01T21:15:00+00:00","dateModified":"2023-02-13T21:10:59+00:00","description":"In short, decorators allow us to modify how a function behaves without changing the function\u2019s source code.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-create-decorators-in-r\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-create-decorators-in-r\/#primaryimage","url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/02\/circuts-energy.jpg","contentUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/02\/circuts-energy.jpg","width":900,"height":550,"caption":"How to Create Decorators in R"},{"@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\/d4018570a16fb867f1c08412fc9c64bc","name":"Andrew Treadway","description":"Andrew Treadway currently works as a Senior Data Scientist, and has experience doing analytics, software automation, and ETL. He completed a master\u2019s degree in computer science \/ machine learning, and an undergraduate degree in pure mathematics. Connect with him on LinkedIn: https:\/\/www.linkedin.com\/in\/andrew-treadway-a3b19b103\/In addition to TheAutomatic.net blog, he also teaches in-person courses on Python and R through my NYC meetup: more details.","sameAs":["https:\/\/theautomatic.net\/about-me\/"],"url":"https:\/\/www.interactivebrokers.com\/campus\/author\/andrewtreadway\/"}]}},"jetpack_featured_media_url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/02\/circuts-energy.jpg","_links":{"self":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts\/184466","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\/388"}],"replies":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/comments?post=184466"}],"version-history":[{"count":0,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts\/184466\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/media\/184471"}],"wp:attachment":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/media?parent=184466"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/categories?post=184466"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/tags?post=184466"},{"taxonomy":"contributors-categories","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/contributors-categories?post=184466"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}