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!

4
  • In the code above mydata<-reactive {( the { and ( need to switch places. Is that the case in your actual code? Commented Aug 28, 2014 at 18:31
  • sorry, yes, it's correct in my actual code. I'll change above. Commented Aug 28, 2014 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 and it still doesn't help. Commented Aug 28, 2014 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. Commented Aug 28, 2014 at 18:35

2 Answers 2

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)
    })
  })
)
Sign up to request clarification or add additional context in comments.

5 Comments

(+1). I didn't know you could call runApp like this. Cool.
@MrFlick, It is handy for small apps but I still generally prefer to keep the two files separate especially when the app becomes larger.
I totally agree. I was mostly thinking of it in the context of answering StackOverflow questions ;)
I am using those extra variables later in the ggplot call.
The df is imported, it's not global.
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.

3 Comments

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.
Note that V8 is a COLUMN of df to begin with, so ggplot should recognize that.
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.