我正在为如何在R中获得一个函数的输出而挣扎,我正在创建一个元素列表。显然这很简单,但是,我不能谈这件事。我检查了不同的问题,但没有结果。这可能是一个很简单的问题,而且可能是我错印了一行。
我正在创建的函数的输出有三个元素,我认为它们应该包括在一个列表中:
)
这是我正在使用的函数的版本:
fun <- function(data, tooth_type = c("m1", "m2", "m3"), position = c("U", "L")) {
model <- lda(Species ~ MD + BL, data = data)
# extract the information of the tooth we are working from the Omo database
tooth <- omo_numeric_mdbl[omo_numeric_mdbl$Tooth == tooth_type & omo_numeric_mdbl$Position == position, ]
tooth <- tooth[complete.cases(tooth[, 31:32]), ]
predictions <- model %>%
predict(tooth)
pred <- as.data.frame(predictions$posterior)*100
# plot
lda.data <- cbind(data, predict(model)$x)
ggplot_fun <- ggplot(lda.data, aes(LD1, LD2)) +
geom_point(aes(color = Species))
list(model, pred, ggplot_fun)
}如您所见,函数中有三个对象我想存储:
pred:,这是dataframemodel:,这是lda informationggplot_fun:,这是ggplot图像我试过:
as.list(model, pred, ggplot_fun) -> unsuccessfullist(model, pred, ggplot_fun) -> -> unsuccessfulx <- list(model, pred, ggplot_fun); return(x) -> unsuccessful最后,我希望的是:
x <- fun(data, "m3", "L") # for example
x$pred # show the dataframe (and display as kable, if required)
x$model # show the lda model, and to get all the details inside as x$model$posterior
x$ggplot_fun # display the ggplot function发布于 2022-11-21 11:29:11
我终于解决了这个问题。为了用同样的问题回答其他人,我修改了示例的最后一行:
list(model, pred, ggplot_fun)由这一个
list(LDA = model, Prediction = pred, Ggplot = ggplot_fun)基本上,我把每个元素命名为。
https://stackoverflow.com/questions/74517965
复制相似问题