Convert a string representation of list into list

,

In a rshiny app, I have to read and write to a URL.

I'm basically trying to imitate the functionality of the bookmarkbutton(), because the bookmarkbutton doesn't work with our current app design that we have.

I'm having trouble figuring out how to handle lists/vectors. For example I need to convert

> query$yoy_layers
[1] "[\"500k xs 500k\",\"5m xs 1m\"]"

into something like:

> yoy_layers
[1] "500k xs 500k" "5m xs 1m"  

Any help/resources is appreciated. I tried using utils:URLdecode and utils::URLencode but I haven't been able to get it to work when reassigning that back to a shiny element that takes a list/vector.

One approach to this would to first clean up your string up until the elements are only separated by the comma. Then split the string using comma as a separating element. The key here is that to filter out unnecessary characters, you need 'Regex' to match the pattern of what you want to match in your string, and what to replace it with (in this case we replace with empty character "" to simply remove them).

Here's an example:

yoy_layers <- "[\"500k xs 500k\",\"5m xs 1m\"]"
[1] "[\"500k xs 500k\",\"5m xs 1m\"]"

clean_yoy_layers <- gsub(pattern = "\\]|\\[|\\\"",  replacement = "",  x = yoy_layers,  fixed = FALSE)
[1] "500k xs 500k,5m xs 1m"

split_yoy_layers <- strsplit(x = clean_yoy_layers,  split = ",")
[[1]]
[1] "500k xs 500k" "5m xs 1m"   


1 Like

Thank you! This worked well, hopefully it'll work for more complex lists. :slight_smile:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.