Eno skriptno datoteko lahko preprosto vključite v druge RKWard vtičnike. Vrednost tega postane takoj očitna, če so nekateri deli vaše kode JS podobni med vtičniki. Te razdelke lahko preprosto definirate v ločeni datoteki .js in to vključite v vse datoteke .js vtičnika. Na primer kot v:
// this is a file called "common_functions.js"
function doCommonStuff () {
// perhaps fetch some options, etc.
// ...
comment ("This is R code you want in several different plugins\n");
// ...
}
// this is one of your regular plugin .js files
// include the common functions
include ("common_functions.js");
function calculate () {
// do something
// ...
// insert the common code
doCommonStuff ();
}
Upoštevajte, da je včasih še bolj uporabno to obrniti in definirati “skeleton” preprocess(), calculate() in funkcije printout() je običajna datoteka in naredi te povratne klice za tiste dele, ki se med vtičniki razlikujejo. Npr.:
// this is a file called "common_functions.js"
function calculate () {
// do some things which are the same in all plugins
// ...
// add in something that is different across plugins
getSpecifics ();
// ...
}
// this is one of your regular plugin .js files
// include the common functions
include ("common_functions.js");
// note: no calculate() function is defined in here.
// it in the common_functions.js, instead.
function getSpecifics () {
// print some R code
}
Ena težava, ki se je morate zavedati pri uporabi te tehnike, je obseg spremenljivk. Oglejte si priročnik JS o obsegih spremenljivk.
Ta tehnika se pogosto uporablja v vtičnikih za distribucijo CLT plot, zato boste morda želeli tam pogledati primere.