Raghavan vmvs
Raghavan vmvs

Reputation: 1265

plotly not generating output in R shiny console

I have created the following dataframe and shiny app

 # Import packages
 library(readxl)
 require(ggplot2)
 require(janitor)
 require(lubridate)
 require(shiny)
 require(plotly)
 require(reshape2)

Function from stack overflow

   #generate date randomly
   rdate <- function(x,
              min = paste0(format(Sys.Date(), '%Y'), '-01-01'),
              max = paste0(format(Sys.Date(), '%Y'), '-12-31'),
              sort = TRUE) {

     dates <- sample(seq(as.Date(min), as.Date(max), by = "day"), x, replace 
     = TRUE)
    if (sort == TRUE) {
     sort(dates)
    } else {
dates
  }

 }

Next we create a dataframe

  DF<-as.data.frame("Date"<-rdate(100))
  DF$variable<-LETTERS[seq( from = 1, to = 10 )]

  DF$Value<-round(runif(1:nrow(DF),min = 10, max = 50))

    # subset the dataframe
  DF<-DF[,c(2:4)]
  DF

    # Write to csv
    write.csv(DF, file = "Book1.csv", col.names = F)

Next we create the shiny APP

    # UI creation

    UI<-fluidPage(fileInput("file", "Browse",
                    accept = c("text/csv",
                               "text/comma-separated-values,text/plain",
                               ".csv")),
          #selectInput(inputId = "Speciesname", label = "Name",choices = 
          #NULL,selected = NULL),
          plotOutput(outputId = "plot1" ))


    # Server creation
    Server<-function(input, output, session){
    output$plot1<-renderPlot({

   infile <- input$file
   if (is.null(infile)) {
  # User has not uploaded a file yet
  return(NULL)
   }

     Book1 <-  read.csv(input$file$datapath, stringsAsFactors = F)


     Book1<-data.frame(Book1)



     Book2<-remove_empty_rows(Book1)


     ggplot(DF, aes(x = Date, y = Value, colour = variable)) + 
      geom_line() + 
      ylab(label="Number of Sales") + 
      xlab("Sales Week")

     }


   )


  }
  shinyApp(UI, Server)

In this App, the graph is generated in the output namey the Shiny UI.

When I make the following change in the server, the plot is generated not in the shiny UI but in the r studio console

     Book2<-remove_empty_rows(Book1)


     P<- ggplot(DF, aes(x = Date, y = Value, colour = variable)) + 
      geom_line() + 
      ylab(label="Number of Sales") + 
      xlab("Sales Week")
      return(ggplotly(p))

I am unable to obtain the plot in the shiny UI console. I request someone to help me. I am unable to locate the error

Upvotes: 0

Views: 631

Answers (1)

forestfanjoe
forestfanjoe

Reputation: 412

If you're using plotly in shiny apps, plotOutput() and renderPlot() won't work. You need to have plotlyOutput() and renderPlotly(). The code below should work. I've taken out the fileInput and read.csv to make it a bit easier.

library(shiny)
library(plotly)

rdate <- function(x,
                  min = paste0(format(Sys.Date(), '%Y'), '-01-01'),
                  max = paste0(format(Sys.Date(), '%Y'), '-12-31'),
                  sort = TRUE) {

    dates <- sample(seq(as.Date(min), as.Date(max), by = "day"), x, replace 
                    = TRUE)
    if (sort == TRUE) {
        sort(dates)
    } else {
        dates
    }

}

UI<-fluidPage(
              plotlyOutput(outputId = "plot1" ))

# Define server logic required to draw a histogram
server <- function(input, output) {

   output$plot1 <- renderPlotly({
      # generate bins based on input$bins from ui.R
       DF<-data.frame(Date = rdate(100))
       DF$variable<-LETTERS[seq( from = 1, to = 10 )]

       DF$Value<-round(runif(1:nrow(DF),min = 10, max = 50))

       g <- ggplot(DF, aes(x = Date, y = Value, colour = variable)) + 
           geom_line() + 
           ylab(label="Number of Sales") + 
           xlab("Sales Week") 

       ggplotly(g)

   })
}

# Run the application 
shinyApp(ui = UI, server = server)

Upvotes: 2

Related Questions