Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessing objects outside of renderPlot() #143

Closed
csarven opened this issue Apr 8, 2013 · 2 comments
Closed

Accessing objects outside of renderPlot() #143

csarven opened this issue Apr 8, 2013 · 2 comments

Comments

@csarven
Copy link

csarven commented Apr 8, 2013

Unless I'm doing something wrong, I can't seem to use objects defined outside of renderPlot(). For example, the following works:

library(shiny)
library(datasets)
library(ggplot2)

shinyServer(function(input, output) {
  output$plot <- renderPlot({
    g <- ggplot(mtcars, aes(mtcars[1][,], mtcars[2][,])) + geom_bar(stat="identity")
    print(g)
  })
})

However, the following does not:

library(shiny)
library(datasets)
library(ggplot2)

mpgData <- mtcars

shinyServer(function(input, output) {
  output$plot <- renderPlot({
    g <- ggplot(mpgData, aes(mpgData[1][,], mpgData[2][,])) + geom_bar(stat="identity")
    print(g)
  })
})

Gives "Error: object 'mpgData' not found"

@wch
Copy link
Collaborator

wch commented Apr 8, 2013

This is probably due to a bug in ggplot2. See tidyverse/ggplot2#743 If this is the source of the problem, the workaround is to pass environment = environment() to ggplot().

As a general note, it's better to name the columns of the data frame when you use aes(), instead of pulling out columns as you've done. Something like this:

  ggplot(mpgData, aes(x=mpg, y=cyl)) + geom_bar(stat="identity")

I believe that doing things this way will also avoid the ggplot2 bug.

@csarven
Copy link
Author

csarven commented Apr 8, 2013

Thanks for the notice: environment = environment() resolved the issue!

I agree that it is better to name x,y values, I only simplified for the example here. Naming them doesn't resolve the issue however, environment = environment() is the only way I can see it working at the moment.

@csarven csarven closed this as completed Apr 8, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants