0

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

Digvijay
  • 406
  • 3
  • 12
  • Try replacing the `df$get(input$group)` with `df[[input$group]]` Also, in the `summarise`, I am not following this `get(input$operator)(get(input$operate))` Are u applying a function – akrun Nov 16 '17 at 06:29
  • Using `df[[input$group]]` still gives that error, for `summarise` `get(input$operator)` gives me sum/mean and `(get(input$operate))` gets me column to perform operation on. this becomes for eg. `sum(Sepal.length)` – Digvijay Nov 16 '17 at 06:59
  • The second part i.e. subset is not clear. Where is the `input$summarytable_rows_selected`? In the first step, you are `summarise`ing by group. In that dataset there is no row index. How exactly you wanted to subset for the `drilldata()`? Let's say if you are grouping by 'Species', then there will be 3 rows and it is not clear about the logic in the last 2 lines of `drilldata` code – akrun Nov 16 '17 at 07:45
  • `input$ID_rows_selected` is a function of DT package that return the row selected. Rows from `renderdatatable(df1())` in this case, for eg, if I `group by` species there will be three rows, if I click on row one of that `datatable`, code line two of drilldata i.e `df1[as.integer(input$summarytable_rows_selected),]$get(input$group)` will give me `setosa` as output `as.integer(input$summarytable_rows_selected)` == 1 `get(input$group)` == species `df1[1,]$species` == "setosa" and then third line of drilldata will select rows from `df` which contains setosa as species – Digvijay Nov 16 '17 at 09:20

1 Answers1

2

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

Bertil Baron
  • 4,923
  • 1
  • 15
  • 24
  • Thanks a lot! this worked. Is there any reason why `get()` in not working for `selected_columns` ? it works when used in `summarise` – Digvijay Nov 16 '17 at 13:49
  • Sorry I can't answer that because I usually don't work with `get()` – Bertil Baron Nov 16 '17 at 13:51
  • i was using `get()` since values of input$ID is character and I wanted to use them as an argument to function, Is `[[ ]]` doing the same thing ? Or this is used since input$ID is a list output and `[[ ]]` is used to extract elements of list. – Digvijay Nov 16 '17 at 13:56
  • No,`[[` is extracting from the string value of input$ID but you can use nummeric values as well like the first element `[[1]]` – Bertil Baron Nov 16 '17 at 14:08