var k=["esm.sh","skypack","esm.run","jsdelivr.esm","fastly.jsdelivr.esm","gcore.jsdelivr.esm","testingcf.jsdelivr.esm","jsdelivr.b-cdn.esm","jspm"],C=["jsdelivr","fastly.jsdelivr","unpkg","gcore.jsdelivr","testingcf.jsdelivr","jsdelivr.b-cdn","npmcdn"],$=["jsdelivr.gh","fastly.jsdelivr.gh","statically","gcore.jsdelivr.gh","testingcf.jsdelivr.gh","jsdelivr.b-cdn.gh"],d={getModuleUrl:(s,{isModule:t=!0,defaultCDN:o="esm.sh",external:e}={})=>{s=s.replace(/#nobundle/g,"");let n=c=>!e||!c.includes("https://esm.sh")?c:c.includes("?")?`${c}&external=${e}`:`${c}?external=${e}`,i=w(s,t,o);return i?n(i):t?n("https://esm.sh/"+s):"https://cdn.jsdelivr.net/npm/"+s},getUrl:(s,t)=>s.startsWith("http")||s.startsWith("data:")?s:w(s,!1,t||S())||s,cdnLists:{npm:C,module:k,gh:$},checkCDNs:async(s,t)=>{let o=[t,...d.cdnLists.npm].filter(Boolean);for(let e of o)try{if((await fetch(d.getUrl(s,e),{method:"HEAD"})).ok)return e}catch{}return d.cdnLists.npm[0]}},S=()=>{if(globalThis.appCDN)return globalThis.appCDN;try{return new URL(location.href).searchParams.get("appCDN")||d.cdnLists.npm[0]}catch{return d.cdnLists.npm[0]}},w=(s,t,o)=>{let e=t&&s.startsWith("unpkg:")?"?module":"";s.startsWith("gh:")?s=s.replace("gh",$[0]):s.includes(":")||(s=(o||(t?k[0]:C[0]))+":"+s);for(let n of q){let[i,c]=n;if(i.test(s))return s.replace(i,c)+e}return null},q=[[/^(esm\.sh:)(.+)/i,"https://esm.sh/$2"],[/^(npm:)(.+)/i,"https://esm.sh/$2"],[/^(node:)(.+)/i,"https://esm.sh/$2"],[/^(jsr:)(.+)/i,"https://esm.sh/jsr/$2"],[/^(skypack:)(.+)/i,"https://cdn.skypack.dev/$2"],[/^(jsdelivr:)(.+)/i,"https://cdn.jsdelivr.net/npm/$2"],[/^(fastly\.jsdelivr:)(.+)/i,"https://fastly.jsdelivr.net/npm/$2"],[/^(gcore\.jsdelivr:)(.+)/i,"https://gcore.jsdelivr.net/npm/$2"],[/^(testingcf\.jsdelivr:)(.+)/i,"https://testingcf.jsdelivr.net/npm/$2"],[/^(jsdelivr\.b-cdn:)(.+)/i,"https://jsdelivr.b-cdn.net/npm/$2"],[/^(jsdelivr\.gh:)(.+)/i,"https://cdn.jsdelivr.net/gh/$2"],[/^(fastly\.jsdelivr\.gh:)(.+)/i,"https://fastly.jsdelivr.net/gh/$2"],[/^(gcore\.jsdelivr\.gh:)(.+)/i,"https://gcore.jsdelivr.net/gh/$2"],[/^(testingcf\.jsdelivr\.gh:)(.+)/i,"https://testingcf.jsdelivr.net/gh/$2"],[/^(jsdelivr\.b-cdn\.gh:)(.+)/i,"https://jsdelivr.b-cdn.net/gh/$2"],[/^(statically:)(.+)/i,"https://cdn.statically.io/gh/$2"],[/^(esm\.run:)(.+)/i,"https://esm.run/$2"],[/^(jsdelivr\.esm:)(.+)/i,"https://cdn.jsdelivr.net/npm/$2/+esm"],[/^(fastly\.jsdelivr\.esm:)(.+)/i,"https://fastly.jsdelivr.net/npm/$2/+esm"],[/^(gcore\.jsdelivr\.esm:)(.+)/i,"https://gcore.jsdelivr.net/npm/$2/+esm"],[/^(testingcf\.jsdelivr\.esm:)(.+)/i,"https://testingcf.jsdelivr.net/npm/$2/+esm"],[/^(jsdelivr\.b-cdn\.esm:)(.+)/i,"https://jsdelivr.b-cdn.net/npm/$2/+esm"],[/^(jspm:)(.+)/i,"https://jspm.dev/$2"],[/^(esbuild:)(.+)/i,"https://esbuild.vercel.app/$2"],[/^(bundle\.run:)(.+)/i,"https://bundle.run/$2"],[/^(unpkg:)(.+)/i,"https://unpkg.com/$2"],[/^(npmcdn:)(.+)/i,"https://npmcdn.com/$2"],[/^(bundlejs:)(.+)/i,"https://deno.bundlejs.com/?file&q=$2"],[/^(bundle:)(.+)/i,"https://deno.bundlejs.com/?file&q=$2"],[/^(deno:)(.+)/i,"https://deno.bundlejs.com/?file&q=https://deno.land/x/$2/mod.ts"],[/^(https:\/\/deno\.land\/.+)/i,"https://deno.bundlejs.com/?file&q=$1"],[/^(github:|https:\/\/github\.com\/)(.[^\/]+?)\/(.[^\/]+?)\/(?!releases\/)(?:(?:blob|raw)\/)?(.+?\/.+)/i,"https://deno.bundlejs.com/?file&q=https://cdn.jsdelivr.net/gh/$2/$3@$4"],[/^(gist\.github:)(.+?\/[0-9a-f]+\/raw\/(?:[0-9a-f]+\/)?.+)$/i,"https://gist.githack.com/$2"],[/^(gitlab:|https:\/\/gitlab\.com\/)([^\/]+.*\/[^\/]+)\/(?:raw|blob)\/(.+?)(?:\?.*)?$/i,"https://deno.bundlejs.com/?file&q=https://gl.githack.com/$2/raw/$3"],[/^(bitbucket:|https:\/\/bitbucket\.org\/)([^\/]+\/[^\/]+)\/(?:raw|src)\/(.+?)(?:\?.*)?$/i,"https://deno.bundlejs.com/?file&q=https://bb.githack.com/$2/raw/$3"],[/^(bitbucket:)snippets\/([^\/]+\/[^\/]+)\/revisions\/([^\/\#\?]+)(?:\?[^#]*)?(?:\#file-(.+?))$/i,"https://bb.githack.com/!api/2.0/snippets/$2/$3/files/$4"],[/^(bitbucket:)snippets\/([^\/]+\/[^\/\#\?]+)(?:\?[^#]*)?(?:\#file-(.+?))$/i,"https://bb.githack.com/!api/2.0/snippets/$2/HEAD/files/$3"],[/^(bitbucket:)\!api\/2.0\/snippets\/([^\/]+\/[^\/]+\/[^\/]+)\/files\/(.+?)(?:\?.*)?$/i,"https://bb.githack.com/!api/2.0/snippets/$2/files/$3"],[/^(api\.bitbucket:)2.0\/snippets\/([^\/]+\/[^\/]+\/[^\/]+)\/files\/(.+?)(?:\?.*)?$/i,"https://bb.githack.com/!api/2.0/snippets/$2/files/$3"],[/^(rawgit:)(.+?\/[0-9a-f]+\/raw\/(?:[0-9a-f]+\/)?.+)$/i,"https://gist.githack.com/$2"],[/^(rawgit:|https:\/\/raw\.githubusercontent\.com)(\/[^\/]+\/[^\/]+|[0-9A-Za-z-]+\/[0-9a-f]+\/raw)\/(.+)/i,"https://deno.bundlejs.com/?file&q=https://raw.githack.com/$2/$3"]];var{getUrl:M,getModuleUrl:P}=d;var j=M("webr@0.4.0/dist/");var D=/((?:(?:library)|(?:require))\s*?\(\s*?((?:".*?")|(?:'.*?')|(?:.*?))\s*?\))/g,T=s=>s.replace(/#.*$/gm,""),R=s=>[...T(s).matchAll(new RegExp(D))].map(t=>t[2].replace(/"/g,"").replace(/'/g,"").split(",")[0].trim()).filter(Boolean),f={canvasHeight:309,canvasWidth:500,canvasPointSize:12,canvasBackground:"transparent"};livecodes.r.packages=[];livecodes.r.run=livecodes.r.run||(async({code:s,container:t,canvasHeight:o,canvasWidth:e,canvasPointSize:n,canvasBackground:i,env:c}={})=>{parent.postMessage({type:"loading",payload:!0},"*"),livecodes.r.output="",livecodes.r.plots=[],t!==null&&livecodes.r.config?.container!==null&&(typeof t=="string"?t=document.querySelector(t):typeof livecodes.r.config?.container=="string"&&(t=document.querySelector(livecodes.r.config.container)),t=t||livecodes.r.config?.container||document.body),o=Number(o||livecodes.r.config?.canvasHeight||f.canvasHeight),e=Number(e||livecodes.r.config?.canvasWidth||f.canvasWidth),n=Number(n||livecodes.r.config?.canvasPointSize||f.canvasPointSize),i=String(i||livecodes.r.config?.canvasBackground||f.canvasBackground),s==null&&(s="",document.querySelectorAll('script[type="text/r"]').forEach(m=>s+=m.innerHTML+` `));let h=R(s).filter(p=>!livecodes.r.packages.includes(p));if(h.length>0){await b,console.log("Installing packages: "+h.join(", "));let p=h.map(m=>`webr::install("${m}") `).join("");await livecodes.r.run({code:p,container:null}),livecodes.r.packages=[...new Set([...livecodes.r.packages,...h])]}let u;if(s.trim()){await b;let{webR:p,webRCodeShelter:m}=livecodes.r,a=null,g=[];await p.init(),u=await m.captureR(s,{withAutoprint:!0,captureStreams:!0,captureConditions:!1,env:c||livecodes.r.config?.env||{},captureGraphics:{width:e,height:o,bg:i,pointsize:n}});try{await p.evalRVoid("dev.off()");let x=r=>u?.output.filter(l=>r==="all"||l.type===r).map(l=>l.data).join(` `)||"",B=x("all"),U=x("stdout"),v=x("stderr");if(v.trim()&&console.log(v),(u?.images||[]).forEach(r=>{a=document.createElement("canvas"),a.width=r.width,a.height=r.height,a.style.width=String(e)+"px",a.style.display="block",a.style.margin="auto",g.push(a),a.getContext("2d")?.drawImage(r,0,0,r.width,r.height)}),t&&typeof t!="string"){t.innerHTML="";let r=document.createElement("pre");if(/\S/.test(U)){let l=document.createElement("code");l.innerText=U,r.appendChild(l)}else r.style.visibility="hidden";t.appendChild(r);for(let l of g){let y=document.createElement("div");y.appendChild(l),t.appendChild(y)}}livecodes.r.output=B,livecodes.r.plots=g}finally{m.purge()}}return parent.postMessage({type:"loading",payload:!1},"*"),u});var b=(async()=>{parent.postMessage({type:"loading",payload:!0},"*");let s=o=>typeof SharedArrayBuffer<"u"?o.SharedArrayBuffer:o.PostMessage;await(async()=>{if(livecodes.r.ready){await livecodes.r.webR.init();return}console.log("Loading WebR...");let{WebR:o,ChannelType:e}=await import(j+"webr.mjs");livecodes.r.webR=new o({baseUrl:j,channelType:s(e)}),await livecodes.r.webR.init(),livecodes.r.webRCodeShelter=await new livecodes.r.webR.Shelter,livecodes.r.ready=!0,console.log("WebR loaded.")})(),parent.postMessage({type:"loading",payload:!1},"*")})();livecodes.r.loaded=b;