【发布时间】:2020-09-15 18:54:29
【问题描述】:
一定是晚了,因为我想不通。当我在 DataTable 下面运行 Shiny 应用程序时,它显示为空白。
你能帮我找出我的错误吗?
据我所知,如果没有选择输入,df_filtered 应该返回df。它被renderDataTable 称为反应函数。
library(shiny)
library(shinydashboard)
library(DT)
library(dplyr)
# Test data
LANZ <- c(1,2,3,4,5)
TIPO <- c('V5', 'V5', 'V10', 'V10', 'V10')
PROY <- c('1', '2', '2', '2', '3')
YEAR <- c(2019, 2020, 2020, 2020, 2019)
df <- data.frame(LANZ, TIPO, PROY, YEAR)
colnames(df)[1] <- "LANZ"
colnames(df)[2] <- "TIPO"
colnames(df)[3] <- "PROY"
colnames(df)[4] <- "YEAR"
ui <- dashboardPage(
dashboardHeader(title = "Title"),
dashboardSidebar(
sidebarMenu(
)),
dashboardBody(
wellPanel(
fluidRow(
column(4, selectInput("idYearD", "", selected=NULL, multiple=FALSE, choice=c('Year' = '', c("2019","2020")))),
column(4, selectInput("idTipo", "", selected=NULL, multiple=FALSE, choice=c('Tipo' = '', c("V5","V10")))),
column(4, selectInput("idProy", "", selected=NULL, multiple=FALSE, choice=c('Proy' = '', c("1","2","3")))
)),
fluidRow(
column(12, DT::dataTableOutput('dataTable'))
))
))
server <- function(input, output) {
df_filtered <- reactive({
data_df <- df
if (!is.null( input$idYearD ) ) {
data_df <- data_df %>% filter(YEAR %in% input$idYearD)
}
if (!is.null( input$idTipo )) {
data_df <- data_df %>% filter(TIPO %in% input$idTipo)
}
if (!is.null( input$idProy )) {
data_df <- data_df %>% filter(PROY %in% input$idProy)
}
data_df
})
output$dataTable <- DT::renderDataTable(
df_filtered()
)
}
shinyApp(ui = ui, server = server)
最好的问候。
【问题讨论】: