闪亮动态 table 错误 "cannot coerce type 'closure' to vector of type 'character'"

Shiny dynamic table error "cannot coerce type 'closure' to vector of type 'character'"

这是 and similar, but not equal, to

后派生出的一道题

我想创建一个大 table 以便在 Shiny 应用程序中创建一些 table 之后的 table。

这是我的 MWE (似乎标题有问题,UI 中的 h3) :

完整 server.R:

#
# This is the server logic of a Shiny web application. You can run the 
# application by clicking 'Run App' above.
#

# Required libraries
if (!require("pacman")) install.packages("pacman")
p_load(shiny,dplyr,DBI,ggplot2)

# Define server logic
shinyServer(

  function(input, output) {

    display_table <- reactive({
      t <- reactive({ as.character(input$year) })

      # Read the RCA matrix
      long_table = tbl_df(mpg) %>% filter(year == t())

      return(long_table)
    })

    output$year = renderText(input$year)

    output$miles <- DT::renderDataTable(DT::datatable({
      display_table() %>% select(manufacturer,model,cty,hwy)
    }))

    output$desc <- DT::renderDataTable(DT::datatable({
      display_table() %>% select(manufacturer,model,trans,class)
    }))

  }
)

完整 ui.R:

#
# This is the user-interface definition of a Shiny web application. You can
# run the application by clicking 'Run App' above.
#

# Required libraries
if (!require("pacman")) install.packages("pacman")
p_load(shiny)

# Define UI for application that draws a histogram
shinyUI(fluidPage(

  verticalLayout(
    # Application title
    titlePanel("ggplot2's mpg dataset example"),

    mainPanel(

      # User parameters
      column(12,
             tags$h3("Parameters"), 
             selectInput('year', 'Year', c("Select year",1999:2015), selected = 1999)
      ),

      # Display tables
      column(12, 
             #withMathJax(includeMarkdown("Theory.md")),
             h3("Miles per gallon for cars made in the year",textOutput("year")),
             DT::dataTableOutput("miles"),
             h3("Description for cars made in the year",textOutput("year")),
             DT::dataTableOutput("desc")
      )

    )
  )
))

问题是my_table是反应式,你不能用DT::dataTableOutput()输出反应式。您只能对服务器中使用 DT::renderDataTable() 创建的对象执行此操作。所以

DT::dataTableOutput("my_table")

不行,但是

DT::dataTableOutput("more_than_10")

会的。如果你想显示整个table,你还必须创建一个数据table,例如这样:

   output$my_table2 <- DT::renderDataTable(DT::datatable({
      my_table()
    }))

然后

DT::dataTableOutput("my_table2")

应该可以。希望这对您有所帮助!


EDIT: You updated your answer with a MWE.

这个 MWE 仍然存在一些问题。

  • 您不能两次使用相同的输出。因此,两个 textOutput('year') 语句将使您的应用程序无声地崩溃。
  • 请记住什么时候是反应性的,什么时候不是。将 input$year 的值分配给它后,不需要 t()
  • 您不需要调用 pacman 包 ;) 并且您确实需要 ggplot2 用于 mpg 数据集。

此代码有效:

library(ggplot2)
library(shiny)
library(dplyr)
server<- function(input,output)
{


      display_table <- reactive({
        t <-  as.character(input$year) 
        # Read the RCA matrix
        long_table = tbl_df(mpg) %>% filter(year == t)
        return(long_table)
      })

      output$year = renderText(input$year)
      output$year2 = renderText(input$year)

      output$miles <- DT::renderDataTable(DT::datatable({
        display_table() %>% select(manufacturer,model,cty,hwy)
      }))

      output$desc <- DT::renderDataTable(DT::datatable({
        display_table() %>% select(manufacturer,model,trans,class)
      }))


}

ui<- shinyUI(fluidPage(

  verticalLayout(
    # Application title
    titlePanel("ggplot2's mpg dataset example"),

    mainPanel(

      # User parameters
      column(12,
             tags$h3("Parameters"), 
             selectInput('year', 'Year', c("Select year",1999:2015), selected = 1999)
      ),

      # Display tables
      column(12, 
             h3("Miles per gallon for cars made in the year",textOutput("year")),
             DT::dataTableOutput("miles"),
             h3("Description for carss made in the year",textOutput("year2")),
             DT::dataTableOutput("desc")
      )

    )
  )
))

shinyApp(ui,server)