The .twbx file looks to be dependent on your R server so it will not meaningfully open. My guess here is that the disagregation is causing every instance of a value in the dataset to be returned once - like a Dimension. is there a reason for using that option?
There are a couple of things going on here. I can say that Tableau is actually computing accurate results so there's no particular issue with your R code, here's a view with Analysis->Aggregate Measures->Off and a calculation to show what goes with what for each record and we can see the correct value of Predicted Impressions:
This view that you had questions about is this one:
This view is *not* showing what goes with what, instead it's showing the first N values of Cost sorted from highest to lowest and then the rest are indicated by the ellipses in the bottom right, then the first N values of Impressions from highest to lowest and then the ellipses and so on.
This is due to Tableau's layout engine. Fundamentally Tableau lays out views based on the pills in the view, their values, and some internal logic (more on the logic below). What's happening in this view is that Measure Names on Columns and Measure Values are on text, and Analysis->Aggregate Measures is Off. So with this view there is no pill on Rows to tell Tableau how to vertically lay on the marks and it's using it's internal logic which says "fit as many marks as can fit in a vertical column taking up the whole view and as many columns as will fit given the measure column width" with the sort that I described in the prior paragraph. This logic also explains what's happening in the view in your original post where you'd shrunk the measure column width to a single column, Tableau is still sorting each column from highest to lowest values in that column.
In order to see the "what goes with what" in the first screenshot above I'd added the Order calculated field with the formula RANK_UNIQUE(SUM([Impressions])). With that as a discrete pill on Rows there's now a set of values used to position each mark (i.e.the value of each measure from each row in the data. If you'd like to understand a bit more about how that calculation works, see Show Me *All* The Numbers: Displaying Every Record for Too Tall Data | Drawing with Numbers for more details.
Now a bit more about Tableau's internal logic: It's generally based on the principles/assumptions that a) we've got a lot of records ("a lot" meaning "more than we can or want to manually analyze & review", it could be 5 records or 5 billion), b) want to aggregate across records, and c) that the data has sufficient dimensions/categorical variable(s) (record IDs and/or combinations of fields that generate unique keys) to control the grouping and display of the data at whatever level of detail we need. This comes from Tableau's background as a tool for interactive visual analytics of large data sets, where the data sets are typically databases that meet all those criteria. c) in particular is something that tools like R, Python, Stata, etc. don't really care about because of their iterative process of 1) compute record by record over a data set or result set, 2) generate a result set, 3) repeat. They are starting out as a record-level result and then aggregating to other levels of detail as desired. Whereas Tableau's more database-oriented model is doing set-based operations so if we want to do record-based work then we need key field(s) (i.e. principle c) to tell Tableau to compute over sets of 1 record each.
Coming back to this particular issue: It provided dataset works well with principle a) and b) but does not have any unique key or combination of keys c). So it's a little more challenging to work with in Tableau at a record-level because we don't have any field(s) we can use as dimension(s) in the view to tell Tableau to compute results at a record level. Instead we have to use Analysis->Aggregate Measures->Off in order to work with record-level values and then (for the layout) add the calculated field to get the view to render in a way that makes sense.
Unfortunately Tableau does not have a function to automatically add row/record IDs to the data, here's a feature request for that in Tableau Desktop https://community.tableau.com/ideas/3345 and another request for Tableau Prep https://community.tableau.com/ideas/8714 .
I've attached a workbook with the Order calculated field. Hope this helps!
r linear model fit.twbx 108.6 KB
Thanks Jonathan! This is a very useful explanation. It also helps that in the balance of time between the original post and your response, I worked my way through your Table Calculation wiki which gave me a much broader base of understanding how to integrate R with Tableau. Great work on that one!
I'm glad it was useful for you!