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:
Related Topics
Using Lists Inside Data.Table Columns
Find Common Substrings Between Two Character Variables
How to Make R Beep/Play a Sound at the End of a Script
Explain Ggplot2 Warning: "Removed K Rows Containing Missing Values"
Plotting Multiple Time-Series in Ggplot
R Loop for Variable Names to Run Linear Regression Model
R Error in X$Ed:$ Operator Is Invalid for Atomic Vectors
Why Is Enquo + !! Preferable to Substitute + Eval
Lib Unspecified & Error in Loadnamespace
Convert Yyyymmdd String to Date Class in R
Idiom for Ifelse-Style Recoding for Multiple Categories
Grouped Barplot in R with Error Bars
Remove All of X Axis Labels in Ggplot
Why Is Apply() Method Slower Than a for Loop in R
Position of the Sun Given Time of Day, Latitude and Longitude