r - How to extract values from data that are imported using ReactiveFileReader?
问题描述
I load data using the reactiveFileReader
, and wanted to extract values like column names to use it in the tableHeader
function. However, I don't understand how can I use the data on its own without going through render*
function. Can someone assist?
reactive_api_data <- reactiveFileReader(
intervalMillis = 1000,
session = session,
filePath = "data/data.csv",
readFunc = read_csv
)
sketch <- htmltools::withTags(table(
tableHeader(c("date", "a", "b", "c", "d", "e", "f", "g")),
tableFooter(c("Total",0,0,0,0,0,0,0))
))
output$data_2019 <- DT::renderDataTable({
reactive_api_data() %>%
filter(date > "2019-01-01")
},
container = sketch,
rownames = FALSE,
options = list(paging = FALSE, searching = FALSE, lengthMenu = FALSE,
footerCallback = JS(
"function(row, data, start, end, display) {",
"var api = this.api(), data;",
"total = api.column(2).data().reduce( function(a, b) { return a + b }, 0);",
"$( api.column(2).footer() ).html(total);",
"}"
)
)
I would like to do something like
tableHeader(colnames(data))
instead of manually typing the column names, like this in my above code:
tableHeader(c("date", "a", "b", "c", "d", "e", "f", "g"))
解决方案
我将其更改sketch
为要在 renderDataTable 中调用的反应函数。
reactive_api_data <- reactiveFileReader(
intervalMillis = 1000,
session = session,
filePath = "data/data.csv",
readFunc = read_csv
)
sketch <- reactive({
htmltools::withTags(
table(
DT::tableHeader(colnames(reactive_api_data())),
DT::tableFooter(c("Total",0,0,0,0,0,0,0))
)
)
})
output$data_2019 <- DT::renderDataTable({
reactive_api_data() %>%
filter(date > "2019-01-01")
},
container = sketch(),
rownames = FALSE,
options = list(paging = FALSE, searching = FALSE, lengthMenu = FALSE,
footerCallback = JS(
"function(row, data, start, end, display) {",
"var api = this.api(), data;",
"total = api.column(2).data().reduce( function(a, b) { return a + b }, 0);",
"$( api.column(2).footer() ).html(total);",
"}"
)
)
推荐阅读
- traefik - Docker 提供程序和 url 上的服务器之间的 Traefik 负载平衡
- python - 如何用python计算当前的互联网连接速度?
- sql - 如何汇总 SQL 数据
- python - 为什么 Python webbrowser 模块不能正常工作?
- swift - 如何在 Swift 中获取应用程序的属性“具有脚本术语”
- oracle - 将不同的表更新记录到 LOG TABLE Oracle 中的 CLOB
- java - Spring @PostMapping 无法从提供的 JSON 创建实体
- php - [PHP][MySQL] PDO Search with multiple where doesent work
- javascript - 使用 JavaScript 从基于另一个下拉列表的 selectedIndex 的数组中动态选择选项
- shell - 如何获取 sftp 命令的输出以查找潜在错误