【问题标题】:Trying to extract a specific row from a data table in Shiny app尝试从 Shiny 应用程序中的数据表中提取特定行
【发布时间】:2015-12-17 10:22:08
【问题描述】:

我对 R 很陌生,这是我第一次尝试 Shiny,所以请原谅可能是明显的修复。

我有以下数据框:

     ID Bet.Total Point.Total Cash.Value
1  2000  5,741.00   43,264.00      61.81
2  2001 10,009.70   90,087.00     128.70
3  2002 10,875.06   97,177.00     138.82
4  2003  5,754.04   43,658.00      62.37
5  2004  5,771.00   43,860.00      62.66
6  2005  5,097.70   38,743.00      55.35
7  2006  1,605.45   59,402.00      84.86
8  2007  7,430.43   55,995.00      79.99
9  2008 20,960.50  188,645.00     269.49
10 2009 10,626.80   92,209.00     131.73
11 2010 42,711.50  384,404.00     549.15

我正在尝试创建一个闪亮的应用程序,用户可以在其中选择他们的 ID,并且该应用程序返回他们的 Bet.Total、Point.Total 和 Cash.Value。

这是我的 ui.R 代码:

library(shiny)

shinyUI(fluidPage(
    titlePanel("Bet Rewards Calculator"),

    fluidRow(
            column(3,
                   selectizeInput("ID", choices = "ID",
                    label = h4("Rewards ID")),
                   br(),
                   actionButton("submit", "Submit")),

            column(7, offset = 2,
                   DT::dataTableOutput("betReport")))

    )
)

这是我的 server.R 代码: 图书馆(DT)

betReport <- read.csv("BetReport.csv")

shinyServer(function(input, output) {
    observeEvent(input$submit)
    output$x1 <- DT::renderDataTable({betReport})

# print the selected indices
    output$x2 = renderPrint({
            s = input$ID_rows_selected
            if (length(s)) {
                    cat("This is your balance.")
                    cat(s, sep = ",")
            }
    })

})

我尝试了从简单到复杂的服务器代码的各种排列以仅返回相应的数据行,但没有成功。当我运行此特定代码时,应用程序崩溃并收到以下错误消息:
Error in exprToFunction(handlerExpr, handler.env, handler.quoted) : argument "expr_sub" is missing, with no default

任何想法/建议将不胜感激!

【问题讨论】:

  • 有很多问题。在server.R 中出现x1x2ID_rows_selected,它们不会出现在ui.R 中。 betReport 输出未定义。不清楚用户可以在哪里选择他/她的ID,因为selectInputchoices 参数只有一个值。

标签: r shiny


【解决方案1】:

我不完全确定您希望您的应用做什么,因此我非常有信心以下解决方案无法让您完全实现您的目标,但我认为它应该很接近。

您收到的错误来自您的server.R 文件中的observeEvent(input$submit)。它需要在触发事件时触发的第二个参数,并且您没有指定任何内容。在下面的代码中,我只是将所有内容都包含在您的服务器代码中,我认为这不是您想要的,只是为了给您一个示例。

此外,在ui.R 文件中,您需要为每个元素提供与server.R 文件中计算的输出元素相对应的标签。这些选择也很无聊,因为现在只有一个可能的选择 (ID)。

但是,以下两个文件可能会让您启动并运行。至少它会显示数据表并响应输入。

第一个是server.R。请记住在您的数据中包含要读取的行以及所有内容。

shinyServer(function(input, output) {
                observeEvent(input$submit, {

                output$x1 <- DT::renderDataTable(betReport)

                                        # print the selected indices                                                                                                           
                output$x2 = renderPrint({
                    s = input$ID_rows_selected
                    if (length(s)) {
                        cat("This is your balance.")
                        cat(s, sep = ",")
                    } else {
            print("I shouldn't be here but the rows_selected is wrong")
                    }
                })
                                                                                   })

            })

ui.R

library(shiny)

shinyUI(fluidPage(
    titlePanel("Bet Rewards Calculator"),

    fluidRow(
        column(3,
               selectizeInput("ID", choices = "ID",
                              label = h4("Rewards ID")),
               br(),
               actionButton("submit", "Submit")),

        column(7, offset = 2,
               DT::dataTableOutput("x1"),
               br(),
               textOutput("x2")
               )
        )



    )
        )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-21
    • 2017-01-01
    • 2013-10-24
    • 1970-01-01
    相关资源
    最近更新 更多