Warning: Error in $: Can't read output 'plot' when outputting linear model visualizations

Due to the nature of the data, I can't share source data to run the code with.
So I started this mostly out of curiosity of what it would like to thumb through these models. My goal is just to look at a lot of regression models quickly so if there is a better way to do this, I'm open to another approach. I receive the error below when running the attached code.

I'm trying to generate linear regression models for renal patients with different stages of chronic kidney disease (CKD) end-stage renal disease (ESRD). I want to generate a linear model for CKD group that examines the relationship where x= visit count and y = Total amount paid

Listening on http://127.0.0.1:3516
Warning: Error in $: Can't read output 'plot'
  46: <Anonymous>
  45: signalCondition
  44: signal_abort
  43: rlang::abort
  42: $.shinyoutput
  40: server [#2]
   3: runApp
   2: print.shiny.appobj
   1: <Anonymous>
Error in output$plot : Can't read output 'plot'

# Load necessary libraries
library(ggplot2)
library(shiny)
library(dplyr)
library(readxl)
library(stringr)

# Load & Rename data 
Kidney_Correlations_Data <- read_excel("Kidney Correlations Data.xlsx")
Kidney_Correlations_Data <- as.data.frame(Kidney_Correlations_Data) %>%
  mutate(
    CKD_STAGE = str_replace(Kidney_Correlations_Data[[3]], "CKD 3.*", "CKD 3")
  )

# Create a reactive function to filter the data based on user input
ckd_condition <- reactive({
  input$condition
})

visit_threshold <- reactive({
  input$visit_threshold
})

filtered_data <- reactive({
  Kidney_Correlations_Data %>% 
    filter(CKD_STAGE == ckd_condition, VISIT_COUNT > visit_threshold)
})

# Create a linear model and visualization
output$plot <- renderPlot({
  validate(
    need(input$condition != "", "Please select a condition"),
    need(input$visit_threshold > 0, "Please enter a valid visit threshold")
  )
                                 # Filter the data based on the user's input
  data <- filtered_data()
  
                                 # Create a linear model
  lm_model <- lm(Sum_Sum_MR_ALLOWED ~ VISIT_COUNT, data = data)
  
                                 # Create a plot of the linear model
  ggplot(data, aes(x = VISIT_COUNT, y = Sum_Sum_MR_ALLOWED)) + 
    geom_point() + 
    geom_smooth(method = "lm", se = FALSE) + 
    labs(x = "Visit Count", y = "Paid Amount")
})

# Create a dropdown menu for the user to select the CKD condition level
# and a numeric input field for the visit threshold
ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput("condition", "Select CKD Condition:", c("CKD 3", "CKD 4", "CKD 5", "ESRD")),
      numericInput("visit_threshold", "Minimum Visit Count:", value = 5, min = 0)
    ),
    mainPanel(
      plotOutput("plot")
    )
  )
)

# Run the application
shinyApp(ui = ui, server = function(input, output) {
  output$plot
})

Your core problem is the rogue
output$plot alone in your anonymous server function in your shinyapp , all the way at the bottom.
Basically you should only ever be having assignments to outputs (assigning render* stuff), and not reading them. When you have an R variable name listed out, you are asking the R interpret to print it to standard output, so its considered a read.

Solution is to move your output$plot <- render code down into that space.

For the benefit of a cleaner structure , you can get into the habit of putting all server code within your server function directly. I always begin my apps from the following template.

Use the following template :

library(shiny)

ui <- fluidPage(
  # ui stuff 
)

server <- function(input, output, session) {
   # reactives and output$ <- render* 's all go here 
}

shinyApp(ui, server)
1 Like

This was such a huge help, thank you. Completely overlooked that section and it would have taken me forever to figure it out. That's what I get for trying to free-hand it!

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.