在R markdown中使用带内联空格的变量名 [英] Use a variable name with spaces inline in R markdown

查看:187
本文介绍了在R markdown中使用带内联空格的变量名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何包含内联R代码,该代码引用包含空格或其他不寻常字符(实际用例为Pr(>F))的变量名称? 反引号是简单的R脚本中的解决方案,但是当代码在命令行中内联时,它们似乎不起作用降价文件这是一个示例:

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))? Backticks are the solution in plain R script, but they don't seem to work when the code is inline in a markdown doc. 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]`

或更确切地说

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

这篇关于在R markdown中使用带内联空格的变量名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