New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Set the visibility and captions of calculations depending on data groups #31
Comments
Hi Marco My first impression is that you are attempting to put a lot of logic into the custom calculation function. A custom calculation function is normally only used to calculate cells values rather than changing the structure of the pivot table.
Custom calculation functions are only executed in step 3 - where as the structure is normally set/fixed before this. However, your general requirement makes sense (if I am understanding it all correctly!). You have highlighted an interesting feature requirement that the package doesn't currently support - the need to pass additional arguments to custom calculation functions. The example below works around this by creating three very thin wrapper functions around your bigger custom calculation function. I will add a new feature request for the future around this (so in the future you will be able to pass additional arguments to the custom function directly without needing these thin wrappers). You have also highlighted a need to be able to more selectively add calculations to a pivot table (e.g. to selectively add only the "Value" calculation under "Reference". I will add a new feature request for this too - though will need to give it some thought about how best to implement this. For now however, it is possible to work around the above two things... So the example below (based on your code) removes columns not needed. The example shows two different ways of doing this - a simple (but hard-coded) way and a more dynamic way that doesn't need to hard-code column numbers. There is also one way you could set the captions from within the custom calculation function but I don't really like doing this for the reasons described above (an example of this is included but commented out below) and the code is also quite ugly. The example doesn't set all of the column captions because I couldn't see from the picture what every column caption should be, but hopefully you can use build on these examples for the other columns.
Output: |
Hello, Chris, thanks a lot for your great support! I have now managed to adapt the table with the dynamic variant (findColumnDataGroups) as I wanted. I had actually stuck more to the "general approach to building a pivot" during a first attempt. It would be possible and easy to create AbsDiff and RelDiff before the pivot table and define columnDataGroup valueTypes = Value, RelDiff, AbsDiff. But it gets more complicated with the aggregates. These must be generated in step 3. I also came across the related issue #2. At the same time I have to say: if the aggregation should be as generic as possible (different data, dynamic filters set by the user), then the aggregation is always complex, whether it is carried out in advance or not. The reason why I initially intended to set the visibility of columns and rows in the custom calculation function: I first assumed that only all cells in a column or row would have to be NULL and then it would be deleted auotmatically. This would be analogous to the onlyCombinationsThatExist=FALSE option. |
I use pivottabler to display results of simulations. I have a reference scenario and would like to display result values (e.g. for production quantities and prices). I want to leave it up to the user whether the actual values ("Value" in the example code below) or the absolute ("AbsDiff") and relative ("RelDiff") differences to the reference scenario are displayed as well. For the reference scenario itself it makes no sense to display the differences (to itself).
I use a user-defined function three times to calculate Value, AbsDiff, RelDiff.
I have experimented with some alternatives. For example, the scenarios could be added via addColumnGroup and then different calculations for the reference scenario and the other scenarios could be defined via addColumnCalculationGroups. If I understand it correctly, this implies that the scenarios are always set as top-level and it would not be possible to flexibly change the order of the groups (indicators before scenarios, scenario groups before scenarios or similar).
Also for the display of the units I can't find a satisfying alternative solution instead of adapting the calculation caption. For example, if I introduce another data group for the units, I can display "€/kg" for the indicator value "Price" and "t" for "Production" (or I could display the unit in indicator captions (e.g. "Price [€/kg]") instead of adding an additional column data group). But then "€/kg" is the heading for all calculations (Value, AbsDiff, RelDiff), but the calculation "RelDiff" has the unit "%".
This is the reproducible example without the modified headers and with the RelDiff and Absdiff columns to be removed. The actual aggregation function is not included in the example (11 is the placeholder value).
Schematic representation of the desired changes:
I'm not sure if I should call the issue a feature request, or if I haven't read the documentation carefully enough. I would be grateful for a response.
The text was updated successfully, but these errors were encountered: