【问题标题】:Shiny: Add a plot to a column in uiOutputShiny:向 uiOutput 中的列添加绘图
【发布时间】:2021-08-16 20:13:25
【问题描述】:

我正在为 uiOutput 动态生成流体行,因为用户选择将确定有多少行。对于每一行,我有 3 列 - 两列是文本,第三列是绘图。

我的文字工作正常,但我正在努力弄清楚如何让情节在那里。

在下面的 reprex 中,它是同一个图,但在我的实际示例中,我需要使用一个不同于传递给 map() 的表,但要根据 .x 值之一对其进行过滤。

library(tidyverse)

ui <- fluidPage(
  uiOutput("row_mt")
)

server <- function(input, output) {
  output$row_mt <- renderUI({
    mt_list <- mtcars %>%
      rownames_to_column(var = "model") %>%
      rowwise() %>%
      group_split() %>%
      map(~{
        tagList(fluidRow(
          column(4,
                 .x$model),
          column(4,
                 .x$mpg),
          column(4, 
                 mtcars %>% 
                 filter(cyl == .x$cyl) %>% 
                   ggplot(aes(x = mpg, y = cyl)) + geom_point())
        ),
        br()
        )

      })

    tagList(mt_list)
  })
}

shinyApp(ui, server)

【问题讨论】:

    标签: r shiny shinydashboard


    【解决方案1】:

    您应该尝试使用renderPlot 创建绘图,然后使用plotOutputrenderUI 中显示它。

    试试这个

    server <- function(input, output) {
      
      output$myplot <- renderPlot({
        mtcars %>%
          rownames_to_column(var = "model") %>%
          rowwise() %>%
          group_split() %>%
          map(~{
                     mtcars %>% 
                       filter(cyl == .x$cyl) %>% 
                       ggplot(aes(x = mpg, y = cyl)) + geom_point()
            
          })
      })
      
      output$row_mt <- renderUI({
        mt_list <- mtcars %>%
          rownames_to_column(var = "model") %>%
          rowwise() %>%
          group_split() %>%
          map(~{
            tagList(fluidRow(
              column(4,
                     .x$model),
              column(4,
                     .x$mpg),
              column(4, 
                     plotOutput("myplot", height=100, width=100))
            ),
            br()
            )
            
          })
        
        tagList(mt_list)
      })
    }
    

    【讨论】:

    • 绘图是特定于每一行的,因此需要动态生成。
    • 是的,您需要将其作为output$myplots 的列表并在renderUI 中使用。这只是第一行的示例。我不确定你在每一行中绘制什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-23
    • 1970-01-01
    • 2015-10-05
    • 2019-08-12
    相关资源
    最近更新 更多