我有一个下面提到的 native JavaScript 函数,我想在 R 中运行它。
JavaScript 函数:
$('.btn')
.click(function () {
var from = parseInt($('.from')[0].value)
, to = parseInt($('.to')[0].value)
, newData = [];
if (!isNaN(from) && !isNaN(to)) {
Highcharts.each(data, function (p) {
if (p.value >= from && p.value <= to) {
newData.push(p);
}
});
}
})
我想在我的主 R 脚本中绑定(bind)这个函数,我正在使用 Shiny 创建 Highcharts 树形图函数。
R Highchart Treemap 主要功能:
hc<-highchart() %>%
hc_title(text = "Costcenter Deviation between Quarters (A-B)") %>%
#hc_subtitle(text="Q1 and Q2 is default selection")%>%
hc_colorAxis( stops = colstops, endOnTick=FALSE,startOnTick=FALSE) %>%
#hc_colorAxis( minColor = '#1a9641', maxColor= '#a6d96a') %>%
hc_add_series(
type = "treemap",
layoutAlgorithm = "squarified",
allowDrillToNode = T,
dataLabels= list(enabled= F), levelIsConstant= F,
levels =list(
list(
level = 1,
dataLabels = list(
enabled = TRUE
),
borderWidth = 3, borderColor= 'black'
)
),
data= list_data
) %>%
hc_tooltip(pointFormat = "<b>{point.name}</b>
<br>EUR: {point.actualvalue:,.2f}</br> <br> Change:{point.change}% </br>",borderWidth=1, shared= F, followPointer=F , enabled=T, borderRadius=5) %>%
hc_plotOptions(borderWidth=15,borderColor='#fdae61', animationLimit=1000,turboThreshold=5000)
hc
那么,有什么可能的方法来实现这一目标吗?
最佳答案
由于您没有提供完全可重现的数据示例,因此我无法提供太多帮助。不过,我已经发布了一些示例代码,希望可以帮助您入门。
library("highcharter")
library("treemap")
library("viridis")
library("htmlwidgets")
data(GNI2014)
head(GNI2014)
tm <- treemap(GNI2014, index = c("continent", "iso3"),
vSize = "population", vColor = "GNI",
type = "comp", palette = rev(viridis(6)),
draw = FALSE)
htm <- highchart(height = 800) %>%
hc_add_series_treemap(tm, allowDrillToNode = TRUE,
layoutAlgorithm = "squarified",
name = "tmdata") %>%
hc_title(text = "Gross National Income World Data") %>%
hc_tooltip(pointFormat = "<b>{point.name}</b>:<br>
Pop: {point.value:,.0f}<br>
GNI: {point.valuecolor:,.0f}")
onRender(
htm,
"
function(el,x){
// el will be the dom element container for our htmlwidget
// x will be the data and options
// uncomment the below line if you want to break in
// in your browser devtools
// debugger;
$('.btn').click(function () {
var from = parseInt($('.from')[0].value),
to = parseInt($('.to')[0].value),
newData = [];
if (!isNaN(from) && !isNaN(to)) {
Highcharts.each(data, function (p) {
if (p.value >= from && p.value <= to) {
newData.push(p);
}
});
}
})
}
"
)
关于javascript - 如何在 R 中运行原生 JavaScript 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39614355/