Issue with Rendering Plot in Shiny Application

Hello everyone,

I have been working on a Shiny application which is designed to calculate OR and DR from input data, then plot the resulting DR with its minimum and maximum values. The application runs perfectly in my local R environment, displaying both the calculated results and the plot as expected.

However, when I run the application in Shiny, an error occurs that states "An error has occurred. Check your logs or contact the app author for clarification." The calculated results are displayed correctly, but the plot does not render at all. I have checked my code thoroughly, and it seems to be fine with no apparent errors.

Here's a sample of the code I am using:

# Server side code
server <- function(input, output) {
  observeEvent(input$calculate, {
    # ...
    # Code for calculations
    # ...

    # Plotting
    output$plot <- renderPlot({
      ggplot() +
        geom_line(aes(x = 1:length(c(DR_min, DR, DR_max)), 
                      y = c(DR_min, DR, DR_max)), color = "blue") +
        geom_point(aes(x = 2, y = DR), color = "blue", size = 4) +
        geom_vline(aes(xintercept = 2), color = "red") +
        geom_vline(aes(xintercept = 2), ymin = 0, ymax = input$MID, color = "red", linetype = "dashed") +
        theme_minimal()
    })
  })
}
shinyApp(ui = ui, server = server)

I have checked the app logs but there aren't any clear indications as to why the plot is not rendering.

Has anyone else encountered this issue? If so, how did you resolve it? Any guidance or suggestions would be greatly appreciated.

Best regards,
Fernando

Welcome to the community @fernandotortosa! To help troubleshoot, are you able to provide code for the full app (i.e. UI and data as well)?

your ggplot-call does not include any data-source!?
Did you remove it (for the example) or forgot it?
Is the result of the calculations stored somewhere?
We need to see more of your code!

ui <- fluidPage(
titlePanel("Cálculo de OR y DR"),

sidebarLayout(
sidebarPanel(
numericInput("interv_Media", "MD (Grupo intervención):", 5.9),
numericInput("interv_SD", "SD (Grupo intervención):", 11.7),
numericInput("interv_n", "n (Grupo intervención):", 60),

  numericInput("control_Media", "MD (Grupo control):", 0.5),
  numericInput("control_SD", "SD (Grupo control):", 14.1),
  numericInput("control_n", "n (Grupo control):", 61),
  
  numericInput("MID", "MID:", 4),
  
  actionButton("calculate", "Calcular")
),

mainPanel(
  verbatimTextOutput("result"), plotOutput("plot")
)

)
)

server <- function(input, output) {
observeEvent(input$calculate, {
# Calcula Xi y Xc
Xi <- (input$MID - input$interv_Media) / input$interv_SD
Xc <- (input$MID - input$control_Media) / input$control_SD

# Calcula las probabilidades
prob_intervention <- 1 - pnorm(Xi, lower.tail = TRUE)
prob_control <- 1 - pnorm(Xc, lower.tail = TRUE)

# Calcula el OR y su error estándar
OR <- (prob_intervention / (1 - prob_intervention)) / (prob_control / (1 - prob_control))
SE_OR <- sqrt((1/input$interv_n) + (1/input$control_n))


IC_OR <- exp(log(OR) + c(-1, 1) * 1.96 * SE_OR)

# Calcula la DR y sus valores mínimos y máximos
DR <- (OR * Xc - Xc) * 100
DR_min <- (IC_OR[1] * Xc - Xc) * 100
DR_max <- (IC_OR[2] * Xc - Xc) * 100


output$result <- renderPrint({
  list(
    "OR" = OR,
    "IC95% OR" = IC_OR,
    "DR" = DR,
    "DR Min" = DR_min,
    "DR Max" = DR_max
  ) 
})

output$plot <- renderPlot({
  ggplot() +
    geom_segment(aes(x = DR_min, xend = DR_max, y = 0, yend = 0), size = 1, color = "blue") +
    geom_point(aes(x = DR, y = 0), size = 4, color = "blue") +
    geom_vline(xintercept = 0, linetype = "dashed", color = "black") +
    geom_vline(xintercept = input$MID, linetype = "dashed", color = "red") +
    labs(x = "DR", y = "", 
         title = "Gráfico de DR con líneas para 0 y MID",
         subtitle = paste("Línea roja: MID =", input$MID),
         caption = "Línea negra: 0") +
    theme_minimal()
})

})
}

shinyApp(ui = ui, server = server)

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.