如何设置Shiny :: toJSON args来自自定义更新***输入功能?

时间:2015-05-25 18:59:14

标签: r shiny inputbinding

我正在为Shiny开发custom input binding。我有data.table我正在传递给javascript。不幸的是,数据需要按行而不是按列进行编码。 private$inputMessageQueueprocesssed,没有额外的参数,因此无法覆盖shiny::toJSON默认参数。我怎样才能获得我需要的格式?

接收我的data.table(作为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"}
] 

当处理私有$ 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?

1 个答案:

答案 0 :(得分:1)

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