Skip to content

Instantly share code, notes, and snippets.

@aagarw30
Created April 26, 2018 19:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aagarw30/ffa7806867150c6b2462436eb2dbafcf to your computer and use it in GitHub Desktop.
Save aagarw30/ffa7806867150c6b2462436eb2dbafcf to your computer and use it in GitHub Desktop.
Update data value to NA based from a specific column based for the data points that are under the brush - Demo - brushedPoints() in R Shiny
# Load required packages
library(shiny)
library(ggplot2)
# UI Code begins here
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
## Add inputs widgets later
),
# Outputs
mainPanel(
plotOutput(outputId = "scatterplot2", brush = "plot_brush"), # the scatter plot
tableOutput("table") # dataset
)
)
)
# Server code begins here
server <- function(input, output) {
mt <- reactiveValues(data=head(mtcars,10)) # making the dataset reactiveValues so that any changes in data later could be reflected throughout
# Create scatterplot object the plotOutput function is expecting
output$scatterplot2 <- renderPlot({
ggplot(data = mt$data, aes(x = mpg, y = hp)) +
geom_point()
})
## Returns the dataset
output$table <- renderTable({
mt$data
})
# Observe will listen for brush over the point and change the "hp" column values for all data points under brush to NA
observe({
df = brushedPoints(mt$data, brush = input$plot_brush, allRows = TRUE)
index = which(df$selected_== TRUE)
df[index, "hp"] = NA
df = df[, -which(names(df) %in% "selected_")]
mt$data = df
})
}
# Create a Shiny app object
shinyApp(ui = ui, server = server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment