4

I am working on a shiny app where I allow a user to select the plotting criteria and then also allow them to brush the plot and see their selection in a table below. I have some NA values in my data. I have noticed that these NAs end up in my brushed point table as full rows of NA. I can remove these manually with something like this. However, I was wondering if I perhaps was doing something wrong on my brush that was causing this.

Code with a working example is below. I have also included an image of a brush selection demonstrating what I mean.

library(shiny)
library(tidyverse)

# replace some random values in mtcars with NA
set.seed(1)
mtnew <-
  as.data.frame(lapply(mtcars, function(m)
    m[sample(
      c(TRUE, NA),
      prob = c(0.8, 0.2),
      size = length(m),
      replace = TRUE
    )]))


# set up UI that allows user to pick x and y variables, see a plot, 
#  brush the plot, and see a table based on the brush
ui <- fluidPage(

  titlePanel("Shiny Test"),

  sidebarLayout(

    sidebarPanel(
      selectInput("xvar", 
                  "pick x", 
                  choices = names(mtnew)),
      selectInput("yvar", 
                  "pick y", 
                  choices = names(mtnew))),

    mainPanel(
      plotOutput("myplot", 
                 brush = brushOpts(id = "plot_brush")),
      tableOutput("mytable")
    )
  )
)


server <- function(input, output) {
  output$myplot <- renderPlot({
    ggplot(data = mtnew) + 
      geom_point(aes(x = !!rlang::sym(input$xvar), 
                     y = !!rlang::sym(input$yvar)))
  })

  output$mytable <- renderTable({
    brush_out <- brushedPoints(mtnew, input$plot_brush)
  })
}

# Complete app with UI and server components
shinyApp(ui, server)

enter image description here

Nick Criswell
  • 1,733
  • 2
  • 16
  • 32

1 Answers1

0

I guess that you'll have to establish which data you want to represent.

You may want to have only defined record without NAs, in that case I would suggest to use the complete.cases function. Yet this solution will highly reduce your data set (below I've applied to your code).

Another option is to preserve all your records but without the NAs. In that case you should consider using imputation methods to set proper values in replacement. Take a look at this post which provides an example.

library(shiny)
library(tidyverse)

# replace some random values in mtcars with NA
set.seed(1)
mtnew <-
  as.data.frame(lapply(mtcars, function(m)
    m[sample(
      c(TRUE, NA),
      prob = c(0.8, 0.2),
      size = length(m),
      replace = TRUE
    )]))

mtnew_complete <- mtnew[complete.cases(mtnew),] 

# set up UI that allows user to pick x and y variables, see a plot, 
#  brush the plot, and see a table based on the brush
ui <- fluidPage(

  titlePanel("Shiny Test"),

  sidebarLayout(

    sidebarPanel(
      selectInput("xvar", 
                  "pick x", 
                  choices = names(mtnew)),
      selectInput("yvar", 
                  "pick y", 
                  choices = names(mtnew))),

    mainPanel(
      plotOutput("myplot", 
                 brush = brushOpts(id = "plot_brush")),
      tableOutput("mytable")
    )
  )
)


server <- function(input, output) {
  output$myplot <- renderPlot({
    #ggplot(data = mtnew) + 
    ggplot(data = mtnew_complete) + 
      geom_point(aes(x = !!rlang::sym(input$xvar), 
                     y = !!rlang::sym(input$yvar)))
  })

  output$mytable <- renderTable({
    #brush_out <- brushedPoints(mtnew, input$plot_brush)
    brush_out <- brushedPoints(mtnew_complete, input$plot_brush)
  })
}

# Complete app with UI and server components
shinyApp(ui, server)
Alessio
  • 910
  • 7
  • 16