英文:
Problem with R shiny dashboard and multiple rows
问题
抱歉,你的代码中包含许多 HTML 实体编码,我会为你提供一个没有编码的翻译版本:
如果问题太基础,我道歉,但我对 R Shiny 相对陌生,想要开发一个工具来显示两行中的多个直方图图表,第一行一个,第二行三个。当我只在一行中绘制时,它可以工作,但当我添加第二行时,出现问题。以下是一个最小可行代码,我感激您的帮助:
if (!require("pacman")) install.packages("pacman")
pacman::p_load(tidyverse, tidyr, shiny, shinydashboard)
data <- as.data.frame(mtcars)
ui <- dashboardPage(
skin = "yellow",
dashboardHeader(title = "My dashboard"),
dashboardSidebar(
sidebarMenu()
),
dashboardBody(
fluidRow(
box(title = "Primary Metric:", status = "primary", solidHeader = TRUE, plotOutput("CPlot"), width = 7),
box(selectInput("metric",
"Metric:",
c("mpg", "cyl", "hp", "wt")),
width = 3
)
),
fluidRow(
box(title = "Sub-Metric1", width = 4, status = "warning", solidHeader = TRUE, plotOutput("CPlot")),
box(title = "Sub-Metric2", width = 4, status = "warning", solidHeader = TRUE, plotOutput("CPlot")),
box(title = "Sub-Metric3", width = 4, status = "warning", solidHeader = TRUE, plotOutput("CPlot"))
)
)
)
server <- function(input, output){
# 绘图
output$CPlot <- renderPlot({
ggplot(data, aes(x=data[[input$metric]])) +
geom_histogram(fill="#D55E00", color="#e9ecef", alpha=0.9) +
labs(
title = paste0("Distribution of ", paste0(input$metric), " over the last 12 month"),
x = req(input$metric),
y = "Count") +
theme_minimal()
})
}
shinyApp(ui = ui, server = server)
希望这对你有所帮助。如果需要进一步解释或有其他问题,请告诉我。
英文:
My apologies if the question is trivial but I am fairly new to R shiny and want to develop a tool to show several histogram plots in two rows, one in the first row and three in the second row. When I plot this in only one row it works, but when I add the sHere is a minimal working code, I appreciate your help with this:
pacman::p_load(tidyverse, tidyr, shiny, shinydashboard)
data <- as.data.frame(mtcars)
ui <- dashboardPage(
skin = "yellow",
dashboardHeader(title = "My dashboard"),
dashboardSidebar(
sidebarMenu()
),
dashboardBody(
fluidRow(
box(title = "Primary Metric:", status = "primary", solidHeader = T, plotOutput("CPlot"), width = 7),
box(selectInput("metric",
"Metric:",
c("mpg", "cyl", "hp", "wt")),
width = 3
)
),
fluidRow(
box(title = "Sub-Metric1", width = 4, status = "warning", solidHeader = T, plotOutput("CPlot")),
box(title = "Sub-Metric2", width = 4, status = "warning", solidHeader = T, plotOutput("CPlot")),
box(title = "Sub-Metric3", width = 4, status = "warning", solidHeader = T, plotOutput("CPlot"))
)
)
)
server <- function(input, output){
# plot
output$CPlot <- renderPlot({
ggplot(data, aes(x=data[[input$metric]])) +
geom_histogram(fill="#D55E00", color="#e9ecef", alpha=0.9) +
labs(
title = paste0("Distribution of ", paste0(input$metric), " over the last 12 month"),
x = req(input$metric),
y = "Count") +
theme_minimal()
})
}
shinyApp(ui = ui, server = server) ```
</details>
# 答案1
**得分**: 1
以下是翻译好的部分:
这种行为的原因是shiny使用相同名称的HTML元素`outputID`渲染,这是无效的显示,我知道的唯一解决方案是使整个绘图对象具有反应性并分配给唯一的outputID(CPlot、DPlot、EPlot、FPlot)
### 完整的代码示例:
```R
data <- as.data.frame(mtcars)
ui <- dashboardPage(
skin = "yellow",
dashboardHeader(title = "My dashboard"),
dashboardSidebar(
sidebarMenu()
),
dashboardBody(
fluidRow(
box(title = "Primary Metric:", status = "primary", solidHeader = T, plotOutput("CPlot"), width = 7),
box(selectInput("metric",
"Metric:",
c("mpg", "cyl", "hp", "wt")),
width = 3
)
),
fluidRow(
box(title = "Sub-Metric1", width = 4, status = "warning", solidHeader = T, plotOutput("DPlot")),
box(title = "Sub-Metric2", width = 4, status = "warning", solidHeader = T, plotOutput("EPlot")),
box(title = "Sub-Metric3", width = 4, status = "warning", solidHeader = T, plotOutput("FPlot"))
)
)
)
server <- function(input, output){
# 绘图
Plot <- reactive({
ggplot(data, aes(x=data[[input$metric]])) +
geom_histogram(fill="#D55E00", color="#e9ecef", alpha=0.9) +
labs(
title = paste0("Distribution of ", paste0(input$metric), " over the last 12 month"),
x = req(input$metric),
y = "Count") +
theme_minimal()
}) %>% bindEvent(input$metric)
output$CPlot <- output$DPlot <- output$EPlot <- output$FPlot <- renderPlot({Plot()})
}
shinyApp(ui = ui, server = server)
英文:
The reason for this behavior is that shiny renders HTML elements with the same name outputID
: 'CPlot', which is invalid to display, the only solution i know is to make the whole plot object reactive and assign it to unique outputIDs ( CPlot, DPlot, EPlot, FPlot)
full code example:
data <- as.data.frame(mtcars)
ui <- dashboardPage(
skin = "yellow",
dashboardHeader(title = "My dashboard"),
dashboardSidebar(
sidebarMenu()
),
dashboardBody(
fluidRow(
box(title = "Primary Metric:", status = "primary", solidHeader = T, plotOutput("CPlot"), width = 7),
box(selectInput("metric",
"Metric:",
c("mpg", "cyl", "hp", "wt")),
width = 3
)
),
fluidRow(
box(title = "Sub-Metric1", width = 4, status = "warning", solidHeader = T, plotOutput("DPlot")),
box(title = "Sub-Metric2", width = 4, status = "warning", solidHeader = T, plotOutput("EPlot")),
box(title = "Sub-Metric3", width = 4, status = "warning", solidHeader = T, plotOutput("FPlot"))
)
)
)
server <- function(input, output){
# plot
Plot <- reactive({
ggplot(data, aes(x=data[[input$metric]])) +
geom_histogram(fill="#D55E00", color="#e9ecef", alpha=0.9) +
labs(
title = paste0("Distribution of ", paste0(input$metric), " over the last 12 month"),
x = req(input$metric),
y = "Count") +
theme_minimal()
}) %>% bindEvent(input$metric)
output$CPlot <-output$DPlot <-output$EPlot <-output$FPlot <- renderPlot({Plot()})
}
shinyApp(ui = ui, server = server)
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论