{"action":"created","issue":{"url":"https://api.github.com/repos/jpgattuso/seacarb-git/issues/53","repository_url":"https://api.github.com/repos/jpgattuso/seacarb-git","labels_url":"https://api.github.com/repos/jpgattuso/seacarb-git/issues/53/labels{/name}","comments_url":"https://api.github.com/repos/jpgattuso/seacarb-git/issues/53/comments","events_url":"https://api.github.com/repos/jpgattuso/seacarb-git/issues/53/events","html_url":"https://github.com/jpgattuso/seacarb-git/issues/53","id":291384800,"number":53,"title":"Avoiding inputs of incorrect lengths","user":{"login":"umihoshijima","id":6562593,"avatar_url":"https://avatars0.githubusercontent.com/u/6562593?v=4","gravatar_id":"","url":"https://api.github.com/users/umihoshijima","html_url":"https://github.com/umihoshijima","followers_url":"https://api.github.com/users/umihoshijima/followers","following_url":"https://api.github.com/users/umihoshijima/following{/other_user}","gists_url":"https://api.github.com/users/umihoshijima/gists{/gist_id}","starred_url":"https://api.github.com/users/umihoshijima/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/umihoshijima/subscriptions","organizations_url":"https://api.github.com/users/umihoshijima/orgs","repos_url":"https://api.github.com/users/umihoshijima/repos","events_url":"https://api.github.com/users/umihoshijima/events{/privacy}","received_events_url":"https://api.github.com/users/umihoshijima/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":3,"created_at":"2018-01-24T22:33:50Z","updated_at":"2018-02-09T10:39:04Z","closed_at":null,"author_association":"CONTRIBUTOR","body":"In going through the function code, I became curious about the way that seacarb handles input variables of varying lengths. Currently, all functions look for the input variable with the maximum length, then force all shorter variables to that length by repeating the first value of that variable. This does deal with the majority of situations, where input values _should_ be either the same length, or 1. However, if for some reason the user passes two inputs of different lengths (e.g. 4 and 5), the current way of dealing with input vector lengths would not throw an error, nor would it return 4 values. It would instead use the first value in the input of length 4 for all five calculations. \r\n\r\nThe matlab CO2SYS does things a similar way, but has another measure to stop this from occurring; that is, they make sure that there are no more than two unique vector lengths being inputted. The idea with this is to allow for constant inputs (e.g. all calculations with constant salinity), with all other values being equal. However, it would not throw an error for the first case presented (inputs of length 4 and 5). \r\n\r\nThe solution here is possibly to implement a line of code such that inputs can only be of length 1, or of the length of the maximum input. One way to do this may be: \r\n```\r\n\"Kf\" <-\r\nfunction(S=35,T=25,P=0,kf='x',pHscale=\"T\",Ks_p0=0,Ks_p=0,warn=\"y\"){\r\n    # create vector of input lengths\r\n    input_lengths = c(length(S), length(T), length(P), length(kf), length(pHscale), length(Ks_p0), length(Ks_p))\r\n\r\n    # create nK, or max vector length\r\n    nK = max(input_lengths)\r\n\r\n    # stop function if any input length is not 1 or the max value in input_lengths. \r\n    # this error text is copied from CO2SYS. \r\n    if (sum(! input_lengths %in% c(1, nK))>0){\r\n    stop('INPUT ERROR: Input vectors must all be of same length, or of length 1. ')    \r\n    }\r\n    \r\n    if(length(S)!=nK){S <- rep(S[1], nK)}\r\n    if(length(T)!=nK){T <- rep(T[1], nK)}\r\n    if(length(P)!=nK){P <- rep(P[1], nK)}\r\n    if(length(kf)!=nK){kf <- rep(kf[1], nK)}\r\n    if(length(pHscale)!=nK){pHscale <- rep(pHscale[1], nK)}\r\n \r\n    ### continue function as normal. \r\n```\r\n\r\nThis should only allow the rest of the function to run if the correct assumption is met: that all inputs have the same length, unless they are of length one. \r\n\r\nIf this is of interest, I would be happy to fork and work on it. I thought I would ask before putting the work in. \r\n "},"comment":{"url":"https://api.github.com/repos/jpgattuso/seacarb-git/issues/comments/364398567","html_url":"https://github.com/jpgattuso/seacarb-git/issues/53#issuecomment-364398567","issue_url":"https://api.github.com/repos/jpgattuso/seacarb-git/issues/53","id":364398567,"user":{"login":"jpgattuso","id":3158301,"avatar_url":"https://avatars3.githubusercontent.com/u/3158301?v=4","gravatar_id":"","url":"https://api.github.com/users/jpgattuso","html_url":"https://github.com/jpgattuso","followers_url":"https://api.github.com/users/jpgattuso/followers","following_url":"https://api.github.com/users/jpgattuso/following{/other_user}","gists_url":"https://api.github.com/users/jpgattuso/gists{/gist_id}","starred_url":"https://api.github.com/users/jpgattuso/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jpgattuso/subscriptions","organizations_url":"https://api.github.com/users/jpgattuso/orgs","repos_url":"https://api.github.com/users/jpgattuso/repos","events_url":"https://api.github.com/users/jpgattuso/events{/privacy}","received_events_url":"https://api.github.com/users/jpgattuso/received_events","type":"User","site_admin":false},"created_at":"2018-02-09T10:39:04Z","updated_at":"2018-02-09T10:39:04Z","author_association":"OWNER","body":"Hi @umihoshijima sure, go ahead. I suggest that you do that using branch Candidate_3.2.5 in which I have tried to fix issue #54 raised by CRAN. Many thanks for spotting this problem and for proposing to fix it."}}
{"actor":{"display_login":"jpgattuso"}}