I've looked through the forums here and figure out that <<-
assign a variable inside a function to a global variable (to be accessible outside the function).
I've done so below, but to no avail - any thoughts?
> 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
>
The object billdata
is a data table that I cleaned up in Billeddata_import()
, and I'm hoping to use it in later functions.
Running the function alone yields:
> Billeddata_import()
Error in Billeddata_import() : object 'billdata' not found
without the out<<-billdata
line, Billeddata_import()
runs fine.