Digvijay
Digvijay

Reputation: 406

Select row(s) from a summary data table and use cell value to subset original data and create a new data table in Shiny

I have created a summary table from iris data set and when i select any row from that datatable I want to use the row selected to subset main data and later publish that subset data as a datatable.

    library(shiny)
    library(dplyr)
    library(DT)


    setwd("C:/Users/143812/Documents/Shiny")

    df <- iris
    vchoices <- colnames(df)
    ui <- fluidPage(h1("PLOT FOR NOW"),
                    sidebarLayout(sidebarPanel(
                      fluidPage(
                        column(10,selectInput(inputId = "group",label = "Group BY",choices = vchoices)),
                        column(10,selectInput(inputId = "operator",label = "OPERATOR",choices = c("sum","mean"),selected = "sum")),
                        column(10,uiOutput("operateUI"))
                      )

                    ),
                    mainPanel(dataTableOutput("summarytable"),dataTableOutput("drilldata")))
    )

    server <- function(input,output,session){
      df1 <- reactive({df %>% group_by_(input$group) %>% summarise(Result = get(input$operator)(get(input$operate)))})

      #input for integer and numeric columns
      a <- (sapply(df,class))
      lv <- a=="integer" | a=="numeric" 
      b <- a[lv]
      numchoice <- names(b)
      output$operateUI <- renderUI({
        selectInput(inputId = "operate",label = input$operator,choices = numchoice)
      })

      #publish summary table to select output from by name summary table          
      output$summarytable <- DT::renderDataTable(df1())

      #create data for drill report          
      drilldata <- reactive({
                            shiny::validate(need(length(input$summarytable_rows_selected) > 0, "Select Rows to drill down")
                                            )
                            selected_column <-  df1[as.integer(input$summarytable_rows_selected),]$get(input$group)

                            df[df$get(input$group) %in% selected_column, ]

      })
      output$drilldata <- DT::renderDataTable(drilldata())


      #function end  
    }

    shinyApp(ui,server)

This code gives me error: object of type 'closure' is not subsettable. I'm asking the user to select the column for group by and operation to be performed, either sum or mean as well as the column on which the operation is to be performed. This method seems to work in this code by mlegge where the values are predefined and not asked from user. link to question

Upvotes: 0

Views: 1140

Answers (1)

Bertil Baron
Bertil Baron

Reputation: 5003

Your error message most probably comes from this line

selected_column <-  df1[as.integer(input$summarytable_rows_selected),]$get(input$group)

where you have forgotten the function brackets () after df1. For me get() didn't work so I used [[ instead

try with

selected_column <-  df1()[as.integer(input$summarytable_rows_selected),][[input$group]]

I also had problem with this line as well

df[df$get(input$group) %in% selected_column, ]

and changed it to

df[df[[input$group]] %in% selected_column, ]

and it was working for me

Upvotes: 2

Related Questions