1

我正在为 Shiny 开发一个自定义输入绑定。我有一个data.table要传递给javascript的。不幸的是,数据需要按行而不是按列进行编码。 private$inputMessageQueue在没有额外参数的情况下进行处理,因此没有地方覆盖shiny::toJSON默认参数。我怎样才能得到我需要的格式?

data.table接收我的(as message$data)的闪亮函数

debugging in: session$sendInputMessage(inputId, message)
debug: {
    data <- list(id = inputId, message = message)
    private$inputMessageQueue[[length(private$inputMessageQueue) + 
        1]] <- data
}

我的结构data.table

Browse[4]> str(message$data)
Classes ‘data.table’ and 'data.frame':  8 obs. of  7 variables:
 $ coOrdinate       : chr  "c1" "c2" "c3" "c4" ...
 $ injectionSite    : Factor w/ 2 levels "head","body": NA NA NA NA NA NA NA NA
 $ drug             : Factor w/ 2 levels "aconitin","nicotine": NA NA NA NA NA NA NA NA
 $ drugConcentration: num  NA NA NA NA NA NA NA NA
 $ bath             : Factor w/ 2 levels "aconitin","nicotine": NA NA NA NA NA NA NA NA
 $ bathConcentration: num  NA NA NA NA NA NA NA NA
 $ status           : Factor w/ 3 levels "active","dead",..: 1 1 1 1 1 1 1 1
 - attr(*, ".internal.selfref")=<externalptr> 

我的实际数据data.table

Browse[4]> message$data
   coOrdinate injectionSite drug drugConcentration bath bathConcentration status
1:         c1            NA   NA                NA   NA                NA active
2:         c2            NA   NA                NA   NA                NA active
3:         c3            NA   NA                NA   NA                NA active
4:         c4            NA   NA                NA   NA                NA active
5:         c5            NA   NA                NA   NA                NA active
6:         c6            NA   NA                NA   NA                NA active
7:         c7            NA   NA                NA   NA                NA active
8:         c8            NA   NA                NA   NA                NA active

我想让 Shiny 做什么:

Browse[4]> jsonlite::toJSON(message$data)

[
    {"coOrdinate":"c1","status":"active"},
    {"coOrdinate":"c2","status":"active"},
    {"coOrdinate":"c3","status":"active"},
    {"coOrdinate":"c4","status":"active"},
    {"coOrdinate":"c5","status":"active"},
    {"coOrdinate":"c6","status":"active"},
    {"coOrdinate":"c7","status":"active"},
    {"coOrdinate":"c8","status":"active"}
] 

当处理 private$InputMessageQueue 时 Shiny 做了什么(如shiny.R中所示)

jsonlite::toJSON(I(message$data),dataframe='columns',null='null', na='null', auto_unbox=T, force=T, rownames=F, keep_vec_name=T)

{
    "coOrdinate":["c1","c2","c3","c4","c5","c6","c7","c8"],
    "injectionSite":[null,null,null,null,null,null,null,null],
    "drug":[null,null,null,null,null,null,null,null],
    "drugConcentration":[null,null,null,null,null,null,null,null],
    "bath":[null,null,null,null,null,null,null,null],
    "bathConcentration":[null,null,null,null,null,null,null,null],
    "status":["active","active","active","active","active","active","active","active"]
}

如果一切都失败了,我可以将我的 data.table 转换为矩阵。这会给我按行的 JSON,但我会丢失列名:

Browse[4]> jsonlite::toJSON(I(as.matrix(message$data)),dataframe='columns',null='null', na='null', auto_unbox=T, force=T, rownames=F, keep_vec_name=T)

[
    ["c1",null,null,null,null,null,"active"],
    ["c2",null,null,null,null,null,"active"],
    ["c3",null,null,null,null,null,"active"],
    ["c4",null,null,null,null,null,"active"],
    ["c5",null,null,null,null,null,"active"],
    ["c6",null,null,null,null,null,"active"],
    ["c7",null,null,null,null,null,"active"],
    ["c8",null,null,null,null,null,"active"]
] 

那么,我怎样才能强制 Shiny 逐行传递我的 data.frame 呢?

4

1 回答 1

1

您可以简单地使用预编码 R 数据jsonlite::toJSON()并将编码的 JSON 字符串传递给shiny。它将被视为逐字 JSON 字符串,而不是被双重编码(例如"[1,2]",不会再次编码为"\"[1,2]\"")。这是因为我们json_verbatim = TRUEjsonlite::toJSON()( PR#795 ) 中使用了参数。这需要闪亮> = 0.12.0。

于 2015-05-25T19:48:18.113 回答