Permission denied in created folder

Hi you guys,
I'm running into a permission denied error when trying to display images from a folder that I create within my R script. Here's a chunk of what I'm doing:

pp_position <- file.path(Data, "Raw", "Image", "pp_position", Config_name)
if (!dir.exists(pp_position)) {
  dir.create(pp_position, recursive = TRUE)
}
for (sn in unique(dp_inl$Serial_No)) {
  plot_data <- pp_defect_log %>% filter(Serial_No == sn)
  
  # Create minimal plot with just dots
  p <- ggplot(plot_data, aes(x = x, y = y)) +
    geom_point(color = "darkorange", size = 1.5) +
    xlim(0, 4032) +
    ylim(0, 3024) +
    theme_void() +  # removes background, grid, axes
    theme(
      plot.margin = margin(0, 0, 0, 0),
      panel.border = element_rect(color = "darkgray", fill = NA, linewidth = 0.5),
      panel.spacing = unit(0, "pt")
    )
  
  # Save as PNG
  ggsave(
    filename = file.path(pp_position, paste0(sn, ".png")),
    plot = p,
    width = 1.83,
    height = 0.91,
    units = "in",
    dpi = 96
  )
}

Later I try to embed the images into a table:

dp_inl_summary_table <- dp_inl %>%
  select(Lot_No, Serial_No) %>%
  left_join(fa_result %>% select(Serial_No, Width, Height, Breakdown, EDX_result, `Before/After PP`), by = "Serial_No") %>%
  mutate(
    pp_path = paste0("Raw/Image/pp_position/", Config_name, "/", Serial_No, ".png"),
    carrier_path = paste0("Raw/Image/carrier_position", Config_name, "/", Serial_No, ".png"),
    
        `PP log` = ifelse(
      file.exists(pp_path),
      paste0('<img src="', pp_position, '">'),
      ""
    ),
  ) %>%
  select(-pp_path) %>%
  select(Lot_No, Serial_No, Width, Height, Area, `Position image`, `PP log`, `Before/After PP`, `Raw image`, `SEM image`, `EDX image`, EDX_result, Breakdown) %>%
  rename(`Jugde` = Breakdown) %>%
  kable(format = "html", escape = FALSE) %>%
  kable_styling() %>%
  scroll_box(height = "600px", width = "100%")

HTML(dp_inl_summary_table)

But when I knit the document, I get this error:

/Raw/Image/pp_position/C3006: withBinaryFile: permission denied (Permission denied)
Error: pandoc document conversion failed with error 1
Execution halted

Additional Notes:

  • The folder does get created, and I can see it.
  • This issue only happens with this specific folder (pp_position). Other image outputs I have tried are displayed just fine.
  • I’ve confirmed that the folder path is correct using print(pp_position) and file.exists() — everything looks fine.

:red_question_mark: My Question:

Why would a folder that I create inside the R script throw a permission denied error when knitting the document?

Is there something I’m missing regarding RMarkdown rendering, Pandoc permissions, or file access on Windows?

Any suggestions on how to approach this would be greatly appreciated!

Thanks in advance :folded_hands:

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