这个应用程序添加了一个总分列,它还会自动按日期对数据进行排序,以防用户没有按时间顺序添加日期。
关于我对总分行的评论,我已将总分行的行名设置为“总分”,这会将总分放在最后一行,而不是像其余行一样的行号的行。总分显示数据框中最近日期的日期,而不是最后一次用户输入的日期。
library(shiny)
ui <- fluidPage(
# Application title
titlePanel("Jack & Jones battle over the Castle!"),
# Input
dateInput('date', "Choose today's date:", value = NULL, min = NULL, max = NULL,
format = "yyyy-mm-dd", startview = "month", weekstart = 0,
language = "en", width = NULL),
numericInput("Score Jack", label = "Submit Score Jack", value = 0, min = 0, max = 300, step = 1, width = '10%'),
numericInput("Score Jones", label = "Submit Score Jones", value = 0, min = 0, max = 300, step = 1, width = '10%'),
actionButton("submit", "Submit Scores", icon("Submit") , width = NULL, style="color: #fff; background-color: #337ab7; border-color: #2e6da4"),
DT::DTOutput("score_table")
)
table_data <- data.frame(Date = as.Date(as.character()), `Score Jack` = as.numeric(), `Score Jones` = as.numeric(), check.names = FALSE)
server <- function(input, output, session) {
tableValues <- reactiveValues(df = data.frame(Date = as.Date(as.character()), `Score Jack` = as.numeric(), `Score Jones` = as.numeric(), check.names = FALSE))
observeEvent(input$submit, {
temp <- tableValues$m
newRow <- data.frame(Date = input$date, `Score Jack` = input$`Score Jack`, `Score Jones` = input$`Score Jones`, check.names = FALSE)
if(!is.null(temp)) {
if(isolate(input$date) < temp[nrow(temp), 1]) {
temp <- rbind(temp, newRow)
temp <- dplyr::arrange(temp, Date)
} else {
temp <- rbind(temp, newRow)
}
} else {
temp <- rbind(temp, newRow)
}
tableValues$m <- temp
})
output$score_table <- DT::renderDataTable({
if(!is.null(tableValues$m)){
table_data <- tableValues$m
totalScore <- data.frame(Date = table_data[nrow(table_data),1], `Score Jack` = sum(table_data$`Score Jack`), `Score Jones` = sum(table_data$`Score Jones`), check.names = FALSE)
table_data <- rbind(table_data, totalScore)
if(nrow(table_data > 2)){
table_data <- dplyr::arrange(table_data, Date)
}
}
rownames(table_data)[nrow(table_data)] <- "Score Total"
table_data
})
observe({print(colnames(tableValues$m))})
observe({print(!is.null(tableValues$m))})
}
shinyApp(ui, server)