### JSONP AJAX Prefilter and Setup Source: https://github.com/annelyng/rtsa/blob/master/vignettes/RSTA-SOP.html Configures jQuery's AJAX to support JSONP requests, dynamically setting the JSONP callback function name and handling the conversion of script responses to JSON. It also manages the cleanup of the global callback. ```javascript var _t, zt = [], Ut = /=(=)\?(?=&|$) |\?\? /; S.ajaxSetup({ jsonp: "callback", jsonpCallback: function() { var e = zt.pop() || S.expando + "_" + wt.guid++; return this[e] = !0, e } }), S.ajaxPrefilter("json jsonp", function(e, t, n) { var r, i, o, a = !1 !== e.jsonp && (Ut.test(e.url) ? "url" : "string" == typeof e.data && 0 === (e.contentType || "").indexOf("application/x-www-form-urlencoded") && Ut.test(e.data) && "data"); if (a || "jsonp" === e.dataTypes[0]) return r = e.jsonpCallback = m(e.jsonpCallback) ? e.jsonpCallback() : e.jsonpCallback, a ? e[a] = e[a].replace(Ut, "$1" + r) : !1 !== e.jsonp && (e.url += (Tt.test(e.url) ? "&" : "?") + e.jsonp + "=" + r), e.converters["script json"] = function() { return o || S.error(r + " was not called"), o[0] }, e.dataTypes[0] = "json", i = C[r], C[r] = function() { o = arguments }, n.always(function() { void 0 === i ? S(C).removeProp(r) : C[r] = i, e[r] && (e.jsonpCallback = t.jsonpCallback, zt.push(r)), o && m(i) && i(o[0]), o = i = void 0 }), "script" }) ``` -------------------------------- ### Install RTSA Package from CRAN (R) Source: https://github.com/annelyng/rtsa/blob/master/README.md This snippet demonstrates how to install the released version of the RTSA package directly from CRAN using the install.packages() function in R. Ensure you have a stable internet connection and R installed. ```r install.packages("RTSA") ``` -------------------------------- ### Check RTSA Package Version in R Source: https://github.com/annelyng/rtsa/blob/master/vignettes/RSTA-SOP.html This code snippet demonstrates how to load the RTSA package in R and retrieve its currently installed version. It's crucial for reproducibility to note the software version used for the analysis. ```r library(RTSA) packageVersion("RTSA") #> [1] '0.2.0' ``` -------------------------------- ### Setting and Getting Element Offsets with jQuery Source: https://github.com/annelyng/rtsa/blob/master/vignettes/RSTA-SOP.html Provides methods for retrieving an element's position relative to the document (`offset`) and its position relative to its offset parent (`position`). It also includes functionality to set an element's position. ```javascript S.offset = { setOffset: function(e, t, n) { var r, i, o, a, s, u, l = S.css(e, "position"), c = S(e), f = {}; "static" === l && (e.style.position = "relative"), s = c.offset(), o = S.css(e, "top"), u = S.css(e, "left"), ("absolute" === l || "fixed" === l) && -1 < (o + u).indexOf("auto") ? (a = (r = c.position()).top, i = r.left) : (a = parseFloat(o) || 0, i = parseFloat(u) || 0), m(t) && (t = t.call(e, n, S.extend({}, s))), null != t.top && (f.top = t.top - s.top + a), null != t.left && (f.left = t.left - s.left + i), "using" in t ? t.using.call(e, f) : c.css(f) } }, S.fn.extend({ offset: function(t) { if (arguments.length) return void 0 === t ? this : this.each(function(e) { S.offset.setOffset(this, t, e) }); var e, n, r = this[0]; return r ? r.getClientRects().length ? (e = r.getBoundingClientRect(), n = r.ownerDocument.defaultView, { top: e.top + n.pageYOffset, left: e.left + n.pageXOffset }) : { top: 0, left: 0 } : void 0 }, position: function() { if (this[0]) { var e, t, n, r = this[0], i = {top: 0, left: 0}; if ("fixed" === S.css(r, "position")) t = r.getBoundingClientRect(); else { t = this.offset(), n = r.ownerDocument, e = r.offsetParent || n.documentElement; while (e && (e === n.body || e === n.documentElement) && "static" === S.css(e, "position")) e = e.parentNode; e && e !== r && 1 === e.nodeType && ((i = S(e).offset()).top += S.css(e, "borderTopWidth", !0), i.left += S.css(e, "borderLeftWidth", !0)) } return { top: t.top - i.top - S.css(r, "marginTop", !0), left: t.left - i.left - S.css(r, "marginLeft", !0) } } }, offsetParent: function() { return this.map(function() { var e = this.offsetParent; while (e && "static" === S.css(e, "position")) e = e.offsetParent; return e || re }) } }) ``` -------------------------------- ### Calculate Futility Boundaries in RTSA (R) Source: https://github.com/annelyng/rtsa/blob/master/vignettes/futility.html This R code demonstrates how to calculate different types of futility boundaries using the `boundaries()` function from the `RTSA` package. It includes examples for one-sided and two-sided designs, with and without futility, and shows the calculation for both 'none' and 'binding' futility types. ```r library(RTSA) bound_none <- boundaries(timing = c(0.5,0.75, 1), alpha = 0.025, beta = 0.2, side = 1, futility = "none", es_alpha = "esOF") bound_bind <- boundaries(timing = c(0.5,0.75, 1), alpha = 0.025, beta = 0.2, side = 1, futility = "binding", es_alpha = "esOF", es_beta = "esOF") bound_none2 <- boundaries(timing = c(0.5,0.75, 1), alpha = 0.05, beta = 0.2, side = 2, futility = "none", es_alpha = "esOF") bound_bind2 <- boundaries(timing = c(0.5,0.75, 1), alpha = 0.05, beta = 0.2, side = 2, futility = "binding", es_alpha = "esOF", es_beta = "esOF") ``` -------------------------------- ### Highlight.js Initialization for Code Syntax Highlighting Source: https://github.com/annelyng/rtsa/blob/master/vignettes/RSTA-SOP.html This code snippet initializes the highlight.js library for syntax highlighting on the page. It configures the languages to be highlighted and then triggers the highlighting process on page load. It includes a fallback for 'complete' document ready state. ```javascript if (window.hljs) { hljs.configure({languages: []}); hljs.initHighlightingOnLoad(); if (document.readyState && document.readyState === "complete") { window.setTimeout(function() { hljs.initHighlighting(); }, 0); } } ``` -------------------------------- ### Scrolling Element Position with jQuery Source: https://github.com/annelyng/rtsa/blob/master/vignettes/RSTA-SOP.html Provides methods to get and set the horizontal (`scrollLeft`) and vertical (`scrollTop`) scroll positions of elements. It handles different browser implementations for accessing scroll offsets. ```javascript S.each(S.extend({ scrollLeft: "pageXOffset", scrollTop: "pageYOffset" }, function(t, i) { var o = "pageYOffset" === i; S.fn[t] = function(e) { return $(this, function(e, t, n) { var r; if (x(e) ? r = e : 9 === e.nodeType && (r = e.defaultView), void 0 === n) return r ? r[i] : e[t]; r ? r.scrollTo(o ? r.pageXOffset : n, o ? n : r.pageYOffset) : e[t] = n }, t, e, arguments.length) } })) ``` -------------------------------- ### File I/O Operations (Python) Source: https://github.com/annelyng/rtsa/blob/master/vignettes/RSTA-SOP.html This Python snippet demonstrates how to read from and write to files. It includes basic text file handling using context managers for safe file operations. ```python # Writing to a file file_path_write = "output.txt" content_to_write = "This is line 1.\nThis is line 2.\nHello from RTSA!" try: with open(file_path_write, "w") as file: file.write(content_to_write) print(f"Successfully wrote to {file_path_write}") except IOError as e: print(f"Error writing to file: {e}") # Reading from a file file_path_read = "output.txt" try: with open(file_path_read, "r") as file: read_content = file.read() print(f"\nContent read from {file_path_read}:") print(read_content) except FileNotFoundError: print(f"Error: The file {file_path_read} was not found.") except IOError as e: print(f"Error reading from file: {e}") # Reading line by line try: with open(file_path_read, "r") as file: print(f"\nReading {file_path_read} line by line:") for line in file: print(f" {line.strip()}") # .strip() removes leading/trailing whitespace including newline except FileNotFoundError: print(f"Error: The file {file_path_read} was not found.") except IOError as e: print(f"Error reading from file: {e}") ``` -------------------------------- ### Creating and Using jQuery Deferred Objects Source: https://github.com/annelyng/rtsa/blob/master/vignettes/RSTA-SOP.html Demonstrates the creation of a Deferred object and the usage of its methods like 'resolve', 'reject', and 'notify' to control its state. It also shows how to attach callbacks using 'done', 'fail', and 'always'. ```javascript var dfd = S.Deferred(); dfd.done(function( value ) { console.log( "Success: " + value ); }).fail(function( value ) { console.log( "Error: " + value ); }).progress(function( value ) { console.log( "Progress: " + value ); }); // To resolve the deferred object: dfd.resolve( "Data" ); // To reject the deferred object: dfd.reject( "Error Data" ); // To notify progress: dfd.notify( "Progress Data" ); ``` -------------------------------- ### Get Event Handlers in JavaScript Source: https://github.com/annelyng/rtsa/blob/master/vignettes/futility.html The `handlers` function retrieves the list of event handlers associated with a specific element and event type. It considers delegated events and ensures that only applicable handlers are returned, preparing them for dispatch. ```javascript handlers: function(e, t) { var n, r, i, o, a, s = [], u = t.delegateCount, l = e.target; if (u && l.nodeType && ("click" === e.type && 1 <= e.button)) ; else for (; l !== this; l = l.parentNode || this) if (1 === l.nodeType && ("click" !== e.type || !0 !== l.disabled)) { for (o = [], a = {}, n = 0; n < u; n++) void 0 === a[i = (r = t[n]).selector + " "] && (a[i] = r.needsCont ``` -------------------------------- ### Initialize Tabsets and Load MathJax (JavaScript) Source: https://github.com/annelyng/rtsa/blob/master/vignettes/RSTA-SOP.html This JavaScript code initializes tabset functionality for navigation and asynchronously loads the MathJax library. The `buildTabsets("TOC")` function likely sets up interactive tabbed content based on an element with the ID 'TOC'. The MathJax script is dynamically added to the document's head, enabling the rendering of mathematical equations in LaTeX or MathML format. This is crucial for displaying scientific and mathematical content. ```javascript $(document).ready(function () { window.buildTabsets("TOC"); }); $(document).ready(function () { $('.tabset-dropdown > .nav-tabs > li').click(function () { $(this).parent().toggleClass('nav-tabs-open'); }); }); (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); })(); ``` -------------------------------- ### JavaScript: Build Tabsets from Markdown Sections Source: https://github.com/annelyng/rtsa/blob/master/vignettes/futility.html The `window.buildTabsets` function converts `div.section.tabset` elements into Bootstrap-compatible tab interfaces. It handles options like fading and pills, determines heading levels, and dynamically creates navigation lists and tab content panes. It also removes corresponding entries from a specified table of contents. ```javascript window.buildTabsets = function(tocID) { function buildTabset(tabset) { var fade = tabset.hasClass("tabset-fade"); var pills = tabset.hasClass("tabset-pills"); var navClass = pills ? "nav-pills" : "nav-tabs"; var match = tabset.attr('class').match(/level(\d) /); if (match === null) return; var tabsetLevel = Number(match[1]); var tabLevel = tabsetLevel + 1; var tabs = tabset.find("div.section.level" + tabLevel); if (!tabs.length) return; var tabList = $(''); $(tabs[0]).before(tabList); var tabContent = $('
'); $(tabs[0]).before(tabContent); var activeTab = 0; tabs.each(function(i) { var tab = $(tabs[i]); var id = tab.attr('id'); if (tab.hasClass('active')) activeTab = i; $("div#" + tocID + " li a[href='#" + id + "']").parent().remove(); id = id.replace(/[\.\/?&!#<>]/g, '').replace(/\s/g, '_'); tab.attr('id', id); var heading = tab.find('h' + tabLevel + ':first'); var headingText = heading.html(); heading.remove(); var a = $('' + headingText + ''); a.attr('href', '#' + id); a.attr('aria-controls', id); var li = $('
  • '); li.append(a); tabList.append(li); tab.attr('role', 'tabpanel'); tab.addClass('tab-pane'); tab.addClass('tabbed-pane'); if (fade) tab.addClass('fade'); tab.detach().appendTo(tabContent); }); $(tabList.children('li')[activeTab]).addClass('active'); var active = $(tabContent.children('div.section')[activeTab]); active.addClass('active'); if (fade) active.addClass('in'); if (tabset.hasClass("tabset-sticky")) tabset.rmarkdownStickyTabs(); } var tabsets = $("div.section.tabset"); tabsets.each(function(i) { buildTabset($(tabsets[i])); }); }; ``` -------------------------------- ### AJAX Script Transport and Prefilter Source: https://github.com/annelyng/rtsa/blob/master/vignettes/RSTA-SOP.html Configures jQuery's AJAX to handle script transport, enabling GET requests for cross-domain script loading and setting cache to false by default. It also prefilters script requests to manage cross-domain scenarios. ```javascript S.ajaxPrefilter(function(e){ e.crossDomain && (e.contents.script = !1) }), S.ajaxSetup({ accepts: { script: "text/javascript, application/javascript, application/ecmascript, application/x-ecmascript" }, contents: { script: /\b(?:java|ecma)script\b/ }, converters: { "text script": function(e) { return S.globalEval(e), e } } }), S.ajaxPrefilter("script", function(e) { void 0 === e.cache && (e.cache = !1), e.crossDomain && (e.type = "GET") }), S.ajaxTransport("script", function(n) { var r, i if (n.crossDomain || n.scriptAttrs) return { send: function(e, t) { r = S("