这是我的R代码.

正如你所见,每个小组("A"和"B")都有多个表格.我怎样才能显示与每个组相关的所有表格?

我试着用 map ,但没用.

有什么帮助吗?

library(shiny)

lista <- as.list(1:12)

tables_shiny<- mtcars %>%
                  rownames_to_column() %>%
                    slice(1:5) %>%
                      pivot_longer(cols = mpg:last_col()) %>%
                        mutate(groups = c(rep("A",27),rep("B",28)), .before = everything())

groups <- tables_shiny$groups %>% unique()
choices <- tables_shiny$rowname %>% unique()


ui <- fluidPage(

    # Application title
    titlePanel("Old Faithful Geyser Data"),

    # Sidebar with a slider input for number of bins
    sidebarLayout(
        sidebarPanel(
          radioButtons(
            "groups",
            label = "Groups",
            choices = groups,
            selected =  groups[1]
          )

        ),


        mainPanel(
            gt_output("tables_1")
        )
    )
)

# Define server logic required to draw a histogram
server <- function(input, output) {

  groups_reactive<- reactive({input$groups})


output$tables_1 <- render_gt({
                      tables_shiny %>%
                          filter(groups == groups_reactive()) %>%
                            group_split(rowname) %>%
                              map(~ .x %>% gt() %>% tab_header(title = groups_reactive()))


  })



}

# Run the application
shinyApp(ui = ui, server = server)

推荐答案

每个输出UI只能呈现一个表,因此必须动态添加它们:

library(tidyverse)
library(shiny)
library(gt)

lista <- as.list(1:12)

tables_shiny <-
  mtcars %>%
  rownames_to_column() %>%
  slice(1:5) %>%
  pivot_longer(cols = mpg:last_col()) %>%
  mutate(groups = c(rep("A", 27), rep("B", 28)), .before = everything())

groups <- tables_shiny$groups %>% unique()
choices <- tables_shiny$rowname %>% unique()


ui <- fluidPage(

  # Application title
  titlePanel("Old Faithful Geyser Data"),

  # Sidebar with a slider input for number of bins
  sidebarLayout(
    sidebarPanel(
      radioButtons(
        "groups",
        label = "Groups",
        choices = groups,
        selected =  groups[1]
      )
    ),
    mainPanel(
      uiOutput("tables")
    )
  )
)

# Define server logic required to draw a histogram
server <- function(input, output) {
  observeEvent(
    eventExpr = input$groups,
    handlerExpr = {
      message("foo")

      tables_shiny$rowname %>%
        unique() %>%
        walk(~ {
          insertUI(selector = "#tables", ui = gt_output(outputId = .x))

          output[[.x]] <-
            tables_shiny %>%
            filter(groups == input$groups & rowname == .x) %>%
            gt() %>%
            tab_header(title = .x) %>%
            render_gt()
        })
    }
  )
}

# Run the application
shinyApp(ui = ui, server = server)

R相关问答推荐

将收件箱变量传递给ggplot 2函数

在处理因素时,Base R grep家族比stringr变体快得多

如何在热图中绘制一个图形,但在每个单元格中通过饼形图显示?

如何判断某列中由某些行组成的百分比

有没有方法将琴弦完全捕捉到R中的多边形?

使用lapply的重新定位功能

如何在四进制仪表板值框中显示值(使用shiny 的服务器计算)

隐藏e_mark_line的工具提示

在某些栏和某些条件下,替换dfs列表中的NA

如何在一次运行中使用count进行多列计数

汇总数据表中两个特定列条目的值

在GG图中绘制射线的自动程序

如何根据数据帧中的值从该数据帧中提取值?

如何创建累加到现有列累计和的新列?

如何计算每12行的平均数?

使用列中的值来调用函数调用中应使用的其他列

计算来自单独分组的分幅的值的百分位数

如何将EC50值绘制在R中的剂量-react 曲线上?

为什么不能使用lApply在包装函数中调用子集

R:使用ApexCharge更改标签在饼图中的位置