Trevor Nederlof February 2016

Scatterplot in Shiny Module Issue

I have a shiny app, example shown below that should be reproducible where I am trying to show a ggplot2 scatterplot with points which can be excluded as shown in this example here. I am also using modules, which might be part of this issue here. https://gallery.shinyapps.io/106-plot-interaction-exclude/

I keep getting this "Error in eval: object 'xaxis' not found" message. Any ideas? I put the module code up front then the rest of the code for the app.R file.

    library(ggplot2)
    library(scales)
    library(shiny)
    library(shinydashboard)


    ###### MODULE CODE ###############
    scatter_graphUI <- function(id, tab_panel_name, height = "500px") {
      ns <- NS(id)

      tabPanel(tab_panel_name,
        plotOutput(ns("scatter_1"), height = height, click = "plot1_click", brush = 
            brushOpts(id = "plot1_brush")),
        actionButton(ns("exclude_toggle"), "Toggle points"),
        actionButton(ns("exclude_reset"), "Reset")
      )
    }

    scatter_graph <- function(input, output, session, scatter_data, col_select) {

      scatter_data_df <- reactive({
        mtcars
      })

      vals <- reactiveValues()
      data_df <- reactive({
        scatter_df <- scatter_data_df()
        main_df <- scatter_df[,col_select]
        vals$keeprows = rep(TRUE,nrow(main_df))
        main_df
      })

      output$scatter_1 <- renderPlot({

          graph_df <- data_df()
          # Plot the kept and excluded points as two separate data sets
          keep    <- graph_df[ vals$keeprows,]
          exclude <- graph_df[!vals$keeprows,]

          final_df <- keep
          title = paste(colnames(final_df)[1], "vs", colnames(final_df)[2])
          line_method = "quad"
          axis_text = 12
          title_text = 16
          split_colors = TRUE
          colors = c("red","black")

          # create red         

Answers


Chris February 2016

The issue is the two lines:

gg <- ggplot(final_df, aes(x = xaxis, y = yaxis)) + geom_point(color = final_df[,"color"])
gg <- gg + geom_point(data = exclude, shape = 21, fill = NA, color = "black", alpha = 0.25) +
            coord_cartesian(xlim = c(1.5, 5.5), ylim = c(5,35))

Because you have not set a new aes for the exclude object, it inherits the aes from your ggplot call. It therefore needs to find a column named xaxis and yaxis in the exclude dataset. Since you only renamed final_df, it throws this error.

A graph is displayed when you change:

colnames(final_df) <- c("xaxis","yaxis","color")

to:

colnames(final_df) <- c("xaxis","yaxis","color")
colnames(exclude) <- c("xaxis","yaxis")

Post Status

Asked in February 2016
Viewed 1,893 times
Voted 4
Answered 1 times

Search




Leave an answer