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

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

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 &lt;- as.data.frame(mtcars)

ui &lt;- dashboardPage(
  skin = &quot;yellow&quot;,
  dashboardHeader(title = &quot;My dashboard&quot;),
  dashboardSidebar(
    sidebarMenu()
  ),
  dashboardBody(
    fluidRow(
      box(title = &quot;Primary Metric:&quot;, status = &quot;primary&quot;, solidHeader = T, plotOutput(&quot;CPlot&quot;), width = 7),
      box(selectInput(&quot;metric&quot;, 
                      &quot;Metric:&quot;, 
                      c(&quot;mpg&quot;,  &quot;cyl&quot;, &quot;hp&quot;, &quot;wt&quot;)),  
          width = 3
      )
    ),
    fluidRow(
      box(title = &quot;Sub-Metric1&quot;, width = 4,  status = &quot;warning&quot;, solidHeader = T, plotOutput(&quot;CPlot&quot;)),
      box(title = &quot;Sub-Metric2&quot;, width = 4,  status = &quot;warning&quot;, solidHeader = T, plotOutput(&quot;CPlot&quot;)),
      box(title = &quot;Sub-Metric3&quot;, width = 4,  status = &quot;warning&quot;, solidHeader = T, plotOutput(&quot;CPlot&quot;))
    )
  )
)




server &lt;- function(input, output){
  
  # plot
  output$CPlot &lt;- renderPlot({
    ggplot(data, aes(x=data[[input$metric]])) +
      geom_histogram(fill=&quot;#D55E00&quot;, color=&quot;#e9ecef&quot;, alpha=0.9) +
      labs(
        title = paste0(&quot;Distribution of &quot;, paste0(input$metric), &quot; over the last 12 month&quot;),
        x = req(input$metric),
        y = &quot;Count&quot;) +
      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 &lt;- as.data.frame(mtcars)

ui &lt;- dashboardPage(
  skin = &quot;yellow&quot;,
  dashboardHeader(title = &quot;My dashboard&quot;),
  dashboardSidebar(
    sidebarMenu()
  ),
  dashboardBody(
    fluidRow(
      box(title = &quot;Primary Metric:&quot;, status = &quot;primary&quot;, solidHeader = T, plotOutput(&quot;CPlot&quot;), width = 7),
      box(selectInput(&quot;metric&quot;, 
                      &quot;Metric:&quot;, 
                      c(&quot;mpg&quot;,  &quot;cyl&quot;, &quot;hp&quot;, &quot;wt&quot;)),  
          width = 3
      )
    ),
    fluidRow(
      box(title = &quot;Sub-Metric1&quot;, width = 4,  status = &quot;warning&quot;, solidHeader = T, plotOutput(&quot;DPlot&quot;)),
      box(title = &quot;Sub-Metric2&quot;, width = 4,  status = &quot;warning&quot;, solidHeader = T, plotOutput(&quot;EPlot&quot;)),
      box(title = &quot;Sub-Metric3&quot;, width = 4,  status = &quot;warning&quot;, solidHeader = T, plotOutput(&quot;FPlot&quot;))
    )
  )
)


server &lt;- function(input, output){
  
  # plot
  Plot &lt;- reactive({
    ggplot(data, aes(x=data[[input$metric]])) +
      geom_histogram(fill=&quot;#D55E00&quot;, color=&quot;#e9ecef&quot;, alpha=0.9) +
      labs(
        title = paste0(&quot;Distribution of &quot;, paste0(input$metric), &quot; over the last 12 month&quot;),
        x = req(input$metric),
        y = &quot;Count&quot;) +
      theme_minimal()
  }) %&gt;% bindEvent(input$metric)
  
    output$CPlot &lt;-output$DPlot &lt;-output$EPlot &lt;-output$FPlot &lt;- renderPlot({Plot()})

  
}

shinyApp(ui = ui, server = server)



</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:

确定