Hack to to在haven :: read_sav()中的文件路径中包含特殊字符

sin*_*dur 12 r r-haven

这里似乎是一个问题,包括在文件路径中的任何类型的特殊字符,包括刚才的文件名时,与避难所(1.1.1)封装.

假设这是一个真正的问题,我正在寻找一些巧妙的黑客/解决方案来解决它.

一个(不理想的)例子是让R将文件的副本放到一个更友好的路径中并给它一个"更好"的文件名然后加载避风港.如:

setwd("c:/temp")
fn <- "randóóm.sav"
file.copy(paste0("./äglæpath/", fn), fn)
file.rename(fn, gsub("[^-\\./a-zA-Z0-9[:space:]]", "", fn))
# now apply read_sav() to the copy
Run Code Online (Sandbox Code Playgroud)

我正在使用:

R version 3.5.0 (2018-04-23)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
Run Code Online (Sandbox Code Playgroud)

Tec*_*e01 2

不幸的是,我已经能够在 Windows 10 上使用标准版本haven和. 这似乎是避风港的已知错误。第371章devtoolshaven

\n\n

推荐解决方法:

\n\n

将文件移动到文件路径或文件名中没有德语变音符号的目录。因此,您的解决方法按所述工作。

\n\n
> file.path(dataFilepath, dtaFilename)\n[1] "\xc3\xa4gl\xc3\xa6path/rand\xc3\xb3\xc3\xb3m.dta"\n\n> dtaFilename <- gsub("[^-\\\\./a-zA-Z0-9[:space:]]", "", dtaFilename)\n> bdatFilename <- gsub("[^-\\\\./a-zA-Z0-9[:space:]]", "", bdatFilename)\n> savFilename <- gsub("[^-\\\\./a-zA-Z0-9[:space:]]", "", savFilename)\n> dataFilepath <- gsub("[^-\\\\./a-zA-Z0-9[:space:]]", "", dataFilepath)\n\n> file.path(dataFilepath, dtaFilename)\n[1] "glpath/randm.dta"\n\n> # Stata\n> read_dta(dtaDest)\n# A tibble: 150 x 5\n   sepallength sepalwidth petallength petalwidth species\n         <dbl>      <dbl>       <dbl>      <dbl> <chr>  \n 1        5.10       3.5         1.40      0.200 setosa \n 2        4.90       3           1.40      0.200 setosa \n 3        4.70       3.20        1.30      0.200 setosa \n 4        4.60       3.10        1.5       0.200 setosa \n 5        5          3.60        1.40      0.200 setosa \n 6        5.40       3.90        1.70      0.400 setosa \n 7        4.60       3.40        1.40      0.300 setosa \n 8        5          3.40        1.5       0.200 setosa \n 9        4.40       2.90        1.40      0.200 setosa \n10        4.90       3.10        1.5       0.100 setosa \n# ... with 140 more rows\n> \n
Run Code Online (Sandbox Code Playgroud)\n\n

Github 错误 #371

\n\n

Read_*() 不适用于文件路径中的特殊字符 #371\n https://github.com/tidyverse/haven/issues/371

\n\n

问题代码位于DfReader.cpp df.parse_dta() 594-612haven/src/DFReader.cpp

\n\n

重现代码

\n\n
require(haven)\nrequire(stringi)\n\ndtaURL  <- "https://github.com/tidyverse/haven/blob/master/inst/examples/iris.dta?raw=true"\nbdatURL <- "https://github.com/tidyverse/haven/blob/master/inst/examples/iris.sas7bdat?raw=true"\nsavURL  <- "https://github.com/tidyverse/haven/blob/master/inst/examples/iris.sav?raw=true"\n\ndtaFilename   <- "rand\xc3\xb3\xc3\xb3m.dta"\nbdatFilename <- "rand\xc3\xb3\xc3\xb3m.bdata"\nsavFilename   <- "rand\xc3\xb3\xc3\xb3m.sav"\n\ndataFilepath      <- "\xc3\xa4gl\xc3\xa6path"\n\nif (!dir.exists(dataFilepath)) {\n  dir.create(file.path(dataFilepath), showWarnings = TRUE)\n}\n\ndtaDest = file.path(dataFilepath, dtaFilename)\nbdatDest = file.path(dataFilepath, bdatFilename )\nsavDest = file.path(dataFilepath, savFilename )\n\ndownload.file(dtaURL, destfile = dtaDest, method = "wget", mode = "wb")\ndownload.file(bdatURL, destfile = bdatDest, method = "wget", mode = "wb")\ndownload.file(savURL, destfile = savDest, method = "wget", mode = "wb")\n\n\n# Stata\nread_dta(dtaDest)\n\n# SAS\nread_sas(bdatDest)\n\n# SPSS\nread_sav(savDest)\n
Run Code Online (Sandbox Code Playgroud)\n\n

控制台输出

