如何从自定义更新***输入函数设置 Shiny::toJSON 参数?
how to set Shiny::toJSON args from custom update***Input function?
我正在开发一个 custom input binding for Shiny. I have a data.table
that I am passing to a javascript. Unfortunately, the data needs to be encoded row-wise rather than column-wise. private$inputMessageQueue
is processsed without extra arguments so there is no place to overrideshiny::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"}
]
处理 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?
您可以简单地使用 jsonlite::toJSON()
对 R 数据进行预编码,然后将编码后的 JSON 字符串传递给 shiny。它将被视为逐字 JSON 字符串而不是双重编码(例如 "[1,2]"
将不会再次编码为 "\"[1,2]\""
)。这是因为我们在 jsonlite::toJSON()
(PR#795) 中使用了参数 json_verbatim = TRUE
。这需要 shiny >= 0.12.0.
我正在开发一个 custom input binding for Shiny. I have a data.table
that I am passing to a javascript. Unfortunately, the data needs to be encoded row-wise rather than column-wise. private$inputMessageQueue
is processsed without extra arguments so there is no place to overrideshiny::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"}
]
处理 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?
您可以简单地使用 jsonlite::toJSON()
对 R 数据进行预编码,然后将编码后的 JSON 字符串传递给 shiny。它将被视为逐字 JSON 字符串而不是双重编码(例如 "[1,2]"
将不会再次编码为 "\"[1,2]\""
)。这是因为我们在 jsonlite::toJSON()
(PR#795) 中使用了参数 json_verbatim = TRUE
。这需要 shiny >= 0.12.0.