【问题标题】:RStudio: Filter data based on value selected in datatableRStudio:根据数据表中选择的值过滤数据
【发布时间】:2021-03-08 19:51:19
【问题描述】:

我是 RStudio 的新手,目前正在做一个 RShiny 项目。用户必须提供一些输入值:用户 id、名字、姓氏或客户的出生日期,以查看有关此客户的某些特定信息。根据输入,将有一个关于满足输入值的客户端的一些一般信息的数据表。因为有些客户可以有相同的名字、姓氏或出生日期,所以用户必须选择他想见的客户。根据(数据表的)所选行,将出现一些其他信息。我想根据所选行的 id 过滤数据(因为 id 是唯一唯一的东西)。

我设法用这段代码做到了这一点,但几天后,它突然不再使用相同的代码了。现在的问题是,无论我选择哪一行,我总是看到同一个人的信息(例如:数据表只显示人 x 的信息,当我在数据表中选择人 y 时)。谁能帮我这个?谢谢!

这是我的代码:

ui.R


column(width = 3, textInput("customer_id", "Customer ID", "")),
              column(width = 3, textInput("first_name", "Voornaam (hoofdletter!)", "")),
              column(width = 3, textInput("last_name", "Achternaam (hoofdletter!)", "")),
              column(width = 3, textInput("birth_date", "Geboortedatum (J-M-D)", "")),
              fluidRow(
               box(title = "Personen die voldoen aan filter", dataTableOutput("x3"), width = 12, background = "aqua")
              )

fluidRow(
                box(title = "Verbruik transacties", dataTableOutput(outputId = "table_use_transactions"), width = 6),
                box(title = "Recharge transacties", dataTableOutput(outputId = "table_recharge_transations"), width = 6)
              ),

服务器.R

output$x3 <- renderDataTable({
    table_general_info_abo <- total_customers %>%
      filter(id == input$customer_id | last_name == input$last_name | first_name == input$first_name | date_of_birth == input$birth_date) %>%
      select(id, first_name, last_name, date_of_birth, gender, full_address, distance_km)

    DT::datatable(table_general_info_abo, selection = 'single')
  })

  output$table_recharge_transations <- renderDataTable({
    req(input$x3_rows_selected)

    table_recharge_transactions_total <- account_recharge_total %>%
      filter(customer_id == table_general_info_abo[input$x3_rows_selected, 1]) %>%
      select(full_date, sale_time, payment_amount, credit_bonus_amount, electronic) %>%
      arrange(full_date, sale_time)

    DT::datatable(table_recharge_transactions_total, options = list(lengthMenu = c(5, 10, 20), pageLength = 5), colnames = c("Date", "Time", "Amount", "Bonus", "Electronic?"))

  })

  output$table_use_transactions <- renderDataTable({
    req(input$x3_rows_selected)

    table_use_transactions_total <- account_use_total %>%
      filter(customer_id == table_general_info_abo[input$x3_rows_selected, 1]) %>%
      select(full_date, sale_time, machine_type, machine_number, capacity, payment_amount) %>%
      arrange(full_date, sale_time)

    DT::datatable(table_use_transactions_total, options = list(lengthMenu = c(5, 10, 20), pageLength = 5), colnames = c("Date", "Time", "Type", "Number", "Capacity", "Amount"))

  })

您可以使用的数据集“total_customers”中的示例数据是:

【问题讨论】:

  • 您可能需要包含一些示例数据以让其他人看到数据结构。
  • 我不知道如何包含。数据是机密的。
  • 你能模拟具有相似结构的数据来重现同样的问题吗?请参阅有关可复制示例 (reprex) stackoverflow.com/help/minimal-reproducible-examplestackoverflow.com/questions/5963269/… 的指南
  • 我添加了一些示例数据来处理
  • 请提供一些可重现数据,例如dput()。屏幕截图没有多大帮助。谢谢。

标签: r filter datatable shinydashboard


【解决方案1】:

由于所提供的信息有限,很难准确地说明一切是如何运作的。 如果您可以更详细地介绍 UI 的布局、包含哪些库、如何创建其他输入 (x3_rows_selected) 以及那里有哪些其他数据(例如 account_use_total)。

但是,这应该可以让您清楚地了解正在运行的应用程序。

我建议使用反应式表达式来引用 table_general_info_abo 对象,然后在需要时调用它,因为这有助于组织应用程序并提高效率。

library(shiny)
library(shinydashboard)
library(DT)

total_customers <- data.frame(id = c("1.A", "60.L", "10.A", "80.A", "120.L"), 
                   first_name = c("first1", "first2", "first3", "first4", "first5"),
                   last_name = c("last1", "last2", "last3", "last4", "last5"),
                   date_of_birth = c(as.Date("1998-01-16"), as.Date("2001-06-05"),
                                     as.Date("1965-10-02"), as.Date("1970-07-21"),
                                     as.Date("1997-09-02")), 
                   gender = c("M", "F", "F", "M", "M"), 
                   full_address = c("address1", "address2", "address3", "address4", "address5"),
                   distance_km = c(0.30, 1.40, 5.98, 0.80, 4.88))


ui <- fluidPage(

            column(width = 3, textInput("customer_id", "Customer ID", "")),
            column(width = 3, textInput("first_name", "Voornaam (hoofdletter!)", "")),
            column(width = 3, textInput("last_name", "Achternaam (hoofdletter!)", "")),
            column(width = 3, textInput("birth_date", "Geboortedatum (J-M-D)", "")),
            fluidRow(
                box(title = "Personen die voldoen aan filter", dataTableOutput("x3"), width = 12, background = "aqua")
            )
            
)

server <- function(input, output, session) {

    table_general_info_abo <- reactive({
        total_customers %>%
        filter(id == input$customer_id | last_name == input$last_name | first_name == input$first_name | date_of_birth == input$birth_date) %>%
        select(id, first_name, last_name, date_of_birth, gender, full_address, distance_km)
    })
    
    output$x3 <- DT::renderDataTable({
        DT::datatable(table_general_info_abo(), selection = 'single')
    })
    
}


shinyApp(ui = ui, server = server)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-24
    • 1970-01-01
    • 1970-01-01
    • 2020-10-09
    • 1970-01-01
    • 2016-10-14
    • 2015-11-11
    • 1970-01-01
    相关资源
    最近更新 更多