【问题标题】:Shiny dynamic table error "cannot coerce type 'closure' to vector of type 'character'"闪亮的动态表错误“无法将类型‘闭包’强制转换为‘字符’类型的向量”
【发布时间】:2018-01-19 01:08:18
【问题描述】:

这是一个源自store input as numeric value to generate three tables in Shiny 的问题,与r shiny error Error in as.vector(x, "character") : cannot coerce type 'closure' to vector of type 'character' 类似但不等于

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

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

完整的服务器。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")
      )

    )
  )
))

【问题讨论】:

  • 顺便说一句,如果您发布一个最小的工作示例,这将非常有帮助,请参阅here。包括一些虚假数据,以便其他人可以运行您的代码。它使调试和帮助您解决问题变得更加容易。
  • 非常感谢!!我将其更改为带有mpg 数据集的 MWE
  • 相应地更新了我的答案。

标签: r shiny shiny-server


【解决方案1】:

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

DT::dataTableOutput("my_table")

不会工作,但是

DT::dataTableOutput("more_than_10")

会的。如果要显示整个表格,还必须创建一个数据表,例如这样:

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

然后

DT::dataTableOutput("my_table2")

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


编辑:你用 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)

【讨论】:

  • 嗨。非常感谢 !我把它改成了output$more_than_10 &lt;- DT::renderDataTable(DT::datatable({ my_table() %&gt;% filter(X1 &gt; 10) })) ,我看到了同样的错误。我会继续挖掘
  • 您是否也更改了 DT::dataTableOutput 语句?
  • 确实 :) 我改了
  • 非常感谢!!准备好后,我将添加一个脚注,感谢您的贡献,祝您有美好的一天!
猜你喜欢
  • 2019-12-11
  • 2015-04-29
  • 2013-12-24
  • 2014-05-19
  • 1970-01-01
  • 1970-01-01
  • 2021-08-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多