闪亮:将绘图添加到 uiOutput 中的列

Shiny: Add a plot to a column in uiOutput

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

我的文本可以正常工作,但我正在努力弄清楚如何在其中添加情节。

在下面的 reprex 中,它是相同的图,但在我的实际示例中,我需要使用一个 table 而不是传递到 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)

您应该尝试使用 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)
  })
}