如果你想有一個情節對象的「載體」,最簡單的方法可能是將其保存在list
。使用paste()
爲您的劇情創建一個名稱,然後將其添加到列表:
# Create a list to hold the plot objects.
pltList <- list()
for(i in 2:15){
# Get data, perform analysis, ect.
# Create plot name.
pltName <- paste('a', i, sep = '')
# Store a plot in the list using the name as an index.
# Note that the plotting function used must return an *object*.
# Functions from the `graphics` package, such as `plot`, do not return objects.
pltList[[ pltName ]] <- some_plotting_function()
}
如果你不想地塊存儲在列表和字面想創建一個新的對象,它有名字包含在pltName
,那麼你可以使用assign()
:
# Use assign to create a new object in the Global Environment
# that gets it's name from the value of pltName and it's contents
# from the results of plot()
assign(pltName, plot(), envir = .GlobalEnv)
謝謝你們的答案 - 他們是非常有幫助的。 – womble 2009-11-29 20:03:11