【问题标题】:Getting libxls error with my filepath with read_xls function in R在 R 中使用 read_xls 函数的文件路径出现 libxls 错误
【发布时间】:2021-12-13 06:58:38
【问题描述】:

我一直在尝试使用以下网址运行以下代码:https://files.hawaii.gov/dbedt/economic/data_reports/DLIR/LFR_LAUS_LF.xls

该网址下载一个 xls 文件。但是在 R 中使用 read_xls(temp, sheet = 1) 命令给了我一个 R。有什么解决方案吗?该文件应该存储在临时文件夹中。

从 DBEDT 抓取就业数据

rm(list = ls(all = TRUE))

#loads necessary packages to run codes
library(dplyr)
library(tidyverse)
library(readxl)
library(stringr)
library(taRifx)
library(lubridate)

#set url link for xls
# url <- "https://www.hirenethawaii.com/admin/gsipub/htmlarea/uploads/LFR_LAUS_LF.xls"
url <- "https://files.hawaii.gov/dbedt/economic/data_reports/DLIR/LFR_LAUS_LF.xls"

#create a temp file to load the xls and download the xls
temp <- tempfile(fileext = "xls")
download.file(url = url, destfile = temp)

#create a function to select only the columns we want
#grab date (col 2), labor force (col 3), employed persons (col 4), and unemployment rate (col 6) columns from state data
keep_cols <- function(data) {
  data <- data %>%
    select(2,3,4,6) %>%
    rename(date = 1, lf = 2, empl = 3, ur = 4)
  
  return(data)
}


#importing the xls sheets into our r environment
state_xls <- read_xls(temp, sheet = 1) %>% keep_cols() %>% mutate(geo = "HI")

错误如下:

 Error: 
  filepath: C:\Users\Jon Doe\AppData\Local\Temp\Rtmp0iFXmt\file8eb8142165d0xls
  libxls error: Unable to open file 

我使用的是 Windows 笔记本电脑,代码最初是用 Macbookbbb 编写的

【问题讨论】:

  • 奇怪,这对我有用,没有任何错误。
  • @RonakShah 我正在使用 PC 顺便说一句。任何其他可以帮助它工作的修改?

标签: r url xls


【解决方案1】:

the download.file() 的 Windows 似乎有问题。所以我添加了mode="wb",它似乎奏效了。所以基本上这部分代码需要更改:

download.file(url = url, destfile = temp, mode = "wb")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-17
    • 1970-01-01
    • 2018-01-23
    • 2021-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多