2015-06-02 13 views
8

Grafici in supporto lucido e gestori di pennelli. È possibile "cancellare"/"rimuovere"/"cancellare" il rettangolo spazzolato senza che l'utente clicchi altrove sulla trama? Per esempio, se volessi memorizzare le coordinate spazzolate una volta che il pennello è finito e quindi cancellare la trama, questo è il codice che userei ma non so come fare il bit di cancellazione.E 'possibile "cancellare" l'area spazzolata di un grafico in lucido?

library(ggplot2) 
library(shiny) 

runApp(shinyApp(
    ui = fluidPage(
    plotOutput("plot", 
       brush = brushOpts("plotBrush", delay = 5000)), 
    actionButton("clear", "Clear") 
), 
    server = function(input, output, session) { 
    values <- reactiveValues(brush = NULL) 

    output$plot <- renderPlot({ 
     ggplot(cars, aes(speed, dist)) + geom_point() 
    }) 

    brush <- reactive({ 
     input$plotBrush 
    }) 

    observeEvent(input$clear, { 
     cat(str(brush())) 
     # clear the brushed area 
    }) 
    } 
)) 

risposta

0

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 
    }) 
    } 
)) 
5

mi trovo in una situazione simile in cui ho più bisogno pennelli e un pulsante per "cancellare il mondo". Non ho trovato un modo ufficiale per rimuovere il div pennello con il codice R, ma si scopre c'è questo pacchetto impressionante chiamato shinyjs;)

library(shiny) 
library(shinyjs) 

ui <- fluidPage(
    useShinyjs(), 
    actionButton("clear", "Clear brush"), 
    fluidRow(
    column(
     width = 6, 
     plotOutput("p1", brush = brushOpts("b1")) 
    ), 
    column(
     width = 6, 
     plotOutput("p2", brush = brushOpts("b2")) 
    ) 
), 
    fluidRow(
    column(
     width = 6, 
     verbatimTextOutput("brush1") 
    ), 
    column(
     width = 6, 
     verbatimTextOutput("brush2") 
    ) 
) 
) 

server <- function(input, output) { 

    values <- reactiveValues(
    brush1 = NULL, 
    brush2 = NULL 
) 

    # update reactive values when input values change 
    observe({ 
    values$brush1 <- input$b1 
    values$brush2 <- input$b2 
    }) 

    # display brush details 
    output$brush1 <- renderPrint({ 
    values$brush1 
    }) 

    output$brush2 <- renderPrint({ 
    values$brush2 
    }) 

    # clear brush values and remove the div from the page 
    observeEvent(input$clear, { 
    values$brush1 <- NULL 
    values$brush2 <- NULL 
    runjs("document.getElementById('p1_brush').remove()") 
    runjs("document.getElementById('p2_brush').remove()") 
    }) 

    output$p1 <- renderPlot({ 
    input$clear 
    m <- brushedPoints(mtcars, values$brush1, allRows = TRUE) 
    qplot(data = m, wt, mpg, colour = selected_) + 
     theme(legend.position = "none") 
    }) 

    output$p2 <- renderPlot({ 
    input$clear 
    m <- brushedPoints(mtcars, values$brush2, allRows = TRUE) 
    qplot(data = m, wt, mpg, colour = selected_) + 
     theme(legend.position = "none") 
    }) 

} 

shinyApp(ui, server) 

IMO, lucido in realtà dovrebbe fornire qualcosa di simile:

clearBrush <- function(id) { 
    shinyjs::runjs(sprintf("document.getElementById('%s_brush').remove()", id)) 
} 
+1

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 –

5

A partire da Shiny version 0.14, è possibile utilizzare l'oggetto session per reimpostare il pennello di un grafico.

Di seguito è una semplice app Shiny dimostrando l'uso di session$resetBrush(<BRUSH_ID>) per cancellare una regione spazzolata. L'app consente di evidenziare una regione di punti o rimuovere la regione spazzolata mantenendo i punti evidenziati o rimuovendo la regione spazzolata e ripristinando il colore dei punti.

Vedere a metà strada a https://shiny.rstudio.com/reference/shiny/latest/session.html per la documentazione ufficiale.

library(shiny) 
library(ggplot2) 

shinyApp(
    ui = fluidPage(
    plotOutput(
     outputId = "plot", 
     brush = brushOpts(
     id = "plotBrush", 
     delay = 5000 
    ) 
    ), 
    actionButton("clearBrush", "Clear brush"), 
    actionButton("resetPlot", "Reset plot") 
), 
    server = function(input, output, session) { 
    output$plot <- renderPlot({ 
     ggplot(mtcars, aes(wt, mpg)) + 
     geom_point() + 
     geom_point(
      data = brushedPoints(mtcars, brush), 
      color = "#79D8CB", 
      size = 2 
     ) 
    }) 

    brush <- NULL 
    makeReactiveBinding("brush") 

    observeEvent(input$plotBrush, { 
     brush <<- input$plotBrush 
    }) 

    observeEvent(input$clearBrush, { 
     session$resetBrush("plotBrush") 
    }) 

    observeEvent(input$resetPlot, { 
     session$resetBrush("plotBrush") 
     brush <<- NULL 
    }) 
    } 
) 
+0

Grazie, questo mi ha aiutato molto e dovrebbe essere la risposta attualmente accettata! – sedot

Problemi correlati