\n\n
> require(haven)\n> require(stringi)\n> dtaURL  <- "https://github.com/tidyverse/haven/blob/master/inst/examples/iris.dta?raw=true"\n> bdatURL <- "https://github.com/tidyverse/haven/blob/master/inst/examples/iris.sas7bdat?raw=true"\n> savURL  <- "https://github.com/tidyverse/haven/blob/master/inst/examples/iris.sav?raw=true"\n> dtaFilename   <- "rand\xc3\xb3\xc3\xb3m.dta"\n> bdatFilename <- "rand\xc3\xb3\xc3\xb3m.bdata"\n> savFilename   <- "rand\xc3\xb3\xc3\xb3m.sav"\n> dataFilepath      <- "\xc3\xa4gl\xc3\xa6path"\n> if (!dir.exists(dataFilepath)) {\n+   dir.create(file.path(dataFilepath), showWarnings = TRUE)\n+ }\n> dtaDest = file.path(dataFilepath, dtaFilename)\n> bdatDest = file.path(dataFilepath, bdatFilename )\n> savDest = file.path(dataFilepath, savFilename )\n> download.file(dtaURL, destfile = dtaDest, method = "wget", mode = "wb")\n--2018-05-29 15:56:59--  https://github.com/tidyverse/haven/blob/master/inst/examples/iris.dta?raw=true\nResolving github.com (github.com)... 192.30.255.113, 192.30.255.112\nConnecting to github.com (github.com)|192.30.255.113|:443... connected.\nHTTP request sent, awaiting response... 302 Found\nLocation: https://github.com/tidyverse/haven/raw/master/inst/examples/iris.dta [following]\n--2018-05-29 15:56:59--  https://github.com/tidyverse/haven/raw/master/inst/examples/iris.dta\nReusing existing connection to github.com:443.\nHTTP request sent, awaiting response... 302 Found\nLocation: https://raw.githubusercontent.com/tidyverse/haven/master/inst/examples/iris.dta [following]\n--2018-05-29 15:56:59--  https://raw.githubusercontent.com/tidyverse/haven/master/inst/examples/iris.dta\nResolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.52.133\nConnecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.52.133|:443... connected.\nHTTP request sent, awaiting response... 200 OK\nLength: 8213 (8.0K) [application/octet-stream]\nSaving to: \'\\344gl\\346path/rand\\363\\363m.dta\'\n\n     0K ........                                              100% 1.56M=0.005s\n\n2018-05-29 15:56:59 (1.56 MB/s) - \'\\344gl\\346path/rand\\363\\363m.dta\' saved [8213/8213]\n\n> download.file(bdatURL, destfile = bdatDest, method = "wget", mode = "wb")\n--2018-05-29 15:56:59--  https://github.com/tidyverse/haven/blob/master/inst/examples/iris.sas7bdat?raw=true\nResolving github.com (github.com)... 192.30.255.113, 192.30.255.112\nConnecting to github.com (github.com)|192.30.255.113|:443... connected.\nHTTP request sent, awaiting response... 302 Found\nLocation: https://github.com/tidyverse/haven/raw/master/inst/examples/iris.sas7bdat [following]\n--2018-05-29 15:56:59--  https://github.com/tidyverse/haven/raw/master/inst/examples/iris.sas7bdat\nReusing existing connection to github.com:443.\nHTTP request sent, awaiting response... 302 Found\nLocation: https://raw.githubusercontent.com/tidyverse/haven/master/inst/examples/iris.sas7bdat [following]\n--2018-05-29 15:56:59--  https://raw.githubusercontent.com/tidyverse/haven/master/inst/examples/iris.sas7bdat\nResolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.52.133\nConnecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.52.133|:443... connected.\nHTTP request sent, awaiting response... 200 OK\nLength: 131072 (128K) [application/octet-stream]\nSaving to: \'\\344gl\\346path/rand\\363\\363m.bdata\'\n\n     0K .......... .......... .......... .......... .......... 39% 4.05M 0s\n    50K .......... .......... .......... .......... .......... 78% 19.7M 0s\n   100K .......... .......... ........                        100% 19.3M=0.02s\n\n2018-05-29 15:57:00 (7.83 MB/s) - \'\\344gl\\346path/rand\\363\\363m.bdata\' saved [131072/131072]\n\n> download.file(savURL, destfile = savDest, method = "wget", mode = "wb")\n--2018-05-29 15:57:01--  https://github.com/tidyverse/haven/blob/master/inst/examples/iris.sav?raw=true\nResolving github.com (github.com)... 192.30.255.113, 192.30.255.112\nConnecting to github.com (github.com)|192.30.255.113|:443... connected.\nHTTP request sent, awaiting response... 302 Found\nLocation: https://github.com/tidyverse/haven/raw/master/inst/examples/iris.sav [following]\n--2018-05-29 15:57:01--  https://github.com/tidyverse/haven/raw/master/inst/examples/iris.sav\nReusing existing connection to github.com:443.\nHTTP request sent, awaiting response... 302 Found\nLocation: https://raw.githubusercontent.com/tidyverse/haven/master/inst/examples/iris.sav [following]\n--2018-05-29 15:57:01--  https://raw.githubusercontent.com/tidyverse/haven/master/inst/examples/iris.sav\nResolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.52.133\nConnecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.52.133|:443... connected.\nHTTP request sent, awaiting response... 200 OK\nLength: 6690 (6.5K) [application/octet-stream]\nSaving to: \'\\344gl\\346path/rand\\363\\363m.sav\'\n\n     0K ......                                                100% 3.09M=0.002s\n\n2018-05-29 15:57:01 (3.09 MB/s) - \'\\344gl\\346path/rand\\363\\363m.sav\' saved [6690/6690]\n\n> # Stata\n> read_dta(dtaDest)\nError in df_parse_dta_file(spec, encoding) : \n  Failed to parse <...>/\xc3\x83\xc2\xa4gl\xc3\x83\xc2\xa6path/rand\xc3\x83\xc2\xb3\xc3\x83\xc2\xb3m.dta: Unable to open file.\n
Run Code Online (Sandbox Code Playgroud)\n