<<;-不工作-R-从外部函数调用对象
我浏览了这里的论坛,发现<<;-不工作-R-从外部函数调用对象,r,function,dplyr,global-variables,R,Function,Dplyr,Global Variables,我浏览了这里的论坛,发现您可以使用返回(out)作为函数的最后一行,然后在每次需要访问变量时调用函数。您可以使用返回(out)作为函数的最后一行,然后在每次需要访问变量时调用函数。注意:使用注意:使用@sweeI尝试了这一点,但无法访问列。这实际上不是必需的——您不需要使用return函数在函数末尾返回对象。return(out)与out的作用相同,只是它也退出了函数。@sweeI尝试了这一点,但无法访问列。这实际上是不必要的——您不需要使用return函数在函数末尾返回对象。return(ou
您可以使用返回(out)
作为函数的最后一行,然后在每次需要访问变量时调用函数。您可以使用返回(out)
作为函数的最后一行,然后在每次需要访问变量时调用函数。注意:使用注意:使用@sweeI尝试了这一点,但无法访问列。这实际上不是必需的——您不需要使用return函数在函数末尾返回对象。return(out)
与out
的作用相同,只是它也退出了函数。@sweeI尝试了这一点,但无法访问列。这实际上是不必要的——您不需要使用return函数在函数末尾返回对象。return(out)
与out
的作用相同,只是它也退出了函数。这是另一个输入错误。您有billdata
,但将billdata
分配给out
。更正最后一行<代码>输出这是另一个打字错误。您有billdata
,但将billdata
分配给out
。更正最后一行<代码>我们我认为你需要在你的答案中更加坚定地反对使用@dww你是对的。我认为OP已经遇到了许多谴责使用的帖子中的一个,我认为你需要在你的回答中更加强烈地反对使用@dww你是对的。我假设OP已经遇到了许多帖子中的一个,比如,谴责使用
> Billeddata_import <- function(burl="C:\\Users\\mcantwell\\Desktop\\Projects\\M & V Analysis\\Final_Bills.csv"){
+ billeddata<-read.csv(burl,header=TRUE, sep=",",stringsAsFactors = FALSE) %>%
+ mutate(Usage=as.numeric(Usage)) %>%
+ #Service.Begin.Date=as.Date(Service.Begin.Date,format='%m/%d/%Y'),
+ #Service.End.Date=as.Date(Service.End.Date,format='%m/%d/%Y')) %>%
+
+ filter(UOM=="Kw",
+ !is.na(Usage),
+ Service.Description %in% c("Demand","Demand On Peak", "Demand Off Peak", "Dmd Partial Pk")) %>%
+ group_by(Location..,Service.Begin.Date,Service.End.Date) %>%
+ summarise(monthly_peak=max(Usage))
+ out<<-billdata
+ }
> out
Error: object 'out' not found
>
> Billeddata_import()
Error in Billeddata_import() : object 'billdata' not found
#This is an example:
myfun <- function(xdat=df) {
billeddata <- xdat %>% select(-var3) %>%
filter(var1=="treatment5")
out<<-billeddata
}
myfun(df) #You need to run the function!!!
out
# var1 var2 value
# 1 treatment5 group_2 0.005349631
# 2 treatment5 group_2 0.005349631
# 3 treatment5 group_1 0.005349631
df <- structure(list(var1 = structure(c(1L, 2L, 3L, 4L, 5L, 1L, 2L,
3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L), .Label = c("treatment1", "treatment2",
"treatment3", "treatment4", "treatment5"), class = "factor"),
var2 = structure(c(1L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 1L, 1L, 1L), .Label = c("group_1", "group_2"), class = "factor"),
var3 = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 3L, 2L, 2L, 3L,
2L, 3L, 2L, 2L, 3L), .Label = c("C8.0", "C8.1", "C8.2"), class = "factor"),
value = c(0.010056478, 0.009382918, 0.003014983, 0.005349631,
0.005349631, 0.010056478, 0.009382918, 0.003014983, 0.005349631,
0.005349631, 0.010056478, 0.009382918, 0.003014983, 0.005349631,
0.005349631)), .Names = c("var1", "var2", "var3", "value"
), class = "data.frame", row.names = c(NA, -15L))
out <- myfun(df)