I am not able to create a ggplot in Shiny Application.

My module example is running fine but I am getting the error in app.R because of the reactive value for df_metric, how can I make this work?

Any help in this regard is appreciated. The code of the module and app.R is attached below.

I start with helpers.R file where my time_plot function exists

library(tidyverse)

time_plot <- function(df, date_var, y_var) {
  
  ggplot(df) +
    aes(
      x = .data[[date_var]],
      y = .data[[y_var]]
    ) +
    geom_line() +
    theme_minimal()
}
source("R/helpers.R")

# plot module ----

plot_UI <- function(id) {
  ns <- NS(id)
  tagList(
    fluidRow(
      column(11, plotOutput(NS(id, "plot"))),
    )
  )
}

plot_Server <- function(id, df, date_var, y_var) {
  moduleServer(
    id,
    function(input, output, session) {
      
      # plot the time series plot
      plot <- reactive({time_plot(df, date_var, y_var)})
      
      output$plot <- renderPlot({plot()})
    }
  )
}



plot_demo <- function() {
  
  df <- data.frame(day = 1:30, arr_delay = 1:30)
  
  ui <- fluidPage(
    plot_UI(id = "x")
    )
  
  server <- function(input, output, session) {
    plot_Server(id = "x", df =  df, date_var = "day", y_var = "arr_delay")
  }
  
  shinyApp(ui = ui, server = server)
  
}

My app in app.R looks like

ui  <-  fluidPage(tabPanel("Performance Over Time",
             sidebarLayout(
               sidebarPanel(
                 selectInput("metrics", "Metrics", choices = c("OTIF", "OT", "IF"),
                             selected = 1)
                 ),
               mainPanel(
                 plot_UI("metrics")
                 )
               )
             )

server <- function(input, output, session) {
  
  # metrics table in Introduction
  output$metricstable <- renderTable(metrics)
  
  # Graph in Performance Over Time ----
  
  # collect metric by user
  df_metric <- reactive({input$metrics})
  
  plot_Server(id = "metrics", df = otif_ot_if_by_day, date_var = "order_placement_date", y_var = df_metric)
  
  
}

shinyApp(ui, server)

I am also attaching a snap shot of the error in Shiny App.

The error says the following in R console

Warning: Error in geom_line: Problem while computing aesthetics.
:information_source: Error occurred in the 1st layer.
Caused by error in FUN():
! Unknown input:
! Unknown input:

Could you format this into a reproducible example? That is a set of code or rstudio.cloud project that folks can easily get up and running to replicate your issue? Currently, this is only part of a shiny app.

IF you aren't familiar with best practices for shiny reprexes, check out

This will make it easier for folks to replicate your issue and offer suggestions to solve it.

@nirgrahamuk I have included complete app with minimal example. My module is running fine when in isolation but it has error when I put it in the app.R file. I am not understanding the reason.

You seem to require y_var to be reactive, so you need to treat it like a reactive in your module server

plot_Server <- function(id, df, date_var, y_var) {
  moduleServer(
    id,
    function(input, output, session) {
      
      # plot the time series plot
      plot <- reactive({time_plot(df, date_var, y_var())})
      
      output$plot <- renderPlot({plot()})
    }
  )
}

@nirgrahamuk The same error was coming. I fixed the error by including brackets in app.R

 plot_Server(id = "metrics", df = otif_ot_if_by_day, date_var = "order_placement_date", y_var = df_metric())

Thanks for your help.

Thats good if youre satisfied but i think this wouldnt react

Yes you are right. Its not reactive. What should I do to make it reactive?

My original idea worked when i made an example from the parts you provided

How can I make y_var reactive?

I am really stuck here.

If its like this, then its reactive

This gives an error in the plot.

If I make it y_var = df_metric() it plots the plot but its not reactive.

What does your plot_server look like, did you not adopt this ?

I opted for this. It is plotting the plot but it is not reactive according to metric.

This topic was automatically closed 54 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.