[英]Table as output based on user input in R shiny
我想構建一個 R Shiny 應用程序,該應用程序創建一個表格,顯示來自用戶的輸入值並將兩個數字輸入變量的總和相加。
我已經定義了以下輸入
# Define UI for application that returns table based on input values
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%'),
submitButton("Submit Scores", icon("Submit") , width = NULL)
)
作為 output,我想返回一個表,其中每個輸入都有一個新行,例如三列(日期、得分 Jack、得分 Jones)和表格末尾的一行,以在“提交”時對兩個得分列求和' 按鈕被使用。 我嘗試使用 renderTable function,但到目前為止沒有產生任何結果。 在搜索類似問題時,我找到了使用 DT package 的解決方法。 然而,它似乎已經不存在了。
我是 Shiny 的新手,所以會很感激任何輸入。
在下面找到一個小的工作示例。 您要呈現的數據幀存儲在reactiveValues
中(根據Add values to a reactive table in shiny )。 一旦通過observeEvent(...)
行單擊按鈕(我變成了一個actionButton
並命名為“submitButton”),就會添加一行。
library(shiny)
# Define UI for application that returns table based on input values
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("scoreJack", label = "Submit Score Jack", value = 0, min = 0, max = 300, step = 1, width = '10%'),
numericInput("scoreJones", label = "Submit Score Jones", value = 0, min = 0, max = 300, step = 1, width = '10%'),
actionButton("submitButton", "Submit Scores", icon("Submit") , width = NULL),
# Output
tableOutput("table")
)
# Define server logic required to render the table
server <- function(input, output) {
values <- reactiveValues()
values$df <- data.frame(data = character(), jack = character(), jones = character())
observeEvent(input$submitButton, {
new_row <- data.frame(data = strftime(input$date, "%Y-%m-%d"), jack = input$scoreJack, jones = input$scoreJones)
values$df <- rbind(values$df, new_row)
})
output$table <- renderTable(values$df)
}
# Run the application
shinyApp(ui = ui, server = server)
這個應用程序添加了一個總分列,它還會自動按日期對數據進行排序,以防萬一用戶沒有按時間順序添加日期。
關於我對總分行的評論,我將總分行的行名設置為“總分”,這會將總分放在最后一行,而不是像 rest 那樣的行號. 總分顯示數據框中最近日期的日期,而不是最后一次用戶輸入的日期。
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.