从闪亮的数据框中动态渲染框和内容
Render box and contents dynamically from dataframe in shiny
我正在处理仪表板,我想使用数据框来生成框和描述。我可以使用 lapply 来制作框,但我不知道如何从数据框中提取描述。到目前为止我有(没有描述):
library(shiny)
library(shinydashboard)
dataset <- data.frame("title" = c("A","B","C"), "description" = c("Info about box A", "Info about box B","Info about box C"), "data" = c(1:3))
ui <- fluidPage(
titlePanel("Dynamic Boxes"),
fluidRow(
uiOutput("boxes")
)
)
dataset <- data.frame("title" = c("A","B","C"), "description" = c("Stuff about box A", "Stuff about box B","Stuff about box C"), "data" = c(1:3))
server <- function(input, output) {
output$boxes <- renderUI({
lapply(dataset[,'title'], function(a) {
box(title = a, p("say stuff here"))
})
})
}
我想不出引入描述的正确逻辑。
我试过 mapply:
server <- function(input, output) {
output$boxes <- renderUI({
mapply(function(x,y) {
box(title = x, p(y)
)
}, x = dataset[,'title'], y = dataset[,'description']
)
})
}
但我不知道自己在做什么。你能帮忙吗?
编辑:
我可以使用上面的虚拟数据使用 mapply
和 SIMPLIFY=FALSE
以及 lapply
来让仪表板工作
server <- function(input, output) {
output$boxes <- renderUI({
lapply(dataset[,'title'], function(a) {
box(title = a, p(dataset[dataset$title==a,2]))
})
})
}
但我一直无法让它与真实数据一起工作,并且无法用“虚拟”数据复制问题。
我的真实数据位于数据库中的服务器上。
这应该有效
server <- function(input, output) {
output$boxes <- renderUI({
lapply(dataset[,'title'], function(a) {
box(title = a, p(dataset[dataset$title==a,2]))
})
})
}
您的方法是正确的 mapply
您需要包含 SIMPLIFY = FALSE
以便它 returns 成为一个列表。
server <- function(input, output) {
output$boxes <- renderUI({
mapply(function(x,y) {
box(title = x, p(y)
)
}, x = dataset[,'title'], y = dataset[,'description'], SIMPLIFY = FALSE
)
})
}
或者使用 Map
,它总是 returns 一个列表。
server <- function(input, output) {
output$boxes <- renderUI({
Map(function(x,y) box(title = x, p(y)), dataset$title, dataset$description)
})
}
我正在处理仪表板,我想使用数据框来生成框和描述。我可以使用 lapply 来制作框,但我不知道如何从数据框中提取描述。到目前为止我有(没有描述):
library(shiny)
library(shinydashboard)
dataset <- data.frame("title" = c("A","B","C"), "description" = c("Info about box A", "Info about box B","Info about box C"), "data" = c(1:3))
ui <- fluidPage(
titlePanel("Dynamic Boxes"),
fluidRow(
uiOutput("boxes")
)
)
dataset <- data.frame("title" = c("A","B","C"), "description" = c("Stuff about box A", "Stuff about box B","Stuff about box C"), "data" = c(1:3))
server <- function(input, output) {
output$boxes <- renderUI({
lapply(dataset[,'title'], function(a) {
box(title = a, p("say stuff here"))
})
})
}
我想不出引入描述的正确逻辑。
我试过 mapply:
server <- function(input, output) {
output$boxes <- renderUI({
mapply(function(x,y) {
box(title = x, p(y)
)
}, x = dataset[,'title'], y = dataset[,'description']
)
})
}
但我不知道自己在做什么。你能帮忙吗?
编辑:
我可以使用上面的虚拟数据使用 mapply
和 SIMPLIFY=FALSE
以及 lapply
server <- function(input, output) {
output$boxes <- renderUI({
lapply(dataset[,'title'], function(a) {
box(title = a, p(dataset[dataset$title==a,2]))
})
})
}
但我一直无法让它与真实数据一起工作,并且无法用“虚拟”数据复制问题。
我的真实数据位于数据库中的服务器上。
这应该有效
server <- function(input, output) {
output$boxes <- renderUI({
lapply(dataset[,'title'], function(a) {
box(title = a, p(dataset[dataset$title==a,2]))
})
})
}
您的方法是正确的 mapply
您需要包含 SIMPLIFY = FALSE
以便它 returns 成为一个列表。
server <- function(input, output) {
output$boxes <- renderUI({
mapply(function(x,y) {
box(title = x, p(y)
)
}, x = dataset[,'title'], y = dataset[,'description'], SIMPLIFY = FALSE
)
})
}
或者使用 Map
,它总是 returns 一个列表。
server <- function(input, output) {
output$boxes <- renderUI({
Map(function(x,y) box(title = x, p(y)), dataset$title, dataset$description)
})
}