我在here
的R中使用BoxPacking包虽然我已经在很多方面对它进行了操作,但基础是我无法将解决方案导出到txt或者最好是csv。我已尝试按照建议here转换为数据框或为txt编写行,但不断收到错误:
Error in as.data.frame.default(x[[i]], optional = TRUE) :
cannot coerce class "structure("Container", package = "BoxPacking")" to a data.frame
是否有人熟悉此套餐以获得帮助?
由于
答案 0 :(得分:1)
CSV似乎是一个糟糕的选择,因为它是一个嵌套结构。 JSON会更合适。
首先,为自定义对象定义一些json序列化程序:
library(jsonlite)
setMethod(jsonlite:::asJSON, "Box", function(x, ...) {
jsonlite:::asJSON(
list(
Box =
list(
origin = x@origin,
length = unbox(x@length),
height = unbox(x@height),
width = unbox(x@width)
)
),
...
)
})
setMethod(jsonlite:::asJSON, "EMS", function(x, ...) {
jsonlite:::asJSON(
list(
EMS = list(
origin = x@origin,
length = unbox(x@length),
height = unbox(x@height),
width = unbox(x@width)
)
),
...
)
})
setMethod(jsonlite:::asJSON, "Container", function(x, ...) {
jsonlite:::asJSON(
list(
Container =
list(
origin = x@origin,
length = unbox(x@length),
height = unbox(x@height),
width = unbox(x@width),
ems = x@ems
)
),
...
)
})
然后,只需在对象上调用jsonlite::toJSON()
:
[
[
{
"Container": {
"origin": [0, 0, 0],
"length": 2,
"height": 2,
"width": 2,
"ems": [
{
"EMS": {
"origin": [0, 0, 0],
"length": 2,
"height": 2,
"width": 2
}
}
]
}
}
],
[
{
"Container": {
"origin": [0, 0, 0],
"length": 2,
"height": 2,
"width": 2,
"ems": [
{
"EMS": {
"origin": [0, 0, 0],
"length": 2,
"height": 2,
"width": 2
}
}
]
}
}
],
[
{
"Container": {
"origin": [0, 0, 0],
"length": 2,
"height": 2,
"width": 2,
"ems": [
{
"EMS": {
"origin": [0, 0, 0],
"length": 2,
"height": 2,
"width": 2
}
}
]
}
}
],
[
{
"Container": {
"origin": [0, 0, 0],
"length": 2,
"height": 2,
"width": 2,
"ems": [
{
"EMS": {
"origin": [0, 1.9, 0],
"length": 2,
"height": 0.1,
"width": 2
}
},
{
"EMS": {
"origin": [0, 0, 1.9],
"length": 2,
"height": 2,
"width": 0.1
}
},
{
"EMS": {
"origin": [1.4, 0.9, 0],
"length": 0.6,
"height": 1.1,
"width": 0.4
}
},
{
"EMS": {
"origin": [0.4, 0.9, 0.4],
"length": 0.1,
"height": 0.5,
"width": 1.6
}
},
{
"EMS": {
"origin": [0, 1.8, 0],
"length": 0.4,
"height": 0.2,
"width": 0.4
}
},
{
"EMS": {
"origin": [0, 1.4, 1.5],
"length": 2,
"height": 0.6,
"width": 0.5
}
},
{
"EMS": {
"origin": [0, 1, 1.4],
"length": 0.4,
"height": 1,
"width": 0.6
}
},
{
"EMS": {
"origin": [0, 1, 1.5],
"length": 0.5,
"height": 1,
"width": 0.5
}
},
{
"EMS": {
"origin": [0, 1, 1.4],
"length": 0.5,
"height": 0.4,
"width": 0.6
}
},
{
"EMS": {
"origin": [0.9, 1.4, 0],
"length": 1.1,
"height": 0.6,
"width": 0.5
}
},
{
"EMS": {
"origin": [1.4, 1.4, 0],
"length": 0.6,
"height": 0.6,
"width": 2
}
},
{
"EMS": {
"origin": [1.5, 1.3, 0],
"length": 0.5,
"height": 0.7,
"width": 2
}
},
{
"EMS": {
"origin": [1, 1.3, 1.5],
"length": 1,
"height": 0.7,
"width": 0.5
}
},
{
"EMS": {
"origin": [1, 0, 1.4],
"length": 1,
"height": 0.9,
"width": 0.6
}
},
{
"EMS": {
"origin": [1.4, 1.3, 0.9],
"length": 0.6,
"height": 0.7,
"width": 1.1
}
},
{
"EMS": {
"origin": [1, 1.3, 0.9],
"length": 1,
"height": 0.1,
"width": 1.1
}
},
{
"EMS": {
"origin": [1.4, 0, 0.8],
"length": 0.6,
"height": 0.4,
"width": 0.1
}
},
{
"EMS": {
"origin": [1.5, 0, 0],
"length": 0.5,
"height": 2,
"width": 0.4
}
},
{
"EMS": {
"origin": [1.8, 0, 0],
"length": 0.2,
"height": 2,
"width": 0.9
}
},
{
"EMS": {
"origin": [1.5, 0, 0.8],
"length": 0.5,
"height": 2,
"width": 0.1
}
},
{
"EMS": {
"origin": [1.5, 0.4, 0],
"length": 0.5,
"height": 1.6,
"width": 0.9
}
},
{
"EMS": {
"origin": [1.8, 0, 0],
"length": 0.2,
"height": 0.5,
"width": 2
}
},
{
"EMS": {
"origin": [1.5, 0, 0.8],
"length": 0.5,
"height": 0.5,
"width": 1.2
}
},
{
"EMS": {
"origin": [1.5, 0.4, 0],
"length": 0.5,
"height": 0.1,
"width": 2
}
},
{
"EMS": {
"origin": [0.8, 0, 1.4],
"length": 1.2,
"height": 0.4,
"width": 0.6
}
},
{
"EMS": {
"origin": [0.4, 0, 1.8],
"length": 1.6,
"height": 0.4,
"width": 0.2
}
},
{
"EMS": {
"origin": [0.4, 0, 1.8],
"length": 0.1,
"height": 2,
"width": 0.2
}
},
{
"EMS": {
"origin": [0.4, 0.4, 1.5],
"length": 0.1,
"height": 1.6,
"width": 0.5
}
},
{
"EMS": {
"origin": [0.4, 0.4, 1.4],
"length": 0.1,
"height": 1,
"width": 0.6
}
}
]
}
},
{
"Box": {
"origin": [0, 0, 0],
"length": 1,
"height": 1,
"width": 0.4
}
},
{
"Box": {
"origin": [0, 0, 0.4],
"length": 1,
"height": 0.4,
"width": 1
}
},
{
"Box": {
"origin": [0, 0.4, 0.4],
"length": 0.5,
"height": 0.5,
"width": 1
}
},
{
"Box": {
"origin": [0.5, 0.4, 0.4],
"length": 1,
"height": 1,
"width": 0.5
}
},
{
"Box": {
"origin": [0, 0.9, 0.4],
"length": 0.4,
"height": 1,
"width": 1
}
},
{
"Box": {
"origin": [0.5, 0.4, 0.9],
"length": 0.5,
"height": 1,
"width": 1
}
},
{
"Box": {
"origin": [1, 0, 0],
"length": 0.5,
"height": 0.4,
"width": 0.4
}
},
{
"Box": {
"origin": [1, 0, 0.9],
"length": 0.5,
"height": 0.5,
"width": 0.5
}
},
{
"Box": {
"origin": [0, 1, 0],
"length": 1,
"height": 0.4,
"width": 0.4
}
},
{
"Box": {
"origin": [1, 0.4, 0],
"length": 0.5,
"height": 0.5,
"width": 0.4
}
},
{
"Box": {
"origin": [1, 0, 0.4],
"length": 0.4,
"height": 0.4,
"width": 0.5
}
},
{
"Box": {
"origin": [0, 0, 1.4],
"length": 0.4,
"height": 1,
"width": 0.5
}
},
{
"Box": {
"origin": [1, 0.9, 0],
"length": 0.4,
"height": 0.5,
"width": 0.4
}
},
{
"Box": {
"origin": [1, 0.5, 0.9],
"length": 1,
"height": 0.4,
"width": 0.5
}
},
{
"Box": {
"origin": [0, 1.4, 0],
"length": 0.4,
"height": 0.4,
"width": 0.4
}
},
{
"Box": {
"origin": [0.4, 1.4, 0],
"length": 0.5,
"height": 0.5,
"width": 0.5
}
},
{
"Box": {
"origin": [0.4, 1.4, 0.5],
"length": 1,
"height": 0.5,
"width": 1
}
},
{
"Box": {
"origin": [1, 0.9, 0.9],
"length": 1,
"height": 0.4,
"width": 1
}
},
{
"Box": {
"origin": [1.4, 0, 0.4],
"length": 0.4,
"height": 0.4,
"width": 0.4
}
},
{
"Box": {
"origin": [0.4, 0, 1.4],
"length": 0.4,
"height": 0.4,
"width": 0.4
}
}
]
]