2
votes

I have a reactive function(datasplit) which does nothing other than subsetting the data based on values sliderInput. And my second reactive function(selectedData) should take the subset data from first reactive function and again filter the columns based on SelectInput.

At last do clustering based on this data.

what works is reactive function 2 which takes input from SelectInput , but failing at first reactive function.

any help much appreciated. I cant upload the data , but for reference my shiny code.

Shiny code :

        library(shiny)
        ui <- fluidPage(
          selectInput("xcol", "X Variable", names(data[,c(2)])),
          selectInput("ycol", "Y Variable", names(data[,c(1)])),
          sliderInput(inputId = "cost",
                      label = "Kosten",
                      value = 1000, min = 1, max = 5000),
          sliderInput(inputId = "count",
                      label = "Anzahl Fälle",
                      value = 500, min = 1, max = 2000),
          numericInput("clusters", "Anzahl Cluster", 3, min=1, max= 9),
          plotOutput(outputId = "plot1")

        )

        server <- function(input, output){
          DealerDF <- read.csv(file = "dealer_category.csv",header = T)
          data <- DealerDF

          datasplit <- reactive({
            subset(data, input$xcol() < input$cost() & input$ycol() < input$count())


          })


          selectedData <- reactive({
            #this seems to be not working 
            datasplit()[, c(input$xcol(), input$ycol())]
          })

          clusters <- reactive({
            kmeans(selectedData(), input$clusters)
          })

          output$plot1 <- renderPlot({

            if (!is.null(selectedData())) {
              par(mar= c(5.1, 4.1,0,1))
              plot(selectedData(),
                   col= clusters()$cluster,
                   pch= 20, cex=3)
              points(clusters()$centers, pch=4, cex=4, lwd=4)
            }
          })

        }

        shinyApp(ui = ui, server = server)

Data :

        cnt av_cst
        479 2.359.695
        479 83.439
        475 891.863
        474 2.496.681
        474 97.654
        474 821.163
        473 1.650.016
        473 143.724
        472 90.398
        470 98.745
        468 681.947
        468 97.392
        467 435.477
        467 97.657
        466 160.547
        463 98.454
        30  24.936
        30  29.432
        30  1.599.577
        30  227.073
        30  227.887
        30  187.147
        30  89.697
        30  615.427
        30  32.398
        30  15.133
        30  24.445.753
        30  25.944
        30  344.933
        30  10.237
        30  15.86
        17082   30.425
        11358   75.541
        9788    30.638
        9667    30.381
        7302    73.051
        6849    1.009.921
        6299    124.441
        6018    30.158
        5646    124.569
        5383    1.133.911
        5381    30.278
        4357    123.213
        3989    3.065
1
@PorkChop data is dataframe with 25 columnsuser3560220
Please provide the dataPork Chop
Your inputs don't need the () at the end. input$cost etcPork Chop
@PorkChop i tried that before posting it here. but dint helpuser3560220
Hi guys anyway to solve this issue. I think I am doing something wrong, but cant get over my headuser3560220

1 Answers

0
votes

Since I don't have the data I could not test the following code. Moreover, I could not understand why you are treating your inputs (input$col) as functions (input$col()). I have implemented this nested subsetting many times. You can add multiple filters. Hoping that your subset functions works without a glitch, the following will help:

DealerDF <- read.csv(file = "dealer_category.csv",header = T)
          data <- reactive({
              DealerDF
          })

          datasplit <- reactive({
            IstSubset <- subset(data(), input$xcol() < input$cost() & input$ycol() < input$count())
            selectedData <- IstSubset[,c(input$xcol(), input$ycol())]
            clusters <- kmeans(selectedData, input$clusters)
            clusters
          })


          output$plot1 <- renderPlot({
            if (!is.null(dataSplit())) {
              par(mar= c(5.1, 4.1,0,1))
              plot(datasplit(),
                   col= clusters()$cluster,
                   pch= 20, cex=3)
              points(clusters()$centers, pch=4, cex=4, lwd=4)
            }
          })

If you don't get the plot output, you might consider moving the kmeans calculation outside the reactive loop