Adjust shiny to plot

How do I plot the image on my shiny app. I tried to adjust my output$Graph, but unfortunately I couldn't. Can you help me?

Thank you very much!

library(shiny)
library(shinythemes)

function.cl<-function(df,date){
  df <- structure(
    list(Update = c("2021-08-21","2021-08-21","2021-08-21","2021-08-21","2021-08-21"),
         date = c("2021-04-01","2021-05-02","2021-06-03","2021-07-04","2021-08-05"),
         D1 = c(3,1,4,5,6), DR01 = c(2,4,5,6,7), DR02 = c(6,3,2,6,1),DR03 = c(5,2,8,9,7),
         DR04 = c(3,5,3,3,7)),class = "data.frame", row.names = c(NA, -5L))
  
  historic<-subset(df,df$date<df$Update) 
  
  average<-mean(historic$D1) 
  standard<-sd(historic$D1)
  evol<-subset(df,df$date<df$Update) 
  median_ocup<-sapply(evol[startsWith(names(evol), "DR")], median)
  
  Plot1<-plot(median_ocup[1:10],xlab="Days",ylab="Number",ylim=c(1, 6))
  abline(h=c(average-standard, average, average+standard), col="red", lwd=5)
  
  return(list(
    "Plot1" = Plot1
    ))
  
  }   
  
  
  
ui <- fluidPage(
  
  ui <- shiny::navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
                          br(),
                          
                          tabPanel("",
                                   sidebarLayout(
                                     sidebarPanel(
                                       
                                     
                                       plotOutput("Graph")
                                       br(),
                                     ),
                                     
                                     mainPanel(
                                     ))
                          )))


server <- function(input, output,session) {
  data <- reactive(function.cl())
  
  output$Graph <- renderPlot({
    function.cl([[1]])
    Plot1<-plot(median_ocup[1:10],xlab="Days",ylab="Number",ylim=c(1, 6))
    abline(h=c(average-standard, average, average+standard), col="red", lwd=5)
    })
  

}

shinyApp(ui = ui, server = server)

The version below does output a plot. Things I changed:

  1. I removed the df and date arguments from function.cl because the function makes its own data.
  2. I added a missing comma after plotOutput("Graph")
  3. In the server code, I changed function.cs([[1]]) to function.cl()[[1]]
  4. I commented out the plot() and abline() calls in renderPlot()
library(shiny)
library(shinythemes)

function.cl<-function(){
  df <- structure(
    list(Update = c("2021-08-21","2021-08-21","2021-08-21","2021-08-21","2021-08-21"),
         date = c("2021-04-01","2021-05-02","2021-06-03","2021-07-04","2021-08-05"),
         D1 = c(3,1,4,5,6), DR01 = c(2,4,5,6,7), DR02 = c(6,3,2,6,1),DR03 = c(5,2,8,9,7),
         DR04 = c(3,5,3,3,7)),class = "data.frame", row.names = c(NA, -5L))
  
  historic<-subset(df,df$date<df$Update) 
  
  average<-mean(historic$D1) 
  standard<-sd(historic$D1)
  evol<-subset(df,df$date<df$Update) 
  median_ocup<-sapply(evol[startsWith(names(evol), "DR")], median)
  
  Plot1<-plot(median_ocup[1:10],xlab="Days",ylab="Number",ylim=c(1, 6))
  abline(h=c(average-standard, average, average+standard), col="red", lwd=5)
  
  return(list(
    "Plot1" = Plot1
  ))
  
}   



ui <- fluidPage(
  
  ui <- shiny::navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
                          br(),
                          
                          tabPanel("",
                                   sidebarLayout(
                                     sidebarPanel(
                                       
                                       
                                       plotOutput("Graph"),
                                       br(),
                                     ),
                                     
                                     mainPanel(
                                     ))
                          )))


server <- function(input, output,session) {
  data <- reactive(function.cl())
  
  output$Graph <- renderPlot({
    function.cl()[[1]]
    #Plot1<-plot(median_ocup[1:10],xlab="Days",ylab="Number",ylim=c(1, 6))
    #abline(h=c(average-standard, average, average+standard), col="red", lwd=5)
  })
  
  
}

shinyApp(ui = ui, server = server)

Thank you very much for your help and for explaining what you did.

Best Regards!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.