Last active
December 27, 2019 14:48
-
-
Save ByungSunBae/cc996f321131a8fe56b3381798c08f45 to your computer and use it in GitHub Desktop.
sync_plots_quiz_answer
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
library("shiny") | |
library("ggplot2") | |
library("dplyr") | |
# reference : | |
# https://shiny.rstudio.com/gallery/plot-interaction-basic.html | |
# https://shiny.rstudio.com/gallery/plot-interaction-zoom.html | |
data <- mtcars %>% mutate(id = 1:n()) | |
my_theme <- | |
theme_bw() + | |
theme( | |
legend.position='none', | |
panel.grid = element_blank()) | |
ui <- fluidPage( | |
fluidRow( | |
mainPanel( | |
h3('How to Sync Plots'), | |
code('hint : reactiveValues, observe') | |
) | |
), | |
fluidRow( | |
column(6, plotOutput('plot1', brush=brushOpts(id='brush'))), | |
column(6, plotOutput('plot2')) | |
) | |
) | |
server <- function(input, output) { | |
# Operation Window Plot 그리기 | |
output$plot1 <- renderPlot({ | |
ggplot(data, aes(wt, qsec)) + | |
geom_text(aes(label = id)) + | |
ggtitle(label = "Operation Window") + | |
my_theme | |
}) | |
output$plot2 <- renderPlot({ | |
# 드래그로 선택된 데이터 | |
sel_data <- brushedPoints(data, input$brush) | |
data2 <- data %>% mutate(selection = ifelse(id %in% sel_data$id, 1, 0)) | |
ggplot(data2) + | |
geom_text(aes(x = hp, y = mpg, label = id, color = as.factor(selection))) + | |
ggtitle(label = "Result Window") + | |
scale_color_manual(values = c("gray", "red")) + | |
my_theme | |
}) | |
} | |
shinyApp(ui, server) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment