2016-03-25 7 views
8

Ich habe eine shiny App und ich möchte eine ggplot mit einem brush am Anfang, so dass der Benutzer nicht bestimmte Bereich von Interesse jedes Mal, wenn die App startet ausgewählt werden muss. Später kann der Benutzer natürlich einen anderen Bereich auswählen. Hier ist ein Beispiel zu beginnen mit:Ist es möglich, Pinsel in ggplot in einer glänzenden App zu initialisieren?

library(shiny) 
library(ggplot2) 

runApp(shinyApp(
    ui = fluidPage(plotOutput('plotA', brush = brushOpts(id = 'plotA_brush')), 
       plotOutput('plotZ')), 
    server = function(input, output, session) { 
    pollData <- reactivePoll(60 * 1000, session, 
          checkFunc = function(){ Sys.time() }, 
          valueFunc = function(){ data.frame(x = 1:100, y = cumsum(rnorm(100)))}) 
    output$plotA <- renderPlot({ 
     dt <- pollData() 
     ggplot(dt, aes(x, y)) + geom_line() 
    }) 
    ranges <- reactiveValues(x = NULL, y = NULL) 
    observe({ 
     brush <- input$plotA_brush 
     if(!is.null(brush)) { 
     ranges$x <- c(brush$xmin, brush$xmax) 
     ranges$y <- c(brush$ymin, brush$ymax) 
     } else { 
     ranges$x <- NULL 
     ranges$y <- NULL 
     } 
    }) 
    output$plotZ <- renderPlot({ 
     dt <- pollData() 
     ggplot(dt, aes(x, y)) + geom_line() + coord_cartesian(xlim = ranges$x, ylim = ranges$y) 
    }) 
    } 
)) 
+0

initialisieren es aber zu dem, was? Es beginnt "initialisiert" für das gesamte Gebiet. Ich vermute, Sie wollen es auf einen bestimmten Teil initialisieren, aber welchen Teil? Alles andere, was kleiner ist als das Ganze? –

+0

Ja, alles andere ist kleiner als die ganze Sache. In diesem speziellen Beispiel möchte ich sagen, dass ich will, dass x im Bereich von 25 bis 75 und y vom Minimum bis zum Maximum liegt. –

Antwort

0

Ja, ist es möglich.

Im folgenden Code habe ich nur ein paar Zeilen hinzugefügt. Zuerst habe ich set.seed(42) hinzugefügt, damit die Grafiken reproduzierbar sind. Zweitens gibt es eine dput(brush), die auskommentiert wurde. Dies war hilfreich, um den ursprünglichen Pinsel zu identifizieren, den ich haben wollte. Schließlich, in der observe Umgebung, die ranges steuert, habe ich einen ifelse gesetzt, um den Standard brush zu verwenden, ist es ein NULL Wert vom input$plotA_brush Objekt.

library(shiny) 
library(ggplot2) 
set.seed(42) 

runApp(shinyApp(
    ui = fluidPage(plotOutput('plotA', brush = brushOpts(id = 'plotA_brush')), 
       plotOutput('plotZ')), 
    server = function(input, output, session) { 


    pollData <- reactivePoll(60 * 1000, session, 
          checkFunc = function(){ Sys.time() }, 
          valueFunc = function(){ data.frame(x = 1:100, y = cumsum(rnorm(100)))}) 
    output$plotA <- renderPlot({ 
     dt <- pollData() 
     ggplot(dt, aes(x, y)) + geom_line() 
    }) 
    ranges <- reactiveValues(x = NULL, y = NULL) 
    observe({ 
     if (is.null(input$plotA_brush)) { 
     brush <- structure(list(xmin = 14.313925002001, xmax = 39.942241912585, ymin = 1.1077251080591, ymax = 5.5028180250535, mapping = structure(list(x = "x", y = "y"), .Names = c("x", "y")), domain = structure(list(left = -3.95, right = 104.95, bottom = -4.07771077213569, top = 9.69030145758825), .Names = c("left", "right", "bottom", "top")), range = structure(list(left = 32.3904099935947, right = 674.020527857828, bottom = 368.859578048224, top = 5.47945189149413), .Names = c("left", "right", "bottom", "top")), log = structure(list(x = NULL, y = NULL), .Names = c("x", "y")), direction = "xy", brushId = "plotA_brush", outputId = "plotA"), .Names = c("xmin", "xmax", "ymin", "ymax", "mapping", "domain", "range", "log", "direction", "brushId", "outputId")) 
     } else { 
     brush <- input$plotA_brush 
     } 
     # dput(brush) # Useful for finding the initial brush 
     if(!is.null(brush)) { 
     ranges$x <- c(brush$xmin, brush$xmax) 
     ranges$y <- c(brush$ymin, brush$ymax) 
     } else { 
     ranges$x <- NULL 
     ranges$y <- NULL 
     } 
    }) 
    output$plotZ <- renderPlot({ 
     dt <- pollData() 
     ggplot(dt, aes(x, y)) + geom_line() + coord_cartesian(xlim = ranges$x, ylim = ranges$y) 
    }) 
    } 
)) 

Die anfängliche Start Seite sieht wie folgt aus:

enter image description here

Verwandte Themen