Create a JavaScript bundle of an odin model

odin_js_bundle(code, include_support = TRUE)

Arguments

code

An expression, string or path to a file containing odin code (as for odin_parse_). If NULL, compile no model and return only the support code.

include_support

Logical, indicating if the support code should be included. Without this you need to manually copy over odin.js or dust.js depending on what model type you have.

Value

A list, with contents subject to change.

Warning

The interface and generated code here are subject to change. As it stands, it does what is needed for our work in odin.api and does not actually produce a useful bundle!

Examples

js <- odin::odin_js_bundle(quote({
  deriv(x) <- 1
  initial(x) <- 1
}), include_support = FALSE)
head(js$model$code, 20)
#>  [1] "class odin {"                                             
#>  [2] "  constructor(base, user, unusedUserAction) {"            
#>  [3] "    this.base = base;"                                    
#>  [4] "    this.internal = {};"                                  
#>  [5] "    var internal = this.internal;"                        
#>  [6] "    internal.initial_x = 1;"                              
#>  [7] "    this.setUser(user, unusedUserAction);"                
#>  [8] "  }"                                                      
#>  [9] "  initial(t) {"                                           
#> [10] "    var internal = this.internal;"                        
#> [11] "    var state = Array(1).fill(0);"                        
#> [12] "    state[0] = internal.initial_x;"                       
#> [13] "    return state;"                                        
#> [14] "  }"                                                      
#> [15] "  setUser(user, unusedUserAction) {"                      
#> [16] "    this.base.user.checkUser(user, [], unusedUserAction);"
#> [17] "    this.updateMetadata();"                               
#> [18] "  }"                                                      
#> [19] "  getInternal() {"                                        
#> [20] "    return this.internal;"