Karthik Suresh
Karthik Suresh

Reputation: 71

Using a selected row to subset another table in r shiny

I am new to using DT in R shiny.Basically what i am trying to do here is to use the select value from the first table to filter the second table.

my Ui.r is

library(shiny)
library(shinydashboard)
 ui <- dashboardPage(skin="green",
                dashboardHeader(title="Inventory Management"),
                dashboardSidebar(disable = TRUE),
                dashboardBody(fluidRow(column(4,box(status="success",
                             uiOutput("Firstselection"),
                             br(),
                             uiOutput("Secondselection"))
                             ),
                             column(4,infoBoxOutput("salesbox")),
                             column(4,infoBoxOutput("Runoutbox")),
                             column(4,infoBoxOutput("Excessbox"))),
                             actionButton("actionbtn","Run"),
                fluidRow(tabBox(tabPanel(
                  DT::dataTableOutput(outputId="table"),title = "Stock Available for the category chosen",width = 12),
                  tabPanel(DT::dataTableOutput(outputId="asso"),title = "Associated products",width = 12)))

                ))

and my server is

server <-function(input, output, session) {
observeEvent(input$actionbtn, {source('global.r',local = TRUE)
#choose sub category based on category
output$Firstselection<-renderUI({selectInput("ray",
                                             "Category:",
                                             c("All",unique(as.character(bestpred$lib_ray))))})
output$Secondselection<-renderUI({selectInput("sray",
                                              "Sub Category:",
                                              c("All",unique(as.character(bestpred[bestpred$lib_ray==input$ray,"lib_sray"]))))})

# Filter data based on selections
output$table <- DT::renderDataTable({
  data <- bestpred
  if (input$ray != "All"){
    data <- data[data$lib_ray == input$ray,]
  }
  if (input$sray != "All"){
    data <- data[data$lib_sray == input$sray,]
  }
  data
},filter="top"
)
output$salesbox<-renderInfoBox({infoBox("Total Sales",sum(data()$Total_Sales),icon = icon("line-chart"))})
output$Runoutbox<-renderInfoBox({infoBox("Total Runout",sum(data()$status=="Runout"),icon = icon("battery-quarter"))})
output$Excessbox<-renderInfoBox({infoBox("Total excess",sum(data()$status=="Excess"),icon = icon("exclamation-triangle"))})



output$asso <- DT::renderDataTable({
  asso <- test1
  s=data[input$tablatable_rows_selected,1]
  asso <- asso[asso$num_art == s,]
  asso
},filter="top")



})}

So when i select a row in the output table i wanna use that as an filter for my asso table

this code dosent poup any error but the output table asso is always empty

Upvotes: 2

Views: 2419

Answers (1)

Tonio Liebrand
Tonio Liebrand

Reputation: 17689

Find a generalized solution in the following: Adapted from here: https://yihui.shinyapps.io/DT-rows/

library(shiny)
library(DT)

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

  output$x1 = DT::renderDataTable(cars, server = FALSE)

  output$x2 = DT::renderDataTable({
    sel <- input$x1_rows_selected
    if(length(cars)){
      cars[sel, ]
    }

  }, server = FALSE)  

})


ui <- fluidPage(

  fluidRow(
    column(6, DT::dataTableOutput('x1')),
    column(6, DT::dataTableOutput('x2'))
  )

)

shinyApp(ui, server)

Upvotes: 4

Related Questions