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")
)
)
)
))
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 withDT::renderDataTable()
in the server. Sowon't work, but
will. If you want to display the entire table, you have to also create a datatable, for example like this:
Then
should work. Hope this helps!
There were still a few issues with this MWE.
textOutput('year')
statements will crash your app silently.t()
after you assigned the value ofinput$year
to it.This code works: