R Shiny仪表板和多行存在问题。

huangapple go评论97阅读模式
英文:

Problem with R shiny dashboard and multiple rows

问题

抱歉,你的代码中包含许多 HTML 实体编码,我会为你提供一个没有编码的翻译版本:

  1. 如果问题太基础,我道歉,但我对 R Shiny 相对陌生,想要开发一个工具来显示两行中的多个直方图图表,第一行一个,第二行三个。当我只在一行中绘制时,它可以工作,但当我添加第二行时,出现问题。以下是一个最小可行代码,我感激您的帮助:
  2. if (!require("pacman")) install.packages("pacman")
  3. pacman::p_load(tidyverse, tidyr, shiny, shinydashboard)
  4. data <- as.data.frame(mtcars)
  5. ui <- dashboardPage(
  6. skin = "yellow",
  7. dashboardHeader(title = "My dashboard"),
  8. dashboardSidebar(
  9. sidebarMenu()
  10. ),
  11. dashboardBody(
  12. fluidRow(
  13. box(title = "Primary Metric:", status = "primary", solidHeader = TRUE, plotOutput("CPlot"), width = 7),
  14. box(selectInput("metric",
  15. "Metric:",
  16. c("mpg", "cyl", "hp", "wt")),
  17. width = 3
  18. )
  19. ),
  20. fluidRow(
  21. box(title = "Sub-Metric1", width = 4, status = "warning", solidHeader = TRUE, plotOutput("CPlot")),
  22. box(title = "Sub-Metric2", width = 4, status = "warning", solidHeader = TRUE, plotOutput("CPlot")),
  23. box(title = "Sub-Metric3", width = 4, status = "warning", solidHeader = TRUE, plotOutput("CPlot"))
  24. )
  25. )
  26. )
  27. server <- function(input, output){
  28. # 绘图
  29. output$CPlot <- renderPlot({
  30. ggplot(data, aes(x=data[[input$metric]])) +
  31. geom_histogram(fill="#D55E00", color="#e9ecef", alpha=0.9) +
  32. labs(
  33. title = paste0("Distribution of ", paste0(input$metric), " over the last 12 month"),
  34. x = req(input$metric),
  35. y = "Count") +
  36. theme_minimal()
  37. })
  38. }
  39. 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:

  1. pacman::p_load(tidyverse, tidyr, shiny, shinydashboard)
  2. data &lt;- as.data.frame(mtcars)
  3. ui &lt;- dashboardPage(
  4. skin = &quot;yellow&quot;,
  5. dashboardHeader(title = &quot;My dashboard&quot;),
  6. dashboardSidebar(
  7. sidebarMenu()
  8. ),
  9. dashboardBody(
  10. fluidRow(
  11. box(title = &quot;Primary Metric:&quot;, status = &quot;primary&quot;, solidHeader = T, plotOutput(&quot;CPlot&quot;), width = 7),
  12. box(selectInput(&quot;metric&quot;,
  13. &quot;Metric:&quot;,
  14. c(&quot;mpg&quot;, &quot;cyl&quot;, &quot;hp&quot;, &quot;wt&quot;)),
  15. width = 3
  16. )
  17. ),
  18. fluidRow(
  19. box(title = &quot;Sub-Metric1&quot;, width = 4, status = &quot;warning&quot;, solidHeader = T, plotOutput(&quot;CPlot&quot;)),
  20. box(title = &quot;Sub-Metric2&quot;, width = 4, status = &quot;warning&quot;, solidHeader = T, plotOutput(&quot;CPlot&quot;)),
  21. box(title = &quot;Sub-Metric3&quot;, width = 4, status = &quot;warning&quot;, solidHeader = T, plotOutput(&quot;CPlot&quot;))
  22. )
  23. )
  24. )
  25. server &lt;- function(input, output){
  26. # plot
  27. output$CPlot &lt;- renderPlot({
  28. ggplot(data, aes(x=data[[input$metric]])) +
  29. geom_histogram(fill=&quot;#D55E00&quot;, color=&quot;#e9ecef&quot;, alpha=0.9) +
  30. labs(
  31. title = paste0(&quot;Distribution of &quot;, paste0(input$metric), &quot; over the last 12 month&quot;),
  32. x = req(input$metric),
  33. y = &quot;Count&quot;) +
  34. theme_minimal()
  35. })
  36. }
  37. shinyApp(ui = ui, server = server) ```
  38. </details>
  39. # 答案1
  40. **得分**: 1
  41. 以下是翻译好的部分:
  42. 这种行为的原因是shiny使用相同名称的HTML元素`outputID`渲染,这是无效的显示,我知道的唯一解决方案是使整个绘图对象具有反应性并分配给唯一的outputID(CPlot、DPlot、EPlot、FPlot)
  43. ### 完整的代码示例:
  44. ```R
  45. data <- as.data.frame(mtcars)
  46. ui <- dashboardPage(
  47. skin = "yellow",
  48. dashboardHeader(title = "My dashboard"),
  49. dashboardSidebar(
  50. sidebarMenu()
  51. ),
  52. dashboardBody(
  53. fluidRow(
  54. box(title = "Primary Metric:", status = "primary", solidHeader = T, plotOutput("CPlot"), width = 7),
  55. box(selectInput("metric",
  56. "Metric:",
  57. c("mpg", "cyl", "hp", "wt")),
  58. width = 3
  59. )
  60. ),
  61. fluidRow(
  62. box(title = "Sub-Metric1", width = 4, status = "warning", solidHeader = T, plotOutput("DPlot")),
  63. box(title = "Sub-Metric2", width = 4, status = "warning", solidHeader = T, plotOutput("EPlot")),
  64. box(title = "Sub-Metric3", width = 4, status = "warning", solidHeader = T, plotOutput("FPlot"))
  65. )
  66. )
  67. )
  68. server <- function(input, output){
  69. # 绘图
  70. Plot <- reactive({
  71. ggplot(data, aes(x=data[[input$metric]])) +
  72. geom_histogram(fill="#D55E00", color="#e9ecef", alpha=0.9) +
  73. labs(
  74. title = paste0("Distribution of ", paste0(input$metric), " over the last 12 month"),
  75. x = req(input$metric),
  76. y = "Count") +
  77. theme_minimal()
  78. }) %>% bindEvent(input$metric)
  79. output$CPlot <- output$DPlot <- output$EPlot <- output$FPlot <- renderPlot({Plot()})
  80. }
  81. 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:

  1. data &lt;- as.data.frame(mtcars)
  2. ui &lt;- dashboardPage(
  3. skin = &quot;yellow&quot;,
  4. dashboardHeader(title = &quot;My dashboard&quot;),
  5. dashboardSidebar(
  6. sidebarMenu()
  7. ),
  8. dashboardBody(
  9. fluidRow(
  10. box(title = &quot;Primary Metric:&quot;, status = &quot;primary&quot;, solidHeader = T, plotOutput(&quot;CPlot&quot;), width = 7),
  11. box(selectInput(&quot;metric&quot;,
  12. &quot;Metric:&quot;,
  13. c(&quot;mpg&quot;, &quot;cyl&quot;, &quot;hp&quot;, &quot;wt&quot;)),
  14. width = 3
  15. )
  16. ),
  17. fluidRow(
  18. box(title = &quot;Sub-Metric1&quot;, width = 4, status = &quot;warning&quot;, solidHeader = T, plotOutput(&quot;DPlot&quot;)),
  19. box(title = &quot;Sub-Metric2&quot;, width = 4, status = &quot;warning&quot;, solidHeader = T, plotOutput(&quot;EPlot&quot;)),
  20. box(title = &quot;Sub-Metric3&quot;, width = 4, status = &quot;warning&quot;, solidHeader = T, plotOutput(&quot;FPlot&quot;))
  21. )
  22. )
  23. )
  24. server &lt;- function(input, output){
  25. # plot
  26. Plot &lt;- reactive({
  27. ggplot(data, aes(x=data[[input$metric]])) +
  28. geom_histogram(fill=&quot;#D55E00&quot;, color=&quot;#e9ecef&quot;, alpha=0.9) +
  29. labs(
  30. title = paste0(&quot;Distribution of &quot;, paste0(input$metric), &quot; over the last 12 month&quot;),
  31. x = req(input$metric),
  32. y = &quot;Count&quot;) +
  33. theme_minimal()
  34. }) %&gt;% bindEvent(input$metric)
  35. output$CPlot &lt;-output$DPlot &lt;-output$EPlot &lt;-output$FPlot &lt;- renderPlot({Plot()})
  36. }
  37. shinyApp(ui = ui, server = server)
  38. </details>

huangapple
  • 本文由 发表于 2023年6月22日 17:25:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76530408.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定