【发布时间】:2020-02-14 15:35:07
【问题描述】:
我正在努力学习如何将 Google Charts 集成到 shiny 应用程序中,而无需使用 googlevis 包的额外开销并将大多数东西自己烘焙到应用程序中。下面是一个简单的 Hello World 示例,目前,我希望在应用程序中正确包含我想要的元素。
我正在尝试学习如何使用对象tmp() 中创建的r 数据来填充我正在创建的数据表。下面的应用程序可以运行,但图表目前是由硬编码在 data 对象中的数据填充的。
最小的可重现 ui 文件
ui <- fluidPage(
titlePanel("Hello Google Charts"),
sidebarLayout(
sidebarPanel(
h1('Hello World')
),
mainPanel(
htmlTemplate("test3.html"),
verbatimTextOutput('x')
)
)
)
最小的可重现服务器文件
server <- function(input, output) {
tmp <- data.frame(v1 = rnorm(4), v2 = rnorm(4))
output$x <- renderPrint({tmp})
}
HTML 文件 test3.html
<html>
<head>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
google.charts.load('current', {'packages':['gauge']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['v1', 'v2'],
['1', -1.9372140],
['2', -1.5234370],
['3', 0.2374601],
['4', 1.0550744]
]);
var options = {
width: 400, height: 120,
redFrom: 75, redTo: 100,
yellowFrom:50, yellowTo: 75,
minorTicks: 10
};
var chart = new google.visualization.Gauge(document.getElementById('chart_div'));
chart.draw(data, options);
setInterval(function() {
data.setValue(0, 1, 40 + Math.round(60 * Math.random()));
chart.draw(data, options);
}, 10000);
setInterval(function() {
data.setValue(1, 1, 40 + Math.round(60 * Math.random()));
chart.draw(data, options);
}, 100);
setInterval(function() {
data.setValue(2, 1, 10 );
chart.draw(data, options);
}, 100);
}
</script>
</head>
<body>
<div id="chart_div" style="width: 400px; height: 120px;">
</div>
</body>
</html>
【问题讨论】:
标签: shiny googlevis r r dataframe javascript r json shiny googlevis