Help with ggplot and Shiny

1

In normal R I generate a very simple graphic with ggplot that I would like to show in Shiny but I can not make it look and I do not know what the error is.

Outside the Server declared the plot but I do not know how to use it. This is the code:

mydat <- read.csv(file = "queryCoca.csv")
datos = as.data.frame(mydat)

ventasMes <- tapply(datos$total_price, datos$aniomes, sum)
ventasMes <- as.data.frame(ventasMes)
ventasMes["Aniomes"]<-rownames(ventasMes)

plotVentas <- ggplot(ventasMes, aes(Aniomes, ventasMes, group = 1)) +
  geom_point(colour="#000099") +
  geom_line(colour="#000099")

Obtaining the following graph:

But since I want to integrate it in the server I do not achieve it:

server<-function(input, output, session){
  output$plot1 <- renderPlot({
    data<-ggplot(ventasMes, aes(Aniomes, ventasMes, group = 1)) +
      geom_point(colour="#000099") +
      geom_line(colour="#000099")
    print(data)
  })

}

I do not have any results. Any help?

    
asked by Jose Incera 22.03.2018 в 01:26
source

1 answer

0

Some information and comments regarding your question:

  • In principle it seems that the problem is in doing data<-ggplot(v... since renderPlot should return the object plot directly and when assigning it to the variable data you do not do it. Test returning data or directly ggplot(...
  • Have you implemented the ui function to define the interface?
  • Have you called shinyApp(ui, server) at the end of your Script?
  • You have renamed the script as app.R so that eventually RStudio will detect it as a shinny application

Just in case this is the basic skeleton of a Shinny app similar to your question.

library(shiny)
library(ggplot2)

ui <- fluidPage(
    titlePanel(title=h4("Prueba de GGplot + Shinny", align="center")),
    mainPanel(plotOutput("plot"))
)

server<-function(input, output, session){

    output$plot <- renderPlot({
        ggplot(mtcars, aes(mpg, disp, group = 1)) +
            geom_point(colour="#000099") +
            geom_line(colour="#000099")
    })

}

shinyApp(ui, server)

Result:

    
answered by 22.03.2018 в 04:38