Reputation: 73
I have a function that I made for a package that does not work anymore and I have not been able to figure out why. The point of the function is to transform a string of hexadecimal data to a dataframe (links to examples on github). This code is quite long so instead of including it all here, here is a link to the code on github. When I run the code line by line it works perfectly, but when I try to use the function I get this:
> sample1.df <- transformssd(hex_data = sample1)
a: 1.08641362190247
a: 59.1739654541016
a: 0.32277500629425
a: 2.70198535919189
a: 0.127873212099075
a: 0.262394219636917
a: 27.9828720092773
a: 0.140874609351158
a: 0.274696856737137
a: 0.336845397949219
a: 0.452615290880203
a: 0.107537969946861
a: 0.12063916772604
a: 0.198753878474236
a: 0.521674871444702
a: 0.863220930099487
a: 0.765538036823273
Error: Must subset columns with a valid subscript vector.
x Can't convert from <double> to <integer> due to loss of precision.
The values shown here are the values that I want to get inside my dataframe, so it appears that creating the dataframe is the problem. When running rlang::last_error()
and rlang::last_trace()
this is what I get:
> rlang::last_error()
x
+-<error/vctrs_error_subscript_type>
| Must subset columns with a valid subscript vector.
| x Can't convert from <double> to <integer> due to loss of precision.
\-<error/vctrs_error_cast_lossy>
Can't convert from <double> to <integer> due to loss of precision.
Backtrace:
1. xrfr::transformssd(hex_data = sample1)
22. vctrs:::try_catch_impl(...)
27. vctrs:::try_catch_callback(data, NULL)
29. vctrs:::vec_cast.integer.double(...)
30. vctrs::maybe_lossy_cast(out, x, to, lossy, x_arg = x_arg, to_arg = to_arg)
34. vctrs:::stop_lossy_cast(...)
35. vctrs:::stop_vctrs(...)
> rlang::last_trace()
x
+-<error/vctrs_error_subscript_type>
| Must subset columns with a valid subscript vector.
| x Can't convert from <double> to <integer> due to loss of precision.
\-<error/vctrs_error_cast_lossy>
Can't convert from <double> to <integer> due to loss of precision.
Backtrace:
x
1. +-xrfr::transformssd(hex_data = sample1)
2. | \-values.df %>% dplyr::select(a)
3. +-dplyr::select(., a)
4. +-dplyr:::select.data.frame(., a)
5. | \-tidyselect::eval_select(expr(c(...)), .data)
6. | \-tidyselect:::eval_select_impl(...)
7. | +-tidyselect:::with_subscript_errors(...)
8. | | +-base::tryCatch(...)
9. | | | \-base:::tryCatchList(expr, classes, parentenv, handlers)
10. | | | \-base:::tryCatchOne(expr, names, parentenv, handlers[[1L]])
11. | | | \-base:::doTryCatch(return(expr), name, parentenv, handler)
12. | | \-tidyselect:::instrument_base_errors(expr)
13. | | \-base::withCallingHandlers(...)
14. | \-tidyselect:::vars_select_eval(...)
15. | \-tidyselect:::walk_data_tree(expr, data_mask, context_mask)
16. | \-tidyselect:::eval_c(expr, data_mask, context_mask)
17. | \-tidyselect:::reduce_sels(node, data_mask, context_mask, init = init)
18. | \-tidyselect:::walk_data_tree(new, data_mask, context_mask)
19. | \-tidyselect:::as_indices_sel_impl(...)
20. | \-tidyselect:::as_indices_impl(x, vars, strict = strict)
21. | \-vctrs::vec_as_subscript(x, logical = "error")
22. \-vctrs:::try_catch_impl(...)
23. +-base::tryCatch(try_catch_callback(data, NULL), ...)
24. | \-base:::tryCatchList(expr, classes, parentenv, handlers)
25. | \-base:::tryCatchOne(expr, names, parentenv, handlers[[1L]])
26. | \-base:::doTryCatch(return(expr), name, parentenv, handler)
27. \-vctrs:::try_catch_callback(data, NULL)
28. \-(function () ...
29. \-vctrs:::vec_cast.integer.double(...)
30. \-vctrs::maybe_lossy_cast(out, x, to, lossy, x_arg = x_arg, to_arg = to_arg)
31. +-base::withRestarts(...)
32. | \-base:::withOneRestart(expr, restarts[[1L]])
33. | \-base:::doWithOneRestart(return(expr), restart)
34. \-vctrs:::stop_lossy_cast(...)
35. \-vctrs:::stop_vctrs(...)
It looks like the problem is with dplyr::select()
somehow, but I have not been able to figure out why. I would be very grateful if anyone could help me figuring this out, thanks!
The hexadecimal data linked above can be used to test the code on.
Edit
I have not been able to reproduce the error in a shorter function so far, but this code does not create the dataframe that it should. Since the original problem was in creating the dataframe, I thought this might still be helpful in solving the problem.
library(tidyverse)
library(magicfor)
subhex <- c("9A0F8B3F", "24B26C42", "C442A53E")
transformhex <- function(hex_string) {
subhex <- hex_string
magic_for(silent = TRUE)
for (i in 1: length(subhex)) {
a <- readBin(as.raw(strtoi(apply(matrix(strsplit(subhex,"")[[i]],2),2,paste, collapse=""), 16)), "double", size=4)
put(a)
}
values1.df <- magic_result_as_dataframe()
values1.df <- values1.df %>%
dplyr::select(a)
return(values1.df)
}
> transformhex(hex_string = subhex)
a
1 1.086414
2 59.173965
3 0.322775
Like the original function it shows the correct values, but does not create the dataframe. Also like before, this is only a problem when using the function, not when running the code line by line.
Upvotes: 0
Views: 672
Reputation: 73
In case anyone else has this problem in the future I will share the solution that worked for me:
My problem was in using functions from the magicfor
package in my function. Thanks to this answer to another question I was able to change my for
loop so that it works:
values.df <- data.frame()
for (i in 1:length(subsubsplit1)) {
a <- readBin(as.raw(strtoi(apply(matrix(strsplit(subsubsplit1,"")[[i]],2),2,paste, collapse=""), 16)), "double", size=4)
values.df <- rbind(values.df, data.frame(t(a), row.names = NULL))
}
colnames(values.df) <- "kcps"
Upvotes: 1