1

I have a server.R file in the following form:

server.R

shinyServer(


  function(input, output, session) {    

    mydata<- reactive({
              df<- dataframe1
              variable1
              variable2
              list(df, variable1, variable2)
             
    })

  output$plot<- renderPlot({  

   p<-ggplot(mydata()$df, aes(y=V8, x = 1:nrow(mydata()$df), fill = V8))
   print(p)
   
   })
 })

My issue is that the call to ggplot, while it seems to recognize mydata$df(), it returns the error

Error in nrow(mydata()$df) : could not find function "mydata".

I am not sure where my syntax is wrong. Can anyone shed some light? Thanks!

Community
  • 1
  • 1
yahooligan8
  • 41
  • 2
  • 8
  • In the code above `mydata<-reactive {(` the `{` and `(` need to switch places. Is that the case in your actual code? – John Paul Aug 28 '14 at 18:31
  • sorry, yes, it's correct in my actual code. I'll change above. – yahooligan8 Aug 28 '14 at 18:32
  • I assume this may be related to the environment of ggplot2 inside aes(), but I put in the fix _environment = environment()_ as suggested [here](http://stackoverflow.com/questions/19531729/r-shiny-fill-value-not-passed-to-ggplot-correctly-in-shiny-server-error-obje) and it still doesn't help. – yahooligan8 Aug 28 '14 at 18:33
  • You might want to just use `renderPrint` or something similar to display `mydata()$df` in the ui. Then you can see if the issue is ggplot or the reactive. – John Paul Aug 28 '14 at 18:35

2 Answers2

1

To my knowledge, reactive shiny objects don't play well with lists. As it appears you aren't using 'variable1' and 'variable2' just omit them and just do the dataframe (which I assume has been made globally accessible and isn't imported?). It also could simply be calling the reactive before the ggplot call, but I err towards simplicity if not using those extra variables. A very quick example:

runApp(
  list(ui = basicPage(
    h1('Demo Shiny'),
    plotOutput("plot")
  )

  ,server = function(input, output) {
    mydata <- reactive({
      dataframe1 <- data.frame(cond = rep(c("A", "B"), each=10),
                               xvar = 1:20 + rnorm(20,sd=3),
                               yvar = 1:20 + rnorm(20,sd=3))
      dataframe1
    })

    output$plot = renderPlot({
      df <- mydata()
      p<-ggplot(df, aes(x=xvar, y = yvar)) + geom_point()
      print(p)
    })
  })
)
cdeterman
  • 19,630
  • 7
  • 76
  • 100
0

I'm going to shamless steal most of @charles code, but i think the problem in this case is actually your aes(). This seems to work

runApp(
  list(ui = basicPage(
    h1('Demo Shiny'),
    plotOutput("plot")
  )

  ,server = function(input, output) {
    mydata <- reactive({
            df <- data.frame( V8=sample(1:4, 20, replace=T))
            list(df=df, variable1=1, variable2=2)
    })

    output$plot = renderPlot({
      p<-ggplot(mydata()$df, aes(x=seq_along(V8), y = V8)) + geom_point()
      print(p)
    })
  })
)

The problem was referring to variables in your aes that were not in your data.frame that you passed to ggplot2. Here by making sure to include a proper variable from the df, we seem to be fine.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • I don't think that's the issue, because when I define the data frame INSIDE renderPlot(), the original call to ggplot2 works fine, provided I put in `environment = environment()` In other words, in this case, the call `p<-ggplot(df(), aes(y=V8,x = 1:nrow(df()), fill = V8), environment=environment() ) +geom_histogram()` works. – yahooligan8 Aug 28 '14 at 19:12
  • Note that V8 is a COLUMN of df to begin with, so ggplot should recognize that. – yahooligan8 Aug 28 '14 at 19:17
  • My point was the code that was causing the error was the code in the `aes()` (specifically `x=`), not the code in the first parameter of `ggplot`. By default the `aes` parameters are evaluated in the context of the data argument. If not found there, generally you walk up the definition environment to resolve the symbol. But in the way render plots are defined, it seems to change the "normal" environment chain. So adding an explicit environment would help, but generally its probably a better idea to have all the variables you want to plot inside the data.frame – MrFlick Aug 28 '14 at 21:17