Cannot produce ggplot2 in Shiny

185 views Asked by At

Below is my code. I am able to produce the ecdf plots in R studio but not when i put it all together in a shiny app like I have below:

Server.R

library(shiny)
library(ggplot2)
shinyServer(function(input, output) {
  datasetInput <- reactive({
    mydf = data.frame(
      a = rnorm(100, 0, 1),
      b = rnorm(100, 2, 1),
      c = rnorm(100, -2, 0.5)
    )

    mydf_m = melt(mydf)
    mydf_m <- ddply(mydf_m,.(variable),transform, ecd = ecdf(value)(value))

  })

  output$myplot <- renderGvis({

    p<-(ggplot(mydf_m,aes(x = value, y = ecd)) + 
      geom_line(aes(group = variable,colour = variable)))
    print(p)

  })
})

ui.R

library(shiny)
library(ggplot2)

shinyUI(fluidPage(
  titlePanel("title panel"),

  sidebarLayout(
    sidebarPanel( "sidebar panel"),
    mainPanel(
      tabsetPanel(
        tabPanel("Tab 1", h4("Head 1"),plotOutput("myplot"))
      )
    )
  )
))

What am I doing wrong?

1

There are 1 answers

1
Rorschach On BEST ANSWER

You have a reactive data set in datasetInput but you aren't using it in your plotting function. In your ggplot call just replace mydf_m with datasetInput(). I also replaced renderGvis with renderPlot and return the data from the reactive datasetInput. The server is then

server <- shinyServer(function(input, output) {
    datasetInput <- reactive({
        mydf = data.frame(
            a = rnorm(100, 0, 1),
            b = rnorm(100, 2, 1),
            c = rnorm(100, -2, 0.5)
        )

        mydf_m = melt(mydf)
        mydf_m <- ddply(mydf_m,.(variable),transform, ecd = ecdf(value)(value))
        mydf_m
    })

    output$myplot <- renderPlot({
        p <- ggplot(datasetInput(), aes(x = value, y = ecd)) + 
              geom_line(aes(group = variable, colour = variable))
        print(p)  # just `p`
        })
})