Prima una nota sugli argomenti del server. Per assegnare valori reattivi devi farlo all'interno di un'espressione reattiva. Quindi, per catturare il pennello coordinate è necessario utilizzare questo
observeEvent(input$plotBrush,{
if(is.null(values$brush)){
values$brush <- input$plotBrush}
})
invece di questo
brush <- reactive({
input$plotBrush
})
La seconda versione crea una funzione denominata pennello che si sarebbe chiamata con il pennello().
Un modo per cancellare la trama sul pennello è verificare se i valori $ pennello sono nulli e modificare ciò che si fa in base a ciò. In questo caso, se i valori $ pennello non sono nulli, viene visualizzato un grafico vuoto e non è possibile selezionare nuovi punti.
library(ggplot2)
library(shiny)
runApp(list(
ui = fluidPage(
plotOutput("plot",
brush = brushOpts("plotBrush",
delay = 5000,
resetOnNew = TRUE)
# resetOnNew = TRUE clears the brush
# each time a new plot is displayed.
),
p("Brushed Points:"),
verbatimTextOutput("brushedPoints")
),
server = function(input, output, session) {
values <- reactiveValues(brush = NULL)
output$plot <- renderPlot({
if(is.null(values$brush)){
ggplot(cars, aes(speed, dist)) + geom_point()
} else {
ggplot(cars, aes(speed, dist)) + geom_blank()
}
})
observeEvent(input$plotBrush,{
#Run this whenever points are brushed
if(is.null(values$brush)){
values$brush <- input$plotBrush}
})
output$brushedPoints <- renderPrint({
values$brush
})
}
))
Una seconda opzione è anche disponibile, vedere https://stackoverflow.com/a/35066532/3229332 per la spiegazione
library(ggplot2)
library(shiny)
runApp(list(
ui = fluidPage(
plotOutput("plot",
brush = brushOpts("plotBrush",
delay = 5000,
resetOnNew = TRUE)
# resetOnNew = TRUE clears the brush
# each time a new plot is displayed.
),
p("Brushed Points:"),
verbatimTextOutput("brushedPoints")
),
server = function(input, output, session) {
values <- reactiveValues(brush = NULL)
output$plot <- renderPlot({
if(is.null(values$brush)){
ggplot(cars, aes(speed, dist)) + geom_point()
} else {
ggplot(cars, aes(speed, dist)) + geom_blank()
}
})
observeEvent(input$plotBrush,{
#Run this whenever points are brushed
output$plot <- renderPlot({
if(is.null(values$brush)){
ggplot(cars, aes(speed, dist)) + geom_point()
values$brush <- input$plotBrush
} else {
ggplot(cars, aes(speed, dist)) + geom_blank()
}
})
}
)
output$brushedPoints <- renderPrint({
values$brush
})
}
))
Questo è in effetti :) un pacchetto impressionante e buona soluzione, ho finito per fare che così, ma grazie per metterlo in scrittura qui in modo che altri possano beneficiare in futuro –