Delete row button in R shiny app not working (using google sheets as datasource)

I am trying to build a simple app: in the first page is data collection with a renderdatatable to the right. I have a submit and edit buttons that do the job correctly, however, the delete button command does not delete the row (and store in the google datasheet). I managed to delete in the render table, but when reinitiating the app it was still there.

I can edit (and save) the changes, so it is not a permission problem. I tried many different approaches without success.

library(shiny)
library(shinythemes)
library(bslib)
library(bsicons)
library(tidyverse)
library(DT)
library(googlesheets4)
library(readxl)

# Load sheet (data.frame with columns: name, date, weight, height)
sheet_url <- "https://docs.google.com/spreadsheets/d/1jI9rwSMS4mi-EbHCDaT--gDETCpDNaadXK98lub0wVA/edit?gid=0#gid=0"

# Functions to save and load data
saveData <- function(data) {
  data <- as.data.frame(data, stringsAsFactors = FALSE)
  sheet_append(sheet_url, data)
}

loadData <- function() {
  raw_data <- read_sheet(sheet_url) %>%
    mutate(date = as_date(date)) %>%
    arrange(date)
  processed_data <- raw_data %>%
    group_by(name) %>%
    mutate(Nmonths = round((as.numeric(difftime(date, min(date), units = "days")) / 30), 1))
  list(raw = raw_data, processed = processed_data)
}

# Define the fields we want to save from the form
fields <- c("name", "date", "weight", "height")

# UI
ui <- navbarPage(
  theme = shinytheme("cerulean"),
  title = "Baby growth",
  tabPanel(
    title = "Data entry",
    sidebarLayout(
      sidebarPanel(
        selectInput("name", "Name", choices = c("John", "Mike")),
        dateInput("date", "Registry date"),
        numericInput("weight", "Weight", value = 0),
        numericInput("height", "Height", value = 0),
        actionButton("submit", "Submit"),
        tags$hr(),
        actionButton("edit", "Edit selected row"),
        actionButton("save", "Save changes"),
        actionButton("delete", "Delete selected row")
      ),
      mainPanel(
        DT::dataTableOutput("entry"),
        tags$hr()
      )
    )
  )
)

# Server
server <- function(input, output, session) {
  # Load and preprocess baby data
  data_list <- loadData()
  raw_baby <- data_list$raw
  processed_baby <- data_list$processed
  
  # Initialize reactive values
  rv <- reactiveValues(selected_row = NULL, data = raw_baby)
  
  # Aggregate form data
  formData <- reactive({
    data <- sapply(fields, function(x) input[[x]], simplify = FALSE)
    data <- as.data.frame(data, stringsAsFactors = FALSE)
    data
  })
  
  # Submit button
  observeEvent(input$submit, {
    new_entry <- formData()
    saveData(new_entry)
    data_list <- loadData()
    rv$data <- data_list$raw
    showNotification("New entry added", type = "message")
  })
  
  # Edit button
  observeEvent(input$edit, {
    selected_row <- input$entry_rows_selected
    if (length(selected_row) == 1) {
      rv$selected_row <- selected_row
      row <- rv$data[selected_row, ]
      updateSelectInput(session, "name", selected = row$name)
      updateDateInput(session, "date", value = row$date)
      updateNumericInput(session, "weight", value = row$weight)
      updateNumericInput(session, "height", value = row$height)
    } else {
      showNotification("Please select a single row to edit", type = "warning")
    }
  })
  
  # Save button
  observeEvent(input$save, {
    selected_row <- rv$selected_row
    if (!is.null(selected_row)) {
      edited_entry <- formData()
      edited_entry <- as.data.frame(edited_entry, stringsAsFactors = FALSE)
      edited_entry$date <- edited_entry$date  # Ensure date is character for saving
      if (ncol(edited_entry) == ncol(rv$data)) {
        rv$data[selected_row, ] <- edited_entry
        # Save updated data to Google Sheets
        range_write(sheet_url, data = rv$data, range = "A1", col_names = TRUE)
        data_list <- loadData()
        rv$data <- data_list$raw
        rv$selected_row <- NULL
        showNotification("Changes saved", type = "message")
      } else {
        showNotification("Error: Data format mismatch", type = "error")
      }
    }
  })
  
  # Delete button
  observeEvent(input$delete, {
    selected_row <- input$entry_rows_selected
    if (length(selected_row) == 1) {
      cat("Deleting row:", selected_row, "n")  # Logging
      rv$data <- rv$data[-selected_row, ]
      # Save updated data to Google Sheets
      range_write(sheet_url, data = rv$data, range = "A1", col_names = TRUE)
      data_list <- loadData()
      rv$data <- data_list$raw
      rv$selected_row <- NULL
      showNotification("Row deleted", type = "message")
    } else {
      showNotification("Please select a single row to delete", type = "warning")
    }
  })
  
  # Render data table
  output$entry <- DT::renderDataTable({
    input$submit
    input$save
    input$delete
    data_list <- loadData()
    processed_baby <- data_list$processed
    rv$data <- data_list$raw
    processed_baby %>%
      mutate(date = as.Date(date))  # Ensure date is displayed correctly
  }, selection = 'single')

}

shinyApp(ui, server)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật