Created
April 26, 2018 19:41
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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