2015-05-25 12 views
8

Desidero utilizzare un dataframe reattivo per mostrare più grafici e grafici. Ho un set di dati che mi piacerebbe poter filtrare. Ho trovato le giuste impostazioni del filtro, vorrei mostrare i dati su un numero di grafici diversi, che verrà aggiornato se le impostazioni del filtro vengono modificate.Come si costruisce un dataframe reattivo in R/lucido?

Questo potrebbe spiegare, quello che sto cercando di fare:

UI:

fluidPage(
    sidebarLayout(
    sidebarPanel(

     checkboxGroupInput("checkGroups", 
         label = "Include", choices = list("1 star" = 1, "2 star" = 2, 
                  "3 star" = 3, "4 star" = 4, 
                  "5 star" = 5), 
         selected = list(1, 2, 3, 4, 5)), 

     checkboxInput("checkbox", "Include Replies"), 

     actionButton("Button", "Update") 

    ), 
    mainPanel(

     showOutput("plot", "nvd3"), 

     showOutput("pieplot", "nvd3") 

    ) 
) 
) 

SERVER:

rating <- c(2, 3, 5, 4, 1, 5, 3, 1, 4) 
date_time <- c("2015-05-14", "2015-05-07", "2015-05-06", "2015-04-11", "2015-01-07", "2014-12-06", "2014-04-11", "2014-01-07", "2013-12-06") 
repliesOnly <- c(TRUE, FALSE, TRUE, TRUE, FALSE, TRUE, FALSE, FALSE, TRUE) 
data <- data.frame(rating, date_time, repliesOnly) 

function(input, output, session) { 

    load("data.Rdata") 

    newdata <- reactive({ 

    filter_data <- data 

    filter_data <- filter_data %>% filter(rating %in% input$checkGroups) 

    filter_data <- filter_data %>% filter(repliesOnly %in% input$checkbox) 

    return(filter_data) 

    }) 

    output$plot <- renderChart({ 

    plot <- nPlot(rating ~ date_time, data = newdata, 
        type = "multiBarHorizontalChart", dom = 'plot') 
    return(plot) 

    }) 

    output$pieplot <- renderChart({ 

    pieplot <- nPlot(rating ~ date_time, data = newdata, 
        type = "pieChart", dom = 'pieplot') 
    return(pieplot) 

    }) 

} 

si può fare? Naturalmente posso solo includere il filtro per ogni output grafico, ma il mio set di dati è piuttosto grande e il mio filtro è piuttosto complesso, quindi se dovesse calcolarlo per ogni grafico ci vorrà per sempre.

Tutti gli aiuti sono molto apprezzati!

+0

Da dove viene la funzione 'showOuput'? Assicurati che il tuo esempio sia riproducibile prima di pubblicarlo. – nrussell

+3

Sembra che tutto ciò che dovete fare sia aggiungere qualcosa come 'dat <- newdata()' nei vostri comandi di output output e usare 'dat' nei vostri comandi plot. – jenesaisquoi

+4

in realtà penso che potresti semplicemente aggiungere '()' dopo 'newdata' come hai scritto, devi solo chiamare' newdata'. – jenesaisquoi

risposta

6

Grazie per l'aiuto - questo ha funzionato:

SERVER:

library(shiny) 
library(rCharts) 

rating <- c(2, 3, 5, 4, 1, 5, 3, 1, 4) 
date_time <- c("2015-05-14", "2015-05-07", "2015-05-06", "2015-04-11", "2015-01-07", "2014-12-06", "2014-04-11", "2014-01-07", "2013-12-06") 
repliesOnly <- c(TRUE, FALSE, TRUE, TRUE, FALSE, TRUE, FALSE, FALSE, TRUE) 
data <- data.frame(rating, date_time, repliesOnly) 
data$rating <- as.character(data$rating) 


function(input, output, session) { 

    load("data.Rdata") 

    datadata <- data 
    makeReactiveBinding("datadata") 

    newData <- reactive({ 

    input$Button 
    isolate({ 

     datadata <- data 

     datadata <- subset(datadata, rating %in% input$checkGroups) 


    }) 

    }) 

    output$plot <- renderChart({ 

    datadata <- newData() 

    plot <- nPlot(rating ~ date_time, data = datadata, 
        type = "multiBarHorizontalChart", dom = 'plot') 
    return(plot) 

    }) 

    output$pieplot <- renderChart({ 

    datadata <- newData() 

    pieplot <- nPlot(rating ~ date_time, data = datadata, 
        type = "pieChart", dom = 'pieplot') 
    return(pieplot) 

    }) 

} 
1

Grazie per questo esempio; Ho anche bisogno di un filtro reattivo per i dataframe. Dopo aver combattuto 2 ore con il seguente errore:

cannot coerce class ""reactive"" to a data.frame 

il tuo post mi ha aiutato a risolvere il problema. Oserei pubblicare una versione semplificata del tuo esempio, più facile da riprodurre (no showOutput o readChart chiamate), per riferimento futuro ad altri utenti brillanti.

N.B .: Uso l'applicazione per file singolo.

library(shiny) 

rating <- c(2, 3, 5, 4, 1, 5, 3, 1, 4) 
date_time <- c("2015-05-14", "2015-05-07", "2015-05-06", "2015-04-11", 
"2015-01-07", "2014-12-06", "2014-04-11", "2014-01-07", "2013-12-06") 
repliesOnly <- c(TRUE, FALSE, TRUE, TRUE, FALSE, TRUE, FALSE, FALSE, TRUE) 
data <- data.frame(rating, date_time, repliesOnly) 
data$rating <- as.character(data$rating) 

ui <- fluidPage(
    sidebarLayout(
    sidebarPanel(

    checkboxGroupInput("checkGroups", 
        label = "Include", choices = c("1 star" = 1, 
                 "2 star" = 2, 
                 "3 star" = 3, 
                 "4 star" = 4, 
                 "5 star" = 5), 
        selected = list(1, 2, 3, 4, 5)) 
), 

mainPanel(
    tableOutput("view") 
    ) 
    ) 
    ) 

server <- function(input, output, session) { 
    newData <- reactive({ 
     data <- subset(data, rating %in% input$checkGroups) 
}) 

output$view <- renderTable({ newData() }) 

} 

shinyApp(ui = ui, server = server) 
Problemi correlati