Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upcsv_to_hier not working as expected when only one element is spitted out by str_split #107
Comments
|
If I understand correctly, a minimal reproducible example would be... # works as expected
data <- read.table(header = FALSE, text = "
a-b-c 124
a2-b2 42")
sunburstR::sunburst(data)
# does not work as expected
data <- read.table(header = FALSE, text = "
a-b-c 124
a2 42")
sunburstR::sunburst(data)ultimately, I believe this comes down to the difference between these... data.frame(t(c("a")), stringsAsFactors = FALSE)
#> t.c..a...
#> 1 a
data.frame(t(c("a", "b")), stringsAsFactors = FALSE)
#> X1 X2
#> 1 a bwhich probably means the Or, that bit could be re-written in a way that avoids the # instead of this
dplyr::bind_rows(
lapply(
strsplit(as.character(csv[[1]]), delim),
function(rw) data.frame(t(rw), stringsAsFactors = FALSE)
)
)
#> X1 X2 X3 t.rw.
#> 1 a b c <NA>
#> 2 <NA> <NA> <NA> a2
# this?
read.delim(header = FALSE, sep = delim, text = paste(csv[[1]], collapse = "\n"))
#> V1 V2 V3
#> 1 a b c
#> 2 a2 |
|
That is 100% correct. Your minimal example are completely illustrating the issue. Not only Using One thing though, I haven't been able to locate the function |
|
|
|
Right. It seems that it does not work on my machine :
|
|
You have to use 3 |
|
My bad. I should have known better! Thanks ! |
|
Coming back to the original issue, I've just tested what you suggested ie
And it does not quite work as expected since In other words this
produces this
Using
gives
|
|
good point! Just to be clear, I am not the maintainer of this project, just a former contributor, so I'm merely making suggestions. I doubt that @timelyportfolio will want to add May I make one suggestion to you @homer3018... in your examples you repeatedly use this very complex process of creating a list object... list(c("a","b","c") %>% paste0(collapse = "-"),
c("a2") %>% paste0(collapse = "-"),
c("a","b","c") %>% paste0(collapse = "-"),
c("a2") %>% paste0(collapse = "-"),
c("a","b","c") %>% paste0(collapse = "-"),
c("a3","b3","c3","d3") %>% paste0(collapse = "-"))That structure is very difficult to quickly interpret, and it also requires a 3rd party package to be loaded for the list("a-b-c",
"a2",
"a-b-c",
"a2",
"a-b-c",
"a3-b3-c3-d3")which would then allow the reader to focus on where the actual bug exists rather than trying to error check an unnecessarily complex object creation. |
|
Apologies for that, I copied/pasted things from my actual code in which I have to resort to the pipe and paste commands, I should have simplified it. I agree that That's mainly why I've not opened a pull request. Surely there is a better way of fixing this. Thanks for your support so far. I guess it's up to @timelyportfolio now. |
|
Not sure what exactly you're trying to achieve, but if the goal is to be able to pass data to fix_data <- function(data, delim = "-") {
node_data <- data[[1]]
node_data <- plyr::ldply(strsplit(as.character(node_data), delim), rbind)
node_data$size = data[[2]]
d3r::d3_nest(node_data, value_cols = "size")
}
data <- read.table(header = FALSE, text = "
a-b-c 1
x 1
d-e-f 1
y 1
g-h-i 1
a3-b3-c3-d3 1")
data
#> V1 V2
#> 1 a-b-c 1
#> 2 x 1
#> 3 d-e-f 1
#> 4 y 1
#> 5 g-h-i 1
#> 6 a3-b3-c3-d3 1
# does not work as expected
sunburstR::sunburst(data)
# works as expected
sunburstR::sunburst(fix_data(data)) |
|
Well my point exactly. I'm doing precisely what you described where I shouldn't, since |
|
Ok... I thought because you're "very annoyed" that you hadn't found a suitable workaround. @timelyportfolio will get to this when they get to it, and I'm confident they'll come up with a good solution. |
|
@homer3018 thanks for filing the issue and @cjyetman thanks so much for stepping in with a quick reply. You are correct in that I do not want to add any more dependencies (actually given the time I would like to start removing dependencies). I plan to work through the potential solutions discussed and will report back today. As a note, the |
|
@homer3018 @cjyetman I pushed some changes to
|
|
Works for me! (using the dev version obviously) |
|
All good as well from my perspective, good work |
|
Thanks @cjyetman @homer3018 I will submit to CRAN today. |

Let's suppose we have this list
There is no separator found for a2, and csv_to_hier currently reports it in the column
t.rwdue to thelapplycall:gives
that's obviously not right.
Using
plyr::ldplywithrbindwould do just fine in such case :gives
Due to this, the following code
any(is.na(df[,1]))kicks in and make a mess (specifically withidx_notna <- which(!is.na(df[,1]))thendf[idx_notna,2] <- df[idx_notna,1]it deletes parts of the hierarchy and does not fix the original issue since we cannot predict the position of the columnt.rw.If
csvhasn't got any NA, then we shouldn't need such checks.Because of that sund2b works but is not correct.