闪亮的动态表错误“不能强制类型'封闭'到'字符'类型的向量'”

时间:2017-08-10 16:58:37

标签: r shiny shiny-server

这是在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")
      )

    )
  )
))

1 个答案:

答案 0 :(得分:2)

问题是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')语句会使您的应用程序静默崩溃。
  • 请注意,某些事情是被动的,什么时候是被动的。在为t()分配值{。}}后,无需input$year
  • 你不需要调用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)