简体   繁体   中英

RStudio: Filter data based on value selected in datatable

I'm new in RStudio and I am currently working on a RShiny project. The user has to give some input value: an user id, a first name, a last name or a date of birth of a client to see some specific info about this client. Based on the input, there will be a datatable of some general info about the clients that meet the input values. Because some clients can have the same first name, last name or date of birth, the user has to select the client he wants te see. Based on the selected row (of the datatable) some other info will occur. I want to filter the data based on the id of the selected row (because the id is the only thing that is unique).

I managed to do this with this code, but after some days, it suddenly didn't work anymore with the same code. The problem now is that I always see the info of the same person, regardless the row I select (for example: the datatable only shows the info of person x, also when I select person y in the datatable). Can anyone help me with this? Thank you!

This is my code:

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)
              ),

server.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"))

  })

Example data in the dataset 'total_customers' that you can work with is: 在此处输入图像描述

With the limited information provided, it's hard to tell exactly how everything works. If you could give us greater detail as to what layout the UI is in, what libraries are included, how the other inputs are created ( x3_rows_selected ) and what other data is there (eg. account_use_total ).

However, this should provide some simple clarity as to a working app.

I would suggest using a reactive expression to refer to the table_general_info_abo object and then calling it when you need it, as this helps organise your app and improve efficiency.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM