问题
I have a table of customers in one of my tabs (let's call it a shop tab) within the shinydashboard. I want to add links for each customer that will send you to another tab in the shinydashboard (customer tab) which will provide more detailed information (mainly graphs of the customer behavior) about this specific customer. This could be easily achieved by copying the customer id and pasting it into the search bar in the customer tab, but I wanna know whether it is possible to do it more interactively -> by clicking on specific customer in a shop tab, the dashboard sends you to the customer tab and at the same time fills the customer id into the search bar, so you are provided with all the graphs filtered by customer id. All suggestions welcomed.
Thanks!
回答1:
You can pick up which rows, columns, cells are clicked with DT
, there's great documentation on using DT with Shiny. Then once that is picked up you could filter the table of customers and send the user to another tab using updateTabsetPanel
.
An example below.
library(DT)
library(shiny)
df <- data.frame(
customer = LETTERS[1:5],
id = seq(1, 5)
)
ui <- navbarPage(
"Stackoverflow",
id = "tabs", # give id to use updateTabsetPanel
tabPanel(
"shop",
h2("Customers are below"),
DTOutput("table")
),
tabPanel(
"customer",
uiOutput("customer")
)
)
server <- function(input, output, session){
output$table <- renderDT(df, selection = "single")
observeEvent(input$table_rows_selected, {
updateTabsetPanel(session = session, inputId = "tabs", selected = "customer")
})
output$customer <- renderUI({
c <- df[input$table_rows_selected, "customer"]
h2(paste("Hi I'm customer", c, "!"))
})
}
shinyApp(ui, server)
来源:https://stackoverflow.com/questions/54854023/shiny-dashboard-reactive-table-with-links