我需要从动态集合中获取数据,例如,如果type = category,然后从类别集合中获取数据,如果type = banner,则从banner中获取数据。
输入数据数组:
IP
预期的输出数组:
[
{
"title": "New Product",
"row_data": [
{
"type": "category",
"data": [
5cf4edcdc70d4716d821d45d,5cf4ee36c70d4716d821d460
]
},
{
"type": "banner",
"data": [
5ce4eb55b02bd01ca09eb909
]
},
{
"type": "product",
"data": [
5cf4eed8c70d4716d821d465,5cf4fa09c70d4716d821d483
]
}
],
"_id": "5cf611c7fcc98b16b0e89200"
}
]
答案 0 :(得分:1)
最好的办法是使用$facet将其拆分为几个不同的聚合,然后将它们“合并”回所需的结构。
看起来有点像这样:
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(selectInput("x","Value of x",choices = c("ComA","ComB")),
checkboxInput("mean","Mean Prices are"),
tableOutput("mean")),
mainPanel(h6("Here it is"),
tableOutput("message")
)
)
)
server <- function(input, output, session) {
data <- reactive({
if (input$x == "ComA") {
data.frame(Unit_Price, Material)
} else if (input$x == "ComB") {
data.frame(Material=c("A","B"), Unit_Price=c(7,8))
}
})
output$message <- renderTable({
data()
})
output$mean <- renderTable(rownames = TRUE, {
req(input$mean)
df <- data()
tapply(df$Unit_Price, df$Material, mean)
})
}
shinyApp(ui, server)
请注意,我试图使这种聚合更具可读性,这意味着它效率不高。