Re: [R] class of 'try' if error is raised
Yes, CRAN did accept 'if(inherits(e, "try-error"))'. I remember now, when I used the try-construct the first time, I also saw tryCatch and found it a bit too extensive for my purposes. Will look at it again when needed. Thanks to you and Enrico On Sun, 15 Dec 2019 at 16:03, Bert Gunter wrote: > > See ?try which links you to ?tryCatch for the preferred approach. > > Alternatively: if(inherits(e, "try-error")) ## should work and satisfy > CRAN > > -- Bert > > > Bert Gunter > > "The trouble with having an open mind is that people keep coming along and > sticking things into it." > -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) > > > On Sun, Dec 15, 2019 at 6:21 AM Hans W Borchers wrote: >> >> I have been informed by CRAN administrators that the development >> version of R issues warnings for my package(s). Some are easy to mend >> (such as Internet links not working anymore), but this one I don't >> know how to avoid: >> >> Error in if (class(e) == "try-error") { : the condition has length > 1 >> >> I understand that `class` can return more than one value. But what >> would be the appropriate way to catch an error in a construct like >> this: >> >> e <- try(b <- solve(a), silent=TRUE) >> if (class(e) == "try-error") { >> # ... do something >> } >> >> Should I instead compare the class with "matrix" or "array" (or >> both)?. That is, in each case check with a correct result class >> instead of an error? >> >> Thanks, HW >> >> __ >> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Please help translate this program in C++ to R
As a C implementation of merge sort, that is the very reverse of impressive. I would not translate *that* code into anything. There is a fundamental difference between between arrays in C and arrays in R, and it is the same as the difference between Python and R. You are MUCH better to start from high level pseudocode and express that in R than to start from code tangled up with the presuppositions and peculiarities of another language with quite different presuppositions and peculiarities. On Sun, 15 Dec 2019 at 14:57, Александр Дубровский wrote: > > /* Iterative C program for merge sort */ > #include > #include > > /* Function to merge the two haves arr[l..m] and arr[m+1..r] of array arr[] > */ > void merge(int arr[], int l, int m, int r); > > // Utility function to find minimum of two integers > int min(int x, int y) { return (x > > /* Iterative mergesort function to sort arr[0...n-1] */ > void mergeSort(int arr[], int n) > { >int curr_size; // For current size of subarrays to be merged >// curr_size varies from 1 to n/2 >int left_start; // For picking starting index of left subarray >// to be merged > >// Merge subarrays in bottom up manner. First merge subarrays of >// size 1 to create sorted subarrays of size 2, then merge subarrays >// of size 2 to create sorted subarrays of size 4, and so on. >for (curr_size=1; curr_size<=n-1; curr_size = 2*curr_size) >{ >// Pick starting point of different subarrays of current size >for (left_start=0; left_start{ >// Find ending point of left subarray. mid+1 is starting >// point of right >int mid = min(left_start + curr_size - 1, n-1); > >int right_end = min(left_start + 2*curr_size - 1, n-1); > >// Merge Subarrays arr[left_start...mid] & > arr[mid+1...right_end] >merge(arr, left_start, mid, right_end); >} >} > } > > /* Function to merge the two haves arr[l..m] and arr[m+1..r] of array arr[] > */ > void merge(int arr[], int l, int m, int r) > { > int i, j, k; > int n1 = m - l + 1; > int n2 = r - m; > > /* create temp arrays */ > int L[n1], R[n2]; > > /* Copy data to temp arrays L[] and R[] */ > for (i = 0; i < n1; i++) > L[i] = arr[l + i]; > for (j = 0; j < n2; j++) > R[j] = arr[m + 1+ j]; > > /* Merge the temp arrays back into arr[l..r]*/ > i = 0; > j = 0; > k = l; > while (i < n1 && j < n2) > { > if (L[i] <= R[j]) > { > arr[k] = L[i]; > i++; > } > else > { > arr[k] = R[j]; > j++; > } > k++; > } > > /* Copy the remaining elements of L[], if there are any */ > while (i < n1) > { > arr[k] = L[i]; > i++; > k++; > } > > /* Copy the remaining elements of R[], if there are any */ > while (j < n2) > { > arr[k] = R[j]; > j++; > k++; > } > } > > /* Function to print an array */ > void printArray(int A[], int size) > { > int i; > for (i=0; i < size; i++) > printf("%d ", A[i]); > printf("\n"); > } > > /* Driver program to test above functions */ > int main() > { > int arr[] = {12, 11, 13, 5, 6, 7}; > int n = sizeof(arr)/sizeof(arr[0]); > > printf("Given array is \n"); > printArray(arr, n); > > mergeSort(arr, n); > > printf("\nSorted array is \n"); > printArray(arr, n); > return 0; > } > > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Please help translate this program in python to R.
The obvious question is "why?" If you just want to sort stuff, ?sort and ?order tell you about the sorting methods available in R. If you want to translate this specific algorithm into R for some reason, (a) if you don't know enough about array processing in R to do this yourself, how are you going to know enough to *use* it? (b) There is a fundamental difference between arrays in Python and R which means that the algorithm as presented cannot possibly work in R. Here's the difference. >>> def f(x): x[1] = 5 ... >>> a = [1,2,3] >>> f(a) >>> a [1, 5, 3] Arrays in Python are collections of *variables* and if you pass one to a function, the function can *change* the very same array that you passed in. > f <- function (x) x[2] <- 5 > a <- c(1,2,3) > f(a) > a [1] 1 2 3 This is a completely different result. Arrays in R are collections of *values*. R acts *as if* x[i] <- e really meant x <- get("[<-")(x, i, e) computing a whole new array and assigning it to x. There is in fact an actual function that does this update, and get("[<-") returns it. I said "as if", because there are things you can do to make the actual implementation much more efficient, but the *observable behaviour* is as if the entire array were replaced. Note that this has nothing to do with how the two languages pass arguments to functions, it's about what an array *is* and how you work with them. The bottom line is that if you *could* translate this algorithm from Python to R, you *shouldn't*. 321 On Sun, 15 Dec 2019 at 14:56, Александр Дубровский wrote: > > # Iterative Merge sort (Bottom Up) > > # Iterative mergesort function to > # sort arr[0...n-1] > def mergeSort(a): > > current_size = 1 > > # Outer loop for traversing Each > # sub array of current_size > while current_size < len(a) - 1: > > left = 0 > # Inner loop for merge call > # in a sub array > # Each complete Iteration sorts > # the iterating sub array > while left < len(a)-1: > > # mid index = left index of > # sub array + current sub > # array size - 1 > mid = left + current_size - 1 > > # (False result,True result) > # [Condition] Can use current_size > # if 2 * current_size < len(a)-1 > # else len(a)-1 > right = ((2 * current_size + left - 1, > len(a) - 1)[2 * current_size > + left - 1 > len(a)-1]) > > # Merge call for each sub array > merge(a, left, mid, right) > left = left + current_size*2 > > # Increasing sub array size by > # multiple of 2 > current_size = 2 * current_size > > # Merge Function > def merge(a, l, m, r): > n1 = m - l + 1 > n2 = r - m > L = [0] * n1 > R = [0] * n2 > for i in range(0, n1): > L[i] = a[l + i] > for i in range(0, n2): > R[i] = a[m + i + 1] > > i, j, k = 0, 0, l > while i < n1 and j < n2: > if L[i] > R[j]: > a[k] = R[j] > j += 1 > else: > a[k] = L[i] > i += 1 > k += 1 > > while i < n1: > a[k] = L[i] > i += 1 > k += 1 > > while j < n2: > a[k] = R[j] > j += 1 > k += 1 > > > # Driver code > a = [12, 11, 13, 5, 6, 7] > print("Given array is ") > print(a) > > mergeSort(a) > > print("Sorted array is ") > print(a) > > # Contributed by Madhur Chhangani [RCOEM] > > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] class of 'try' if error is raised
See ?try which links you to ?tryCatch for the preferred approach. Alternatively: if(inherits(e, "try-error")) ## should work and satisfy CRAN -- Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Sun, Dec 15, 2019 at 6:21 AM Hans W Borchers wrote: > I have been informed by CRAN administrators that the development > version of R issues warnings for my package(s). Some are easy to mend > (such as Internet links not working anymore), but this one I don't > know how to avoid: > > Error in if (class(e) == "try-error") { : the condition has length > 1 > > I understand that `class` can return more than one value. But what > would be the appropriate way to catch an error in a construct like > this: > > e <- try(b <- solve(a), silent=TRUE) > if (class(e) == "try-error") { > # ... do something > } > > Should I instead compare the class with "matrix" or "array" (or > both)?. That is, in each case check with a correct result class > instead of an error? > > Thanks, HW > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] class of 'try' if error is raised
> "HW" == Hans W Borchers writes: HW> I have been informed by CRAN administrators that the development HW> version of R issues warnings for my package(s). Some are easy to mend HW> (such as Internet links not working anymore), but this one I don't HW> know how to avoid: HW> Error in if (class(e) == "try-error") { : the condition has length > 1 HW> I understand that `class` can return more than one value. But what HW> would be the appropriate way to catch an error in a construct like HW> this: HW> e <- try(b <- solve(a), silent=TRUE) HW> if (class(e) == "try-error") { HW> # ... do something HW> } HW> Should I instead compare the class with "matrix" or "array" (or HW> both)?. That is, in each case check with a correct result class HW> instead of an error? HW> Thanks, HW You should probably use if (inherits(e, "try-error")) { # ... do something } kind regards ENrico -- Enrico Schumann Lucerne, Switzerland http://enricoschumann.net __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] class of 'try' if error is raised
I have been informed by CRAN administrators that the development version of R issues warnings for my package(s). Some are easy to mend (such as Internet links not working anymore), but this one I don't know how to avoid: Error in if (class(e) == "try-error") { : the condition has length > 1 I understand that `class` can return more than one value. But what would be the appropriate way to catch an error in a construct like this: e <- try(b <- solve(a), silent=TRUE) if (class(e) == "try-error") { # ... do something } Should I instead compare the class with "matrix" or "array" (or both)?. That is, in each case check with a correct result class instead of an error? Thanks, HW __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.