首页 > 解决方案 > Shiny中的表格输出问题

问题描述

这是我的数据集

在此处输入图像描述

我想用 flextable 过滤 Shiny 中的产品并得到这样的东西:

在此处输入图像描述

这是我的代码:

library(shiny)

my_data = data.frame(product = rep(c("auto", "boat"),each=2),
                  year = c("2009", "2011", "2005", "2019"),
                  price = c("10 000", "20 000", "7 000", "60 000"),
                  speed = c("220", "250", "70", "140"))


ui <- fluidPage(
  selectInput("product", "", choices = my_data$product),
  tableOutput("tbl")
)

server <- function(input, output, session) {
  output$tbl <- renderTable( {
    out <- subset(my_data, product ==input$product)
    library(flextable)
    flextable(out) # i got error
  })
}

shinyApp(ui, server)

但我收到了这个错误:无法将类“flextable”强制转换为 data.frame

我们怎样才能解决它?一些帮助将不胜感激

标签: rshinydashboardflextable

解决方案


您不能renderTableflextable对象一起使用。见?renderTable

expr:返回可与 xtable::xtable() 一起使用的 R 对象的表达式。

在这里,您可以找到有关如何flextable在闪亮的应用程序中使用的教程。

请检查以下内容:

library(shiny)
library(flextable)

my_data = data.frame(product = rep(c("auto", "boat"),each=2),
                     year = c("2009", "2011", "2005", "2019"),
                     price = c("10 000", "20 000", "7 000", "60 000"),
                     speed = c("220", "250", "70", "140"))


ui <- fluidPage(
  selectInput("product", "", choices = my_data$product),
  uiOutput("tbl")
)

server <- function(input, output, session) {
  output$tbl <- renderUI( {
    out <- subset(my_data, product ==input$product)
    library(flextable)
    htmltools_value((flextable(out)))
  })
}

shinyApp(ui, server)

推荐阅读