admin管理员组

文章数量:1357639

The objective of my project was to estimate the sum of number of visits and the total of expected_visiting_duration from the city of Wageningen with 9 zip codes (6701-6709)-to 6 recreation areas having a distance between 1.2 to 3.2 km from the city of Wageningen. These results obtained from the Excel file: fdata152.xlsx with 26945 records and 21 variables with the following names:

1.RecrID
2.recreation_areas
3.zip_code
4.hgpID
5.household_types
6.household_types_definition
7.group_types
8.group_types_definition
9.Time_segment
10.Time_available_per_time_segment(inmin_
11.transport_type_ID
12.transport_type_definition
13.maximum_budget_per_day
14.number_of_persons
15.activity_pattern_definition
16.distances_km
17.total_activities_costs
18.fuel_cost_total
19.expected_costs
20.expected_visiting_duration
21.number_of_visits 

For the construction of the pivot tables the following variables were used:

2.recreation_areas
3.zip_code
4.hgpID
10.Time_available_per_time_segment(inmin_
20.expected_visiting_duration number of visits 
21.number_of_visits

I shall try to simplify my problem with the following 2 R projects:

1.pivottablesmodels3.Rproj (pivot_model3)and
2.pivottablesmodels4.Rproj (pivot_model4)

Both R projects have the same data file: inst/fdata152.xlsx

  1. pivottablesmodels3.Rproj (pivot_model3) I have set up 3 pivot tables according to the package pivotabler following the instructions of the author: Christopher Bailiss. For the pivot_model3.R the following 3 pivot tables have been generated:

    a. Excel sheet: number of visits The rows are the recreation areas and the columns are zip codes. As subrow was introduced the time segment visisted (in minutes) . The folowing constucted variables from the pivottablesr are: Count_of_hgpID (number of units) number_of_visits (the sum) mean_number_of_visits median_number_of_visits

    b. Excel sheet:exp_vis_dur_time_segmentt The rows are the recreation areas and the columns are zip codes. As subrow was introduced the time segment visisted (in minutes). The folowing constucted variables from the pivottablesr are: Count_of_hgpID (number of units) total_exp_visit_duration mean_exp_visit_duration median_exp_visit_duration c. Excel sheet:total_costs distances_km,Count_of_hgpID (number of units), total_fuel_cost, T_activ_costs, N_of_visits, mean_of_visits,N_of_duration and mean_duration .

  2. For the pivot_model4.R the following 4 pivot tables have been generated: The pivot tables of the pivottablesmodels3.Rproj plus the pivot table of percentage of the number of visits per Grand total.

Both projects created the pivot tables on the Excel workbook: Wageningen_crosstables3.xlsx and Wageningen_crosstables4.xlsx correctly. However when I have tried to clean and install for the project: pivottablesmodels3.Rproj this was DONE while to the contrary of the project: pivottablesmodels4.Rproj I got the following message:

==> R CMD INSTALL --preclean --no-multiarch --with-keep.source pivottablesmodels4 installing to library
‘/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library’
installing source package ‘pivottablesmodels4’ ...
** using staged installation
** R
** byte-compile and prepare package for lazy loading
[1] "Sum of number of visits per location"
[1] "calculate the total of expected_visiting_duration"
[1] "total costs"
[1] "additional variables: distance, total fuel cost, total activities cost"
[1] "% number of visits Grand total"
Error in base::union(x, y, ...) :
unused argument (list()) Calls: ... -> envlist -> -> union ->
union.default Execution halted ERROR: lazy loading failed for
package ‘pivottablesmodels4’ removing
‘/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/pivottablesmodels4’

rpivot_model4

#.The objective of my project  was to estimate the sum of number of visits and the total of
#. expected_visiting_duration from the city of Wageningen with 9 zip codes (6701-6709)to 6 recreation
#.areas having a distance between 1.20 to 3.30 km from the city of Wageningen.
#.These results have been   obtained from my model ACRER and are stored on the Excel file fdata152,xls


library(openxlsx)
library(pivottabler)
library(devtools)
library(R6)


fdata152 <- read.xlsx(
  "inst/fdata152.xlsx",sheet = 1,startRow = 1,colNames = TRUE,rowNames = FALSE,rows = NULL,cols = NULL)

wb <- createWorkbook(creator = "Geios Martakis", title = "Wageningen_crosstables4")

print("Sum of number of visits per location")
pt <- PivotTable$new()
pt$addData(fdata152)
pt$addRowDataGroups("recreation_areas",header = "recreation areas", totalCaption = "All_recreation_areas", styleDeclarations = list("color" = "white", "font-weight" = "bold","background-color" = "blue"))
pt$addColumnDataGroups("zip_code",totalCaption = "All_zip_codes", styleDeclarations = list("color" = "white", "font-weight" = "bold","background-color" = "blue"))
pt$addColumnDataGroups("Time_available_per_time_segment",totalCaption = "All_Time_segments",styleDeclarations = list("color" = "black", "font-weight" = "bold","background-color" = "red"))
pt$defineCalculation(calculationName = "Count_hgpID", summariseExpression = "n()", format = "%.2f", visible = TRUE)
pt$defineCalculation(calculationName = "number_of_visits",summariseExpression = "sum(number_of_visits)",format = "%.2f")
pt$defineCalculation(calculationName = "mean_number_of_visits",summariseExpression = "mean(number_of_visits)",format = "%.2f")
pt$defineCalculation(calculationName = "median_number_of_visits",summariseExpression = "median(number_of_visits)",format = "%.2f")
pt$setRowDataGroupHeader(levelNumber = 1, header = "Recreation areas")
pt$renderPivot(exportOptions = list(skipNA = TRUE, skipNaN = TRUE), showRowGroupHeaders = TRUE)
pt$saveHtml("extdata/number_of_visits.html")
addWorksheet(wb, "number_of_visits", gridLines = openxlsx_getOp("gridLines", TRUE))
pt$writeToExcelWorksheet(wb, wsName = "number_of_visits",topRowNumber = 1, leftMostColumnNumber = 1,outputValuesAs = "formattedValueAsNumber",outputHeadingsAs = "formattedValueAsText", applyStyles = TRUE, mapStylesFromCSS = TRUE)
saveWorkbook(wb, "extdata/Wageningen_crosstables4.xlsx", overwrite = TRUE)
rm(pt)


print("calculate the total of expected_visiting_duration")
pt <- PivotTable$new()
pt$addData(fdata152)
pt$addRowDataGroups("recreation_areas",header = "recreation areas",totalCaption = "All_recreation_areas",
                    styleDeclarations = list("color" = "white", "font-weight" = "bold","background-color" = "blue"))
pt$addColumnDataGroups("zip_code",totalCaption = "All_zip_codes",
                       styleDeclarations = list("color" = "white", "font-weight" = "bold","background-color" = "blue"))
pt$addColumnDataGroups("Time_available_per_time_segment",totalCaption = "All_Time_segments",
                               styleDeclarations = list("color" = "black", "font-weight" = "bold","background-color" = "red"))
pt$defineCalculation(calculationName = "Count_hgpID", summariseExpression = "n()", format = "%.2f", visible = TRUE)
pt$defineCalculation(calculationName = "total_exp_visit_duration",
                     summariseExpression = "sum(expected_visiting_duration)",format = "%.2f")
pt$defineCalculation(calculationName = "mean_exp_visit_duration",
                     summariseExpression = "mean(expected_visiting_duration)",format = "%.2f")
pt$defineCalculation(calculationName = "median_exp_visit_duration",summariseExpression = "median(expected_visiting_duration)",format = "%.2f")
pt$setRowDataGroupHeader(levelNumber = 1, header = "Recreation areas")
pt$renderPivot(exportOptions = list(skipNA = TRUE, skipNaN = TRUE),showRowGroupHeaders = TRUE)
pt$saveHtml("extdata/exp_visiting_duration.html")
addWorksheet(wb,"exp_vis_dur_time_segment")
pt$writeToExcelWorksheet(wb = wb, wsName = "exp_vis_dur_time_segment",
topRowNumber = 1, leftMostColumnNumber = 1,outputValuesAs = "formattedValueAsNumber", applyStyles = TRUE, mapStylesFromCSS = TRUE)
saveWorkbook(wb, "extdata/Wageningen_crosstables4.xlsx", overwrite = TRUE)
rm(pt)

print("total costs")
print("additional variables: distance, total fuel cost, total activities cost")
pt <- PivotTable$new()
pt$addData(fdata152)
pt$addRowDataGroups("recreation_areas",header = "recreation areas",totalCaption = "All_recreation_areas", styleDeclarations = list("color" = "white", "font-weight" = "bold","background-color" = "blue"))
pt$addColumnDataGroups("zip_code",totalCaption = "All_zip_codes", styleDeclarations = list("color" = "white", "font-weight" = "bold","background-color" = "blue"))
pt$addColumnDataGroups("Time_available_per_time_segment",totalCaption = "All_Time_segments",
                      styleDeclarations = list("color" = "black", "font-weight" = "bold","background-color" = "red"))
pt$defineCalculation(calculationName = "distances_km",summariseExpression = "mean(as.numeric(distances_km))", format = "%.2f")
pt$defineCalculation(calculationName = "Count_hgpID", summariseExpression = "n()", format = "%.2f", visible = TRUE)
pt$defineCalculation(calculationName = "total_fuel_cost", summariseExpression = "mean(as.numeric(fuel_cost_total))",format = "%.2f")
pt$defineCalculation(calculationName = "T_activ_costs",summariseExpression = "mean(as.numeric(total_activities_costs))",format = "%.2f ")
pt$defineCalculation(calculationName = "N_of_visits", summariseExpression = "sum(number_of_visits)", format = "%.2f")
pt$defineCalculation(calculationName = "mean_of_visits", summariseExpression = "mean(number_of_visits)", format = "%.2f")
pt$defineCalculation(calculationName = "N_of_duration", summariseExpression = "sum(number_of_visits)", format = "%.2f")
pt$defineCalculation(calculationName = "mean_duration", summariseExpression = "mean(expected_visiting_duration)",format = "%.2f")
pt$renderPivot(exportOptions = list(skipNA = TRUE, skipNaN = TRUE),showRowGroupHeaders = TRUE)
pt$saveHtml("extdata/total_cost.html")
addWorksheet(wb, "total_costs")
pt$writeToExcelWorksheet(wb = wb, wsName = "total_costs",topRowNumber = 1, leftMostColumnNumber = 1,outputValuesAs = "formattedValueAsNumber",applyStyles = TRUE)
saveWorkbook(wb, "extdata/Wageningen_crosstables4.xlsx", overwrite = TRUE)
rm(pt)

print("percent of the expected_visiting_duration per Grand total")
pt <- PivotTable$new()
pt$addData(fdata152)
pt$addRowDataGroups("recreation_areas",header = "recreation areas",totalCaption = "All_recreation_areas", styleDeclarations = list("color" = "white", "font-weight" = "bold","background-color" = "blue"))
pt$addColumnDataGroups("zip_code",totalCaption = "All_zip_codes", styleDeclarations = list("color" = "white", "font-weight" = "bold","background-color" = "blue"))
pt$addColumnDataGroups("Time_available_per_time_segment",totalCaption = "All_Time_segments",
                      styleDeclarations = list("color" = "black", "font-weight" = "bold","background-color" = "red"))
pt$defineCalculation(calculationName = "Count_hgpID", summariseExpression = "n()", format = "%.2f", visible = TRUE)
pt$defineCalculation(calculationName = "Sum_expected_visiting_duration", summariseExpression = "sum(expected_visiting_duration)", format = "%.2f", visible = TRUE)
pt$defineCalculation(calculationName = "mean_expected_visiting_duration", summariseExpression = "mean(expected_visiting_duration)", format = "%.2f", visible = TRUE)
filterOverrides <- PivotFilterOverrides$new(pt, removeAllFilters = TRUE)
pt$defineCalculation(calculationName = "GrandTotalRecreationAreas", filters = filterOverrides,
summariseExpression = "sum(expected_visiting_duration)", format = "%.2f", caption = "GrandTotalRecreationAreas")
pt$defineCalculation(calculationName = "Exp_vs_dur_%_GT",type = "calculation", basedOn = c("Sum_expected_visiting_duration","GrandTotalRecreationAreas"),calculationExpression = "values$Sum_expected_visiting_duration/values$GrandTotalRecreationAreas * 100",caption = "% of All", format = "%.2f")
pt$renderPivot(exportOptions = list(skipNA = TRUE, skipNaN = TRUE),showRowGroupHeaders = TRUE)
pt$saveHtml("extdata/exp_visiting_duration_Grand_total.html")
addWorksheet(wb, "Exp_vs_dur_%_GT")
pt$writeToExcelWorksheet(wb = wb, wsName = "Exp_vs_dur_%_GT",topRowNumber = 1, leftMostColumnNumber = 1,outputValuesAs = "formattedValueAsNumber",applyStyles = TRUE)
saveWorkbook(wb, file = 'extdata/Wageningen_crosstables4.xlsx', overwrite = TRUE)
rm(pt)
rm(wb)

本文标签: