Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R-创建带转义引号的字符串,并使用变量的输入_R_Quotes_Double Quotes - Fatal编程技术网

R-创建带转义引号的字符串,并使用变量的输入

R-创建带转义引号的字符串,并使用变量的输入,r,quotes,double-quotes,R,Quotes,Double Quotes,我需要创建具有确切内容的字符串,以便在API REST客户端中使用 body = "{ \"epic\": \"sweden\", \"direction\": \"BUY\" }" 我已经在通过创建字符串而不调用变量来使用解决方案。我现在希望用变量的输入构建字符串 我需要一个不添加更多R包的解决方案。最好是以最低的复杂度为一个良好的概述。我希望避免长正则表达式(但是如果正则表达式是一个很好的推荐方法,我愿意考虑)。 在attemp-1和a

我需要创建具有确切内容的字符串,以便在API REST客户端中使用

body =
      "{
        \"epic\":      \"sweden\",
        \"direction\": \"BUY\"
}"
我已经在通过创建字符串而不调用变量来使用解决方案。我现在希望用变量的输入构建字符串

我需要一个不添加更多R包的解决方案。最好是以最低的复杂度为一个良好的概述。我希望避免长正则表达式(但是如果正则表达式是一个很好的推荐方法,我愿意考虑)。 在attemp-1和attemp-2中,我故意省略了花括号,以使问题及其代码最小化。不过,它们的方括号需要成为解决方案的一部分

迄今为止的尝试:

Attemp-1(使用粘贴解决):

epic1        <- paste0("\"", "sweden")
direction1   <- paste0("BUY", "\"")
create.body1 <- c(epic1, "," ,direction1)
epic2        <- paste0("\"", "sweden")
direction2   <- paste0("BUY", "\"")
create.body2 <- noquote(c(epic2, "," ,direction2))
# Curly brackets.
curly.bracket.left <- "{"
curly.bracket.right <- "}"

# Epic build [key/pair]
epic_key   <- "\"epic\": "
epic_value <- "\"sweden\""
epic_pair  <- c(epic_key, epic_value)

# Direction build [key/pair]
direction_key   <- "\"direction\": "
direction_value <- "\"BUY\""
direction_pair  <- c(direction_key, direction_value)

# Construct body string.
build.body <- c(
                curly.bracket.left,
                epic_pair,
                direction_pair,
                curly.bracket.right
)
问题:每个提供的变量输入都有引号。此外,转义字符仅作为完整字符串的包装添加,而不是根据需要为每个键和值添加

Attemp-2(用[noQuote]去掉引号):

epic1        <- paste0("\"", "sweden")
direction1   <- paste0("BUY", "\"")
create.body1 <- c(epic1, "," ,direction1)
epic2        <- paste0("\"", "sweden")
direction2   <- paste0("BUY", "\"")
create.body2 <- noquote(c(epic2, "," ,direction2))
# Curly brackets.
curly.bracket.left <- "{"
curly.bracket.right <- "}"

# Epic build [key/pair]
epic_key   <- "\"epic\": "
epic_value <- "\"sweden\""
epic_pair  <- c(epic_key, epic_value)

# Direction build [key/pair]
direction_key   <- "\"direction\": "
direction_value <- "\"BUY\""
direction_pair  <- c(direction_key, direction_value)

# Construct body string.
build.body <- c(
                curly.bracket.left,
                epic_pair,
                direction_pair,
                curly.bracket.right
)
问题:所需的转义字符反斜杠已消失

Attemp-3(在构建主体字符串之前预构建[键/值对]):

epic1        <- paste0("\"", "sweden")
direction1   <- paste0("BUY", "\"")
create.body1 <- c(epic1, "," ,direction1)
epic2        <- paste0("\"", "sweden")
direction2   <- paste0("BUY", "\"")
create.body2 <- noquote(c(epic2, "," ,direction2))
# Curly brackets.
curly.bracket.left <- "{"
curly.bracket.right <- "}"

# Epic build [key/pair]
epic_key   <- "\"epic\": "
epic_value <- "\"sweden\""
epic_pair  <- c(epic_key, epic_value)

# Direction build [key/pair]
direction_key   <- "\"direction\": "
direction_value <- "\"BUY\""
direction_pair  <- c(direction_key, direction_value)

# Construct body string.
build.body <- c(
                curly.bracket.left,
                epic_pair,
                direction_pair,
                curly.bracket.right
)

问题:正文字符串中有太多的引号。所有[\“]都很好。

我从你的第三次尝试开始。结果看起来像你想要的。我基本上只是将c()切换为paste(),并在模板中有空间的地方插入了一些空间。这就是你需要的吗

curly.bracket.left <- "{"
curly.bracket.right <- "}"

# Epic build [key/pair]
epic_key   <- "\"epic\": "
epic_value <- "  \"sweden\","
epic_pair  <- c(epic_key, epic_value)

# Direction build [key/pair]
direction_key   <- "\"direction\": "
direction_value <- "\"BUY\""
direction_pair  <- c(direction_key, direction_value)

# Construct body string.
build.body <- c(
  curly.bracket.left,
  epic_pair,
  direction_pair,
  curly.bracket.right
)

string <- paste(curly.bracket.left, epic_key, epic_value, direction_key, direction_value, curly.bracket.right)

> print(string)
[1] "{ \"epic\":    \"sweden\", \"direction\":  \"BUY\" }" 


curly.bracket.left您是否尝试过粘贴(epic1,”,direction1,collapse=“”)
?它输出
“\”瑞典,购买\”“
@LAP:这几乎解决了问题。但是,您的建议将整个字符串包装在[\”],同时主体需要以一种方式构建,即每个键和值都围绕[\”]也许在构建体字符串之前,我需要考虑对包含它的密钥和对的每个变量进行操作。