使用 OS 特定的换行符(CRLF、LF、CR)构建字符串以将其写入数据库 table 列
Build string with OS-specific newline characters (CRLF, LF, CR) to write it into a database table column
我想将包含 R (\n
) 的常用换行符的字符串写入数据库的列 table。
如何将新行转换为操作系统特定的表示形式(Windows = CR/LF、Linux = LF、Mac = CR...)?
我了解到 R 不提供操作系统特定的表示,所以我必须找到一个解决方法:
- Printing newlines with print() in R
对 print/cat 字符串的任何尝试都失败了:
msg <- "I want to have \n a new line"
cat(msg)
# I want to have
# a new line
out <- capture.output(cat(msg))
out
# a vector with two elements (one for each row but no new line characters anymore)
# [1] "I want to have " " a new line"
paste(out, collapse = "\n") # how could I inject the correct new line characters here?
# [1] "I want to have \n a new line"
# welcome endless-loop :-)
有没有办法让 R 从字符串中的 \n
创建正确的换行符?
PS:我正在使用内置的 tcltk
包和 puts
但我总是以 R "reconverting" 换行符结尾 \n
...
另一个 "cheat" 可能是用引号将 \n
括起来,以便将其视为一行。到目前为止我不知道这是怎么回事...
在R中正确设置新行代码的一种方法是查询操作系统。由于 OS X 和 Linux 的行为方式相同,因此这是一个判断操作系统是否为 Windows 的问题。一种方法是查询 OS
环境变量,如下所示。
if(substr(Sys.getenv("OS"),1,7) == "Windows") {
# set Windows newline
newLine <- "\r\n"
}
else {
# set non-Windows newline
newLine <- "\n"
}
接下来使用 paste()
和 newLine
对象来为操作系统的新行生成正确的字符。
paste("my text string on a line",newline,sep="")
问候,
莱恩
在这里您可以找到我的最终实现,作为已接受答案的可能替代方案:
# Returns the operating system specific new line character(s):
# CR LF on Windows, else only LF...
# Simlar to Microsofts .Net "Environment.NewLine"
platform.NewLine <- function() {
is.windows <- grepl(tolower(.Platform$OS.type), "windows", fixed = TRUE)
if (is.windows) {
newline <- "\r\n"
} else {
newline <- "\n"
}
sys.name <- Sys.info()["sysname"]
is.windows.2nd.opinion <- grepl(tolower(sys.name), "windows", fixed = TRUE)
if (is.windows != is.windows.2nd.opinion)
warning("R seems to run on Windows OS but this could not be recognized for sure")
return(newline)
}
# Usage (examples) ------------------------------------------------------------------------------------------------
newline <- platform.NewLine()
# "print" shows the "symbolic" names (escape codes)
print(paste("Line1", "Line2", sep = newline))
# [1] "Line1\r\nLine2"
# uses "\n" or "\r\n" depending on your OS
# "cat" applies the newline escape codes to the output
cat(paste("Line1", "Line2", sep = newline))
# Line1
# Line2
我想将包含 R (\n
) 的常用换行符的字符串写入数据库的列 table。
如何将新行转换为操作系统特定的表示形式(Windows = CR/LF、Linux = LF、Mac = CR...)?
我了解到 R 不提供操作系统特定的表示,所以我必须找到一个解决方法:
- Printing newlines with print() in R
对 print/cat 字符串的任何尝试都失败了:
msg <- "I want to have \n a new line"
cat(msg)
# I want to have
# a new line
out <- capture.output(cat(msg))
out
# a vector with two elements (one for each row but no new line characters anymore)
# [1] "I want to have " " a new line"
paste(out, collapse = "\n") # how could I inject the correct new line characters here?
# [1] "I want to have \n a new line"
# welcome endless-loop :-)
有没有办法让 R 从字符串中的 \n
创建正确的换行符?
PS:我正在使用内置的 tcltk
包和 puts
但我总是以 R "reconverting" 换行符结尾 \n
...
另一个 "cheat" 可能是用引号将 \n
括起来,以便将其视为一行。到目前为止我不知道这是怎么回事...
在R中正确设置新行代码的一种方法是查询操作系统。由于 OS X 和 Linux 的行为方式相同,因此这是一个判断操作系统是否为 Windows 的问题。一种方法是查询 OS
环境变量,如下所示。
if(substr(Sys.getenv("OS"),1,7) == "Windows") {
# set Windows newline
newLine <- "\r\n"
}
else {
# set non-Windows newline
newLine <- "\n"
}
接下来使用 paste()
和 newLine
对象来为操作系统的新行生成正确的字符。
paste("my text string on a line",newline,sep="")
问候,
莱恩
在这里您可以找到我的最终实现,作为已接受答案的可能替代方案:
# Returns the operating system specific new line character(s):
# CR LF on Windows, else only LF...
# Simlar to Microsofts .Net "Environment.NewLine"
platform.NewLine <- function() {
is.windows <- grepl(tolower(.Platform$OS.type), "windows", fixed = TRUE)
if (is.windows) {
newline <- "\r\n"
} else {
newline <- "\n"
}
sys.name <- Sys.info()["sysname"]
is.windows.2nd.opinion <- grepl(tolower(sys.name), "windows", fixed = TRUE)
if (is.windows != is.windows.2nd.opinion)
warning("R seems to run on Windows OS but this could not be recognized for sure")
return(newline)
}
# Usage (examples) ------------------------------------------------------------------------------------------------
newline <- platform.NewLine()
# "print" shows the "symbolic" names (escape codes)
print(paste("Line1", "Line2", sep = newline))
# [1] "Line1\r\nLine2"
# uses "\n" or "\r\n" depending on your OS
# "cat" applies the newline escape codes to the output
cat(paste("Line1", "Line2", sep = newline))
# Line1
# Line2