Skip to content

Commit

Permalink
Merge pull request #31 from yeedle/master
Browse files Browse the repository at this point in the history
use math.js to evaluate user's arith expressions
  • Loading branch information
tim-salabim authored Sep 25, 2020
2 parents 37cdb63 + 71fe16a commit 92deeba
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
4 changes: 3 additions & 1 deletion R/addGeoRaster.R
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ leafletGeoRasterDependencies = function() {
, "georaster-layer-for-leaflet.browserify.min.js"
, "georaster-binding.js"
, "georasterUtils.js"
, "mathjs.min.js"
)
)
)
Expand All @@ -372,7 +373,8 @@ bandCalc = function(f) {
idx_r = gregexpr("[0-9]+", band_calc)
js_bands = as.numeric(unlist(regmatches(band_calc, idx_r)))
js_bands = js_bands - min(js_bands)
js_bands = as.integer(rscl(js_bands, to = c(0, length(unique(js_bands)) - 1)))
js_bands = as.integer(rscl(js_bands, to = c(1, length(unique(js_bands)))))


js_band_calc = gsub("[0-9]+", "%s", band_calc)
js_band_calc = gsub(formalArgs(f), "values", js_band_calc)
Expand Down
5 changes: 5 additions & 0 deletions inst/htmlwidgets/lib/georaster-for-leaflet/georasterUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,13 @@ function evalDomain(arr, arith) {
return [Math.min(...out), Math.max(...out)];
}

/*
function evalMath(a, values) {
return Function('values', 'with(Math) return ' + a)(values);
} */

function evalMath(rawExpression, values) {
return safeEval(rawExpression, {values});
}


Expand Down
Loading

0 comments on commit 92deeba

Please sign in to comment.