-
Notifications
You must be signed in to change notification settings - Fork 12
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
Better facilitate piping with sp
inputs
#42
Comments
@ateucher I guess this is subjective. Personally, I am still not convinced. One issue is debugging. Debugging pipes is not a good experience imo, and then if you write the equivalent pipe-less solution, the function will run different code, so the error might not come up. Just one example of how non-pure functions are dangerous. |
@ateucher, could you preserve your |
Surely, @gaborcsardi has a point, and there's sometimes a tradeoff between a "clean" DSL. That said it is surely possible, as it is almost the same as what jqr is doing. There the json query is built up by each verb and only the last one in the chain will trigger the actual jq interpreter. I guess you need to consider how you feel about the pros and cons and make up your mind. |
There is some discussion on |
Also, how do you know what the next function in the pipeline expects as input? That function may come from another package or wherever. Do you check if it is coming from your package? I think this'll get really tricky to implement and probably also fragile. But I might miss the easy workaround.... |
Maybe as default |
Yes, but even it is not last, it might need to return and |
@gaborcsardi another good point about debugging. And yes, it would need to be able to detect whether the next function in the pipe was from rmapshaper. It does seem like a potential house of cards... @timelyportfolio I think even more efficient than preserving the v8 context would be to pass the original unaltered json all the way through, collecting and assembling mapshaper commands as we go (perhaps in an attribute), then apply the whole thing at once in the last step - similar to what @smbache mentioned wrt jqr. Thanks all for your thoughts! I will think on it some more and poke around in jqr, but am leaning towards @gaborcsardi's advice and try not to be to clever ;) |
That seems like a pretty nice way to allow user override on a bit of a black-box process... |
I think a better (but still tricky) way of solving these kind of problems, is to pass an object that can behave both as This might not be possible at this point, but in general, you can have an R6 object that has two active bindings: |
+1 for R6 idea |
For any of the methods for
sp
objects, the function converts it tojson
(character
) before sending it into theV8
context along with commands for processing, and ajson
character object is returned from theV8
context. This is then converted back to ansp
object before being returned to the user.If the function is being used in a magrittr pipe chain with other
rmapshaper
functions, it should not perform the final conversion tosp
, rather it should be able to return ajson
object to be passed to the nextrmapshaper
function in the chain so that the expensivesp -> json -> sp
operation doesn't need to happen at each step. Finally, in the last step in the chain, the resulting object should be converted tosp
to be returned to the user.Current behaviour
Preferred behaviour
This could be achieved by:
The function detects that is being piped into another
rmapshaper
function and so doesn't convert the return value tosp
, rather returns a json character string to be piped into the next function. The last function in the chain knows that it is last and thus converts and returns ansp
objectHave a
return_class
argument that has defaultclass(input)
, but can be set tojson
or one of theSpatial
classes depending where in the chain it is:Just advise users to convert to
json
before doing a pipe, as I currently do in the vignette and README:The text was updated successfully, but these errors were encountered: