numeric value format for condition #123
Replies: 5 comments 19 replies
-
Great examples here. I'll separate these examples though as they are actually asking two different things. 1: Can we encode numbers as numbers and not strings?We've encoded all question values as strings as it's just simply easier to work with on the backend for a whole lot of reasons. This is also the behavior for most shiny widgets (e.g. see this SO issue). I also feel like if we set this as a standard, then it's easier for users to know what to expect when writing their surveys. For example, in the example above, you can just wrap the input in sd_show_if(
as.numeric(input$buy_preference) <= 3 ~ "what_to_buy"
) So we probably won't make this change and we'll keep everything as strings. 2: How should we use multi-option responses in
|
Beta Was this translation helpful? Give feedback.
-
2: How should we use multi-option responses in show_if conditions?
This is a bigger challenge, as the way we've written things is to store the results inside a single concatenated string. For example, if a user chose "Car" and "Boat" in your kind_transport example question above, then this would be stored in the data under the kind_transport column as "4, 5". We do this primarily so the resulting data is easier to store.
Some things aren't too bad with this structure. Like in your example, we could write the condition like this:
input$kind_transport == "1, 4" ~ "buy_preference"
Or you could write a simple function to do the more complex work of converting the value back into a vector
Perhaps it would be worthwhile for sd to provide utility functions to convert to/from the comma-delimited string representation. E.g., something along the lines of
```r
str <- 'A, B, C'
vals <- c('A','B','C')
sd_str2vals <- function(str)
{
strsplit(str, ',', fixed = TRUE) |> unlist()
}
sd_vals2str <- function(vals)
{
paste(vals, collapse=', ')
}
sd_str2vals(str)
sd_vals2str(vals)
```
… and then using the vector, something like this:
transport_less_than_two <- function(input) {
x <- as.numeric(strsplit(input$kind_transport, ", ")[[1]])
return(length(x) <= 2)
}
sd_show_if(
transport_less_than_two() ~ "buy_preference"
)
This is certainly more work, but it's a pretty complex condition, and I don't know how else to specify it.
The only alternative I can think of for is to split the data into multiple columns for mc_multi type questions, similar to how we do with matrix type questions. In your example, instead of have just one kind_transport column, we would have 5 columns: kind_transport_1, kind_transport_2, kind_transport_3, kind_transport_4, kind_transport_5. I'm not a big fan of this design though. It also requires that we choose a value to store in these columns to indicate selection, like 1 and 0 or maybe TRUE and FALSE. With this design, the same condition would look something like this:
input$kind_transport_1 & input$kind_transport_4 ~ "buy_preference"
—
Reply to this email directly, view it on GitHub <#123 (comment)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/ABO4GX33PIPJV4UTKGOHC2DZ3PP2FAVCNFSM6AAAAABP445YVSVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTAOJTHA2DKMY>.
You are receiving this because you are subscribed to this thread.
|
Beta Was this translation helpful? Give feedback.
-
Okay, so pulling together some of the comments here, it seems like it would be helpful to have some sort of function like
I don't think I would include something like Finally, about the point @StefanMunnes made regarding working with the data post-survey for multi-response questions, I agree that one would probably want to convert the data into dummy-coded columns. This could be handled separately, e.g. inside |
Beta Was this translation helpful? Give feedback.
-
Hi @StefanMunnes ! Please check out our new demo survey, as well as its GitHub repo. Now we have 4 scenarios. as.numeric(input$car_number) > 1 ~ "ev_ownership" The 4th scenario is where we applied multiple inputs (there are 2 conditions): input$fav_fruits %in% c("apple", "banana") ~ "apple_or_banana",
length(input$fav_fruits) > 3 ~ "fruit_number" These above conditions should comply your needs. I hope they are helpful. |
Beta Was this translation helpful? Give feedback.
-
We updated the docs now to highlight some of the ways you can set up conditions in more detail. We now have a separate page on conditional control, and in it we have a section on common conditions. Hopefully this will help direct future users to ways to set up some of these more complex conditions. |
Beta Was this translation helpful? Give feedback.
-
Description
I have noticed that numeric values for mc or mc_multi variables are converted to strings and are also joined with a "," for multiple answers. At least for the last case I understand that this might be necessary for export to the database, but for internal reference it makes things more difficult:
SURVEY.R
APP.R
These are some example where it could be handy to reference directly to the numeric values, provided as option values of the question.
And I would also mention, that it would be handy if the numeric values would be stored as numbers in the database. Specially for scales but also factor variables, we would transform them back anyway.
Beta Was this translation helpful? Give feedback.
All reactions