1

I would like to use the principal component analysis for the problem that has been solved at this link Identify points within a rectangle in a scatterplot. The chart displays correctly, but I miss something in displaying the table. If anyone knew where the problem is, please help.

Code (EDIT):

head(decathlon2.active[, 1:6], 4)
##         X100m Long.jump Shot.put High.jump X400m X110m.hurdle
## SEBRLE   11.0      7.58     14.8      2.07  49.8         14.7
## CLAY     10.8      7.40     14.3      1.86  49.4         14.1
## BERNARD  11.0      7.23     14.2      1.92  48.9         15.0
## YURKOV   11.3      7.09     15.2      2.10  50.4         15.3

#################################################################

library(shiny)
library(DT)

server <- function(input, output) {
  output$plot1 <- renderPlot({
    data(decathlon2)
    decathlon2.active <- decathlon2[1:23, 1:10]
    head(decathlon2.active[, 1:6], 4)
    res.pca <- PCA(decathlon2.active, graph = FALSE)
    fviz_pca_ind(res.pca)

  })

  df <- reactive({
    res.pca$ind$coord
  })

    output$plotui <- renderUI({
    plotOutput("plot1", height=300,
               brush = brushOpts(id = "plot_brush")
    )
  })

  output$plot_brushed_points <- renderDataTable({
      res <- brushedPoints(df(), input$plot_brush) 

    datatable(res)
  })
}

ui <- fluidPage(
  uiOutput("plotui"),
  dataTableOutput("plot_brushed_points")
)

shinyApp(ui = ui, server = server)

Result:

enter image description here

Elia
  • 53
  • 7

1 Answers1

1

I think your issue is that in res <- brushedPoints(df, input$plot_brush) you reference the dataframe df which contains data that you generated in your renderPlot({...}) command. That data would not be available to call in your table output command. Try adding

df <- reactive({
    #your data here
})

and then calling res <- brushedPoints(df(), input$plot_brush).

If that doesn't seem to work, post some sample data and I'll update my answer.

Edit:

Here is a more explicit suggestion. You are still trying to call data from renderPlot({...}). Try to generate the data in the reactive statement and then call it after.

df <- reactive({
    data(decathlon2)
    decathlon2.active <- decathlon2[1:23, 1:10]
    res.pca <- PCA(decathlon2.active, graph = FALSE)
})

You will stil need to supply the xvar and yvar arguments to brushedPoints.

https://shiny.rstudio.com/reference/shiny/latest/brushedPoints.html

AndS.
  • 7,748
  • 2
  • 12
  • 17