File Path Issues in R Using Windows ("Hex Digits in Character String" Error)

File path issues in R using Windows ( Hex digits in character string error)

replace all the \ with \\.

it's trying to escape the next character in this case the U so to insert a \ you need to insert an escaped \ which is \\

Paste a file path onto the beginning of each element of a character list of file names in R

You can simply use paste0() directly on the character vector (there's no need for an explicit loop), like so:

filenames <- c("a.txt", "b.txt", "c.txt")
paste0("prefix/", filenames)
#> [1] "prefix/a.txt" "prefix/b.txt" "prefix/c.txt"

R seldom operates on single bits of data; most operations apply to vectors of data.

RStudio addin to parse input string literals by changing backslashes (as in 'C:\Marketing') into forward slashes

A working solution (or rather a workaround, as initially I wanted to solve this with a function)

As I wrote in the question above, solving this problem with a function was not possible, so I tried a different solution, which is doing exactly what I wanted, and might be useful for others as well. I have built an addin package (a nice article by RStudio) with the following function:

#' Parse selected "Windows" path to an "R-usable" one
#'
#' @return
#' @export
#' @importFrom rstudioapi getActiveDocumentContext
#' @importFrom magrittr '%>%'
#'
#' @examples
path_parse <- function() {
getActiveDocumentContext()[['selection']][[1]][['text']] %>%
{ gsub('\\\\', '/', .) } %>%
{ gsub('//', '/', .) } %>% # To remove double f. slashes
{ ifelse(check_for_quotes(.), insertText(.), insertText(paste0('\'', ., '\''))) }
}
### Old function:
# path_parse <- function() {
# getActiveDocumentContext()[['selection']][[1]][['text']] %>%
# { chartr('\\', '/', .) } %>% { insertText(paste0('\'', ., '\'')) }
# }

and assigned a Ctrl+Alt+P shortcut to it.

What it does is, basically, parsing the selected text (i.e. the pasted "Windows" path) by changing all backslashes into forward slashes and inserting it back into the code:

Addin screenshots



Related Topics



Leave a reply



Submit