如果无错误的 R 函数不这样做,如何 return 一个值
How to return a value if an error-free R function does not do so
我的目标是将 pyramid package
生成的情节分配给列表。稍后,我会将列表中的该图和其他图插入到文档中。但是 pyramid function
似乎不是 return 一个值。如何将金字塔图分配给对象?
install.packages("pyramid") # functions to draw a population pyramid
library(pyramid)
# create a mock data frame to comparing this plot to a counterpart from plotrix
df <- data.frame(level1 = c(9,9,4,3,34,28), levelsame = c(9,9,4,3,34,28),
title = c("Dir", "Exec. Dir", "Mgr", "Sr. Mgr", "Mgt Princ", "EVP+"))
# assign the plot (hopefully) to an object
empty <- pyramid(df, Laxis = seq(1,35,5), AxisFM = "g", Csize = 0.8, Cgap = .5, Llab = "",
Rlab = "", Clab = "Title", GL = F, Lcol = "blue", Rcol = "blue",
Ldens = -1, main = "Distribution of Levels")
> empty
NULL
同样,如果我将 pyramid call 分配到我的列表中,则什么也不会发生。 pyramid
编辑的 return 列表没有值。
plotlist2[["pyramid"]] <- pyramid(df, Laxis = seq(1,35,5), AxisFM = "g", Csize = 0.8, Cgap = .5, Llab = "",
Rlab = "", Clab = "Title", GL = F, Lcol = "blue", Rcol = "blue",
Ldens = -1, main = "Distribution of Levels")
> plotlist2[1]
[[1]]
NULL
我担心我在一些明显的误解中犯了错误,所以我欢迎被纠正。谢谢。
您可以使用recordPlot()
函数将当前绘图保存到变量。
在你的情况下你可以这样做:
#print the plot
pyramid(df, Laxis = seq(1,35,5), AxisFM = "g", Csize = 0.8, Cgap = .5, Llab = "",
Rlab = "", Clab = "Title", GL = F, Lcol = "blue", Rcol = "blue",
Ldens = -1, main = "Distribution of Levels")
#save the current printed plot
pyrPlot<-recordPlot()
#plot it again
pyrPlot
您可能必须使用 dev.control(displaylist ="enable")
启用显示列表才能正常工作,具体取决于您使用的图形设备
我的目标是将 pyramid package
生成的情节分配给列表。稍后,我会将列表中的该图和其他图插入到文档中。但是 pyramid function
似乎不是 return 一个值。如何将金字塔图分配给对象?
install.packages("pyramid") # functions to draw a population pyramid
library(pyramid)
# create a mock data frame to comparing this plot to a counterpart from plotrix
df <- data.frame(level1 = c(9,9,4,3,34,28), levelsame = c(9,9,4,3,34,28),
title = c("Dir", "Exec. Dir", "Mgr", "Sr. Mgr", "Mgt Princ", "EVP+"))
# assign the plot (hopefully) to an object
empty <- pyramid(df, Laxis = seq(1,35,5), AxisFM = "g", Csize = 0.8, Cgap = .5, Llab = "",
Rlab = "", Clab = "Title", GL = F, Lcol = "blue", Rcol = "blue",
Ldens = -1, main = "Distribution of Levels")
> empty
NULL
同样,如果我将 pyramid call 分配到我的列表中,则什么也不会发生。 pyramid
编辑的 return 列表没有值。
plotlist2[["pyramid"]] <- pyramid(df, Laxis = seq(1,35,5), AxisFM = "g", Csize = 0.8, Cgap = .5, Llab = "",
Rlab = "", Clab = "Title", GL = F, Lcol = "blue", Rcol = "blue",
Ldens = -1, main = "Distribution of Levels")
> plotlist2[1]
[[1]]
NULL
我担心我在一些明显的误解中犯了错误,所以我欢迎被纠正。谢谢。
您可以使用recordPlot()
函数将当前绘图保存到变量。
在你的情况下你可以这样做:
#print the plot
pyramid(df, Laxis = seq(1,35,5), AxisFM = "g", Csize = 0.8, Cgap = .5, Llab = "",
Rlab = "", Clab = "Title", GL = F, Lcol = "blue", Rcol = "blue",
Ldens = -1, main = "Distribution of Levels")
#save the current printed plot
pyrPlot<-recordPlot()
#plot it again
pyrPlot
您可能必须使用 dev.control(displaylist ="enable")
启用显示列表才能正常工作,具体取决于您使用的图形设备