2016-09-05 5 views
1

Ich habe diese Shiny App aus Tutorial und Beispielen zusammengestellt, und ich bin festgefahren. Mein Ziel ist es, den Plot reaktiv zu machen, so dass die Datenpunkte in 'uval $ df' geplottet werden, was bedeutet, dass ausgewählte Punkte aus dem Graph entfernt werden und nicht zweimal ausgewählt werden können. Wie mache ich das? (Ich habe das Gefühl, dass etwas in meinem Grundverständnis fehlt)Shiny Plotly reaktives Datenplot

Danke!

library(shiny) 
library(plotly) 
library(dplyr) 

ui <- fluidPage(
    fluidRow(
    column(12,plotlyOutput("plot"), 
      verbatimTextOutput("txtout1"), 
      verbatimTextOutput("txtout2"), 
      verbatimTextOutput("txtout3")) 
) 
) 

server <- function(input, output, session) { 
x<-c(1,2,34,2,1,23,24) 
y<-c(10,20,30,40,50,60,70) 
df<-data.frame(x,y) 
vector.is.empty <- function(x) return(length(x) ==0) 

K <-reactive({ 
    event_data("plotly_selected",source = "B") 
}) 

M<-reactive({ 
    K()[,c("x","y")] 
}) 

values <- reactiveValues() 
values$df <- data.frame(x = numeric(0), y = numeric(0)) 
newEntry <- observeEvent(K(),priority = 1,{ 
    new0 <- isolate(M()) 
    isolate(values$df <- rbind(values$df, new0)) 
}) 

uval <- reactiveValues() 
uval$df <- df 
newEntry1 <- observeEvent({values$df},priority = 2,{ 
    new1 <- isolate(data.frame(values$df)) 
    isolate(uval$df <- setdiff(df,new1)) 
}) 

output$plot <- renderPlotly({ 
    plot_ly(x = df$x, y = df$y, mode = "markers",source="B") %>% 
    layout(dragmode = "select", title = "Original Plot", font=list(size=10)) 
}) 

output$txtout1 <- renderPrint({ 
    if(vector.is.empty(K())) "Click and drag across points" else M() 
}) 

output$txtout2 <- renderPrint({ 
    uval$df 
}) 

output$txtout3 <- renderPrint({ 
    values$df 
}) 

} 

shinyApp(ui, server, options = list(display.mode = "showcase")) 

Antwort

1

Einfach, wie ich dachte.

plot_ly(uval$df, x = x, y = y, mode = "markers",source="B")