Thomas Luechtefeld
Thomas Luechtefeld

Reputation: 1456

ggplot and rshiny plot is not refreshed

I have an output plot that is dependent on a reactive function called datasetInput. Whenever I change the input variable the output function updates, but the plot I see on the client does not end up changing. I output the data used by ggplot to generate the plot and the data is changing. I'm not sure whats going on.

datasetInput <- reactive({    
    data <- input$globalData
    table <- c()
    ...
    table
})

output$plot <- renderPlot({    
    table <- datasetInput()   
    cat('32hr: ',unlist(table[which(table$group=='32hr'),3]),'\n')     
    cat('24hr: ',unlist(table[which(table$group=='24hr'),3]),'\n')     
    range <- max(table$centroidDistances.RG) - min(table$centroidDistances.RG)
    cat('range: ',range,'\n')
    plot <- ggplot(table,aes(x=table$centroidDistances.RG,fill=table$group)) + 
        geom_histogram(aes(y=..density..),pos="dodge") +
        geom_density(alpha=0.2)
    print(plot)
},height=300,width=600)

I have not seen this problem before, how can I get the client to change its output when output$plot changes (is this a specific problem to ggplot?)

Upvotes: 3

Views: 2042

Answers (1)

Thomas Luechtefeld
Thomas Luechtefeld

Reputation: 1456

This problem appears to be fixed by setting environment = environment() within ggplot:

ggplot(table,aes(x=table$centroidDistances.RG,fill=table$group),environment=environment()) + 
  geom_histogram(aes(y=..density..),pos="dodge",binwidth=range/30,minx=0,width=600) +
  geom_density(alpha=0.2)

Not sure why this fixes things, but including this environment setting makes the graph update as expected on the client side.

Upvotes: 3

Related Questions