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

喜你入骨 提交于 2019-12-24 10:06:35

问题


This is a question derivated after store input as numeric value to generate three tables in Shiny and similar, but not equal, to r shiny error Error in as.vector(x, "character") : cannot coerce type 'closure' to vector of type 'character'

I want to create a large table to create some tables after that table in a Shiny app.

Here is my MWE (seems to be a problem with the titles, the h3's in the UI) :

Full 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)
    }))

  }
)

Full 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:


The problem is that my_table is a reactive, and you can not output a reactive with DT::dataTableOutput(). You can only do that for objects created with DT::renderDataTable() in the server. So

DT::dataTableOutput("my_table")

won't work, but

DT::dataTableOutput("more_than_10")

will. If you want to display the entire table, you have to also create a datatable, for example like this:

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

Then

DT::dataTableOutput("my_table2")

should work. Hope this helps!


EDIT: You updated your answer with a MWE.

There were still a few issues with this MWE.

  • You cannot use the same output twice. So two textOutput('year') statements will crash your app silently.
  • Keep in mind when something is a reactive and when it isn't. no need for t() after you assigned the value of input$year to it.
  • you don't need the call to the pacman package ;) and you do need ggplot2 for the mpg dataset.

This code works:

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)


来源:https://stackoverflow.com/questions/45619547/shiny-dynamic-table-error-cannot-coerce-type-closure-to-vector-of-type-chara

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!