简体   繁体   English

在 R markdown 中使用带空格的变量名

[英]Use a variable name with spaces inline in R markdown

How can I include inline R code that refers to a variable name that contains spaces or other unusual characters (actual use-case is Pr(>F) )?如何包含引用包含空格或其他异常字符的变量名称的内联R代码(实际用例是Pr(>F) )? Backticks are the solution in plain R script, but they don't seem to work when the code is inline in a markdown doc.反引号是普通R脚本中的解决方案,但当代码内联在降价文档中时,它们似乎不起作用。 Here's an example:下面是一个例子:

```{r}
df <- data.frame(mydata= 1:10, yourdata = 20:29)
names(df) <- c("your data", "my data")

```

The first five values of your data are `r df$`your data`[1:5]`

Which when knitted gives:针织时给出:

Quitting from lines 7-9 (test-main.Rmd) 
Error in base::parse(text = code, srcfile = NULL) : 
  2:0: unexpected end of input
1: df$
   ^
Calls: <Anonymous> ... <Anonymous> -> withVisible -> eval -> parse_only -> <Anonymous>
Execution halted

Note that this is different from showing the backticks .请注意,这与显示反引号不同。 All I want to do is have the code executed when the the doc is knitted.我想要做的就是在编写文档时执行代码。 My workaround is to assign the value of the odd-named variable to another object with a simple name in the chunk preceding the inline code.我的解决方法是将奇数命名变量的值分配给另一个具有简单名称的对象,该对象位于内联代码之前的块中。 But I'm curious about how to directly call inline these objects with unusual names.但是我很好奇如何使用不寻常的名称直接调用内联这些对象。

In this instance can use normal quotes,在这种情况下可以使用普通引号,

 The first five values of your data are `r df$"your data"[1:5]`

or rather更确切地说

 The first five values of your data are `r df[["your data"]][1:5]`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM