Generate a list of expression literals from an integer sequence

≯℡__Kan透↙ 提交于 2020-01-01 06:45:09

问题


I would like to map a sequence of integers to a sequence of expression literals in order to use the latter as tick mark labels in a plot, e.g.

lbls <- lapply(-2:2, function(i) expression(i * pi))
plot(...)
axis(1, at=seq(-2,2)*pi, labels=lbls)

So far I've tried all variations of bquote, substitute, expression etc. that I could think of, but apparently I must have missed something. Also, the FAQ and related SO questions & answers didn't fully solve this for me.

How would I do it correctly (I want axis to render pi as the greek letter and have -2 ... 2 substituted for i in the above example)?


回答1:


try this:

lbls <- do.call("expression", lapply(-2:2, function(i) substitute(X * pi, list(X = i))))
plot(-10:10, -10:10, xaxt="n")
axis(1, at=seq(-2,2)*pi, labels=lbls)




回答2:


Try this:

lbls <- parse(text = paste(seq(-2, 2), "pi", sep = "*"))


来源:https://stackoverflow.com/questions/10042258/generate-a-list-of-expression-literals-from-an-integer-sequence

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!