【发布时间】:2020-12-11 01:11:48
【问题描述】:
第一次在这里发布海报。
1.背景
我想在shinydashboard 中创建一个包含盒子和图表的动态 UI。我发现这篇博文非常适合我的需求:Create a dynamic number of UI elements in Shiny with purrr
2.问题
我只想更改一个设计元素。目前,仪表板每个 fluidRow 包含两个框,因为每个框都有 width=6。我可以将宽度更改为 12 来解决我的问题,但我想看看是否可以为每行创建一盒 width=6。我尝试添加br(),但这并没有多大作用。
这是我的最小示例:
3.代码
这是我的代码
# Load packages---------
library(tidyverse)
library(shinydashboard)
library(shiny)
library(shinyWidgets)
library(shinymanager)
library(kableExtra)
library(readxl)
# Create function that plots graphs
plot_mtcars <- function(df) {
df %>%
ggplot(aes(wt, mpg)) +
geom_point() +
labs(x = "Weight",
y = "Miles per gallon")
}
# Start of shinydashboard---------
header <- dashboardHeader(
title = "Minimal App",
titleWidth = 240
)
sidebar <- dashboardSidebar(
# Width of the sidebar (in pixels)
width = 240,
sidebarMenu(
h1("Welcome!"),
menuItem(text = "WEEKLY RESULTS", tabName = "weekly_results")
)
)
body <- dashboardBody(
tabItems(
tabItem(tabName = "weekly_results",
fluidRow(
uiOutput(outputId = "graphs_ui")
)
)
)
)
ui <- dashboardPage(skin = "black", header = header,
sidebar = sidebar,
body = body)
server <- function(input, output, session) {
mtcars_reactive <- reactive({
mtcars %>%
head(8) %>%
mutate(car_names = rownames(head(mtcars, 8)))
})
# Create reactive graphs
graphs <- reactive({
mtcars_reactive() %>%
group_by(car_names) %>%
nest() %>%
mutate(
graphs = map(data, plot_mtcars)) %>%
pull(graphs)
})
output$graphs_ui <- renderUI({
iwalk(graphs(), ~{
output_name <- paste0("plot_", .y)
output[[output_name]] <- renderPlot(.x)
})
plots_list <- imap(graphs(), ~{
tagList(
tags$div(class = "another-box", id = paste0("danger", .y, "_id"),
box(width = 6,
title = "Mtcars Data",
status = "danger", solidHeader = TRUE,
plotOutput(outputId = paste0("plot_", .y)),
""
),
tags$style(HTML(
paste0(
"
#danger", .y, "_id .box.box-solid.box-danger>.box-header {
color:#fff;
background:", "#FF0000","
}
#danger", .y, "_id .box.box-solid.box-danger {
border-bottom-color:", "#FF0000", ";
border-left-color:", "#FF0000", ";
border-right-color:", "#FF0000",";
border-top-color:", "#FF0000", ";
}
"
)
))
),
br()
)
})
tagList(plots_list)
})
}
shinyApp(ui, server)
【问题讨论】:
标签: r shiny shinydashboard shiny-reactivity