表作为基于R闪亮用户输入的输出

问题描述 投票:0回答:1

我想构建一个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)
)

作为输出,我想为每个输入返回一个带有新行的表,例如使用“提交”按钮时,三列(日期,杰克分数,琼斯分数)和两个分数列的总和。我尝试使用renderTable函数,但到目前为止没有任何结果。当搜索类似的问题时,我发现使用DT包可以解决问题。但是,它似乎不再存在。

我是Shiny的新手,所以感谢您的投入。

r shiny
1个回答
0
投票

在下面找到一个小的工作示例。要渲染的数据帧存储在reactiveValues中(按照Add values to a reactive table in shiny)。单击按钮(我将其变成actionButton并命名为“ submitButton”)后,会通过observeEvent(...)行添加一行。

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)
© www.soinside.com 2019 - 2024. All rights reserved.