这是在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):
#
# 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)
}))
}
)
#
# 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")
)
)
)
))
答案 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
。此代码有效:
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)