Je modifie un script issu de R Shiny concernant la sélection de point sur un graphe et exclusion des points sélectionnés du fichier
renvoyé dans un reactive.
Le script utilise le dataframe "mcars" et le code est ci-dessous.
Ma question : comment adapter ce script pour que le dataframe en entrée dbinit <- mtcars soit lui même un reactive dataframe
qui sera lui même déjà utilisé par ailleurs pour des tests statistiques sur les différentes varaibles.
Je dois mal comprendre la syntaxe des reactive et reactivevalues ou bien ignorer certaines fonctions.
Il se peut que le forum ai déjà répondu en partie à cette interrogation merci alors d'éclairer ma lanterne.
Code : Tout sélectionner
# Research
# https://datascience.stackexchange.com/questions/5326/how-to-read-file-from-user-in-shiny-and-assign-it-to-a-variable-in-global-r
library(shiny)
library(ggplot2)
cat("\014") ; rm(list=ls())
# Utilisation daframe mtcars / exemple dans ggplot2
# Ajout col : colone de couleur
# dbinit : prévu pour être reactive dataframe data initiales
# dbselect : prévu pour être reactive dataframe data finales
dbinit <- mtcars
dbselect <- c()
dbinit$col <- "black"
ui <- fluidPage(
p("Pour exclure un/des points, dessiner un rectangle autour du/des points et valider le bouton Exclusion"),
p("Pour annuler une ou plusieurs exclusions, redessiner le rectangle autour du/des points et valider le bouton Exclusion"),
p("Pour tout annuler, bouton Annulation")
,
plotOutput("plot1_3", brush = brushOpts(id = "plot1_brush", fill="red", resetOnNew=TRUE)),
actionButton("exclude_toggle", "Exclusion des points entourés", icon("window-close"),
style="color: #fff; background-color: red; border-color: black"),
actionButton("exclude_reset", "Annulation", icon("sync"),
style="color: #000080; background-color: #E0FFFF; border-color: black"),
plotOutput("plot1_4")
)
server <- function(input, output) {
# For storing which rows have been excluded
vals <- reactiveValues(keeprows = rep(TRUE, nrow(dbinit)))
output$plot1_3 <- renderPlot({
# Plot the kept and excluded points as two separate data sets
keep <- dbinit[ vals$keeprows, , drop = FALSE]
exclude <- dbinit[!vals$keeprows, , drop = FALSE]
if (nrow(exclude)>0) {
exclude$col <- "red"
}
ggplot(keep, aes(x= wt , y= mpg)) + geom_point(color = keep$col) +
geom_smooth(method = lm, fullrange = TRUE, color = "black") +
geom_point(data = exclude, shape = 21, fill = "red", color = exclude$col)
})
# Plot the final data sets
output$plot1_4 <- renderPlot({
keep <- dbinit[ vals$keeprows, , drop = FALSE]
keep$col <- "green"
ggplot(keep, aes(wt , y = mpg)) + geom_point(color = keep$col) +
geom_smooth(method = lm, fullrange = TRUE, color = "black")
})
# Toggle points that are brushed, when button is clicked
observeEvent(input$exclude_toggle, {
res <- brushedPoints(dbinit, input$plot1_brush, allRows = TRUE)
# fonction xor compare les 2 dataframes et modifie le vals$keeprows
vals$keeprows <- xor(vals$keeprows, res$selected_)
#observe(cat(str(vals$keeprows)))
})
# Reset all points
observeEvent(input$exclude_reset, {
vals$keeprows <- rep(TRUE, nrow(dbinit))
#observe(cat(str(vals$keeprows)))
})
# Store selected rows # # Base on reply_01
dbselect<- reactive({
data2<- dbinit[ vals$keeprows, , drop = FALSE]
data2$col <- "green"
data2
})
# Observe wich rows have been selected
observe({print(vals$keeprows)})
observe({print(dbselect())})
# output$contents <- renderText({
# data2<- dbinit[ vals$keeprows, , drop = FALSE]
# observe(cat(str(data2)))
# #assign('dbselect',data2,envir=.GlobalEnv)
#})
}
shinyApp(ui, server)