You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

macOS系统下R语言调用read_xls读取.xls文件失败求助

Troubleshooting "failed to open" Error with read_xls in R

Hey there, let’s work through this frustrating "failed to open" error together! Since your friend can run the exact same code on the same file without issues, the problem is almost certainly tied to your local R environment or file setup. Here are actionable steps to fix this:

1. Confirm the file path is correct

This is the most common culprit!

  • First, test if R can even locate your file with:
    file.exists("unb-dgp.xls")
    
    If this returns FALSE, R can’t find the file at the path you provided.
  • Switch to an absolute path to avoid relative path confusion. For example:
    • Windows: read_xls("C:/Users/YourUsername/Documents/unb-dgp.xls", sheet=1, col_names = TRUE)
    • Mac/Linux: read_xls("/home/YourUsername/Documents/unb-dgp.xls", sheet=1, col_names = TRUE)
  • Or use file.choose() to manually select the file, which guarantees the path is correct:
    library(readxl)
    test <- read_xls(file.choose(), sheet=1, col_names = TRUE, col_types = NULL, skip=0)
    

2. Check file permissions and locks

  • Make sure the file isn’t open in another program (like Excel)—locked files can’t be read by R. Close any apps accessing it and try again.
  • On Windows: Right-click the file → Properties → Security tab, confirm your user account has "Read" permissions.
  • On Mac/Linux: Run this terminal command to grant read access if needed:
    chmod +r unb-dgp.xls
    

3. Update readxl and its dependencies

Outdated versions can cause compatibility issues:

  • Update the readxl package first:
    install.packages("readxl")
    
  • Restart your R session completely, then reload the package with library(readxl) before running your code.
  • If you’re on Linux, readxl relies on the libxls system library. Install it via your package manager:
    • Ubuntu/Debian: sudo apt-get install libxls-dev
    • CentOS/RHEL: sudo yum install libxls-devel

4. Test in a clean R session

Sometimes other loaded packages or environment variables can interfere:

  • Close RStudio/R Shell entirely, then open a fresh session.
  • Only load readxl (library(readxl)) and run your read_xls command—don’t load any other packages first.

5. Verify the file is a valid .xls file

Even if the extension is .xls, it might actually be a different format (like .xlsx). Try:

# Test if read_xlsx works instead
test <- read_xlsx("unb-dgp.xls", sheet=1, col_names = TRUE)
# Or check if Excel sheets can be detected
excel_sheets("unb-dgp.xls")

If this works, the file was mislabeled (though your friend’s success makes this less likely, it’s worth checking!).

内容的提问来源于stack exchange,提问作者Skalwalker

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:21:53