Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Input type to allow rankings #6

Closed
priyankagagneja opened this issue Feb 7, 2021 · 9 comments · Fixed by #10
Closed

Input type to allow rankings #6

priyankagagneja opened this issue Feb 7, 2021 · 9 comments · Fixed by #10
Labels
enhancement New feature or request

Comments

@priyankagagneja
Copy link

I am needing to create a survey which asks users to rank a couple of options on a scale of 1-4.
20210206_215540

@jdtrat jdtrat added the enhancement New feature or request label Feb 7, 2021
@jdtrat
Copy link
Owner

jdtrat commented Feb 7, 2021

Would something like Shiny's sliderInput work?

library(shiny)
ui <- fluidPage(
  shiny::sliderInput("skillA", "How would you rate skill A?",
                     min = 0, max = 4, value = 2)
)
server <- function(input, output, session) {}
shinyApp(ui, server)

Screen Shot 2021-02-06 at 10 09 12 PM

@priyankagagneja
Copy link
Author

priyankagagneja commented Feb 7, 2021 via email

@jdtrat
Copy link
Owner

jdtrat commented Feb 8, 2021

Should users be allowed to select multiple buttons per row, or no?

@priyankagagneja
Copy link
Author

priyankagagneja commented Feb 8, 2021 via email

jdtrat added a commit that referenced this issue Feb 23, 2021
jdtrat added a commit that referenced this issue Feb 27, 2021
jdtrat added a commit that referenced this issue Feb 27, 2021
Add Rank Question Input, closing #6.
@mayeulk
Copy link

mayeulk commented May 19, 2021

Hi,
Thanks a lot for this package!
From the tests I made, it is possible to select only one radio button for the entire matrix of buttons.
Here is a reproducible example:

#remotes::install_github("jdtrat/shinysurveys", ref = "26837fbb3365a56b861feca40502eeaa721e303c") # 5 May 2021
library(shiny)
library(shinysurveys)

df <- data.frame(question = c("What is your favorite food?", "Rank the software according to your preference:", "Rank the software according to your preference:"),
                 option = c("Your Answer",  "R", "S+"),
                 input_type = c("text", "rank_{{2}}", "rank_{{2}}"), 
                 input_id = c("favorite_food", "software", "software"),
                 dependence = rep(NA, 3),
                 dependence_value = rep(NA, 3),
                 required = rep(F, 3)
                 )

ui <- fluidPage(
  surveyOutput(df = df,
               survey_title = "Hello, World!",
               survey_description = "Welcome! This is a demo survey showing off the {shinysurveys} package.")
)

server <- function(input, output, session) {
  renderSurvey(df = df)
  
  observeEvent(input$submit, {
    showModal(modalDialog(
      title = "Congrats, you completed your first shinysurvey!",
      "You can customize what actions happen when a user finishes a survey using input$submit."
    ))
  })
}

shinyApp(ui, server)

(with R 4.0.3, shiny_1.6.0, shinysurveys_0.1.2-dev, on Ubuntu and Firefox)
With the example above, when I select 1 for R then 2 for S+, the 1 for R gets unselected.
Maybe I'm doing it the wrong way? If so, an working example would help.
Below is the DOM in Firefox:

<div class="ranking">
              <div class="ranking-title">Rank the software according to your preference:</div>
              <div id="software" class="form-group shiny-input-radiogroup shiny-input-container shiny-input-container-inline shiny-bound-input" role="radiogroup" aria-labelledby="software-label">
                <label class="control-label" id="software-label" for="software">R</label>
                <div class="shiny-options-group">
                  <label class="radio-inline">
                    <input type="radio" name="software" value="1">
                    <span>1</span>
                  </label>
                  <label class="radio-inline">
                    <input type="radio" name="software" value="2">
                    <span>2</span>
                  </label>
                </div>
              </div>
              <div id="software" class="form-group shiny-input-radiogroup shiny-input-container shiny-input-container-inline" role="radiogroup" aria-labelledby="software-label">
                <label class="control-label" id="software-label" for="software">S+</label>
                <div class="shiny-options-group">
                  <label class="radio-inline">
                    <input type="radio" name="software" value="1">
                    <span>1</span>
                  </label>
                  <label class="radio-inline">
                    <input type="radio" name="software" value="2">
                    <span>2</span>
                  </label>
                </div>
              </div>
            </div>

If, with Firefox webdeveloper tools, I replace the 'name' of the two input values for a single line (say, "S+"), as below, the behaviour is different:

                <label class="control-label" id="software-label" for="software">S+</label>
                <div class="shiny-options-group">
                  <label class="radio-inline">
                    <input type="radio" value="1" name="software2">
                    <span>1</span>
                  </label>
                  <label class="radio-inline">
                    <input type="radio" value="2" name="software2">
                    <span>2</span>
                  </label>

With the above, at least I can select a value for R, and a value for S+. Nothing prevents the user to select R as 1st and S+ as 1st (which can make sense: ties). Some control could be implemented later in a way similar to shinyFeedback (see https://cran.r-project.org/web/packages/shinyFeedback/vignettes/shinyFeedback-intro.html ).
Thanks.
Mayeul

@mayeulk
Copy link

mayeulk commented May 19, 2021

Also, it might be interesting to have a look at the shinyRadioMatrix package (https://github.com/szelepke/shinyRadioMatrix ).

@jdtrat
Copy link
Owner

jdtrat commented Jun 5, 2021

Hi @mayeulk! Thanks for the looking into this. I believe this happens because the input ID for both rows of the ranking input are the same, so when you interact with it one overrides the other. Does the following fix it for you?

library(shiny)
library(shinysurveys)

df <- data.frame(question = c("What is your favorite food?", "Rank the software according to your preference:", "Rank the software according to your preference:"),
                 option = c("Your Answer",  "R", "S+"),
                 input_type = c("text", "rank_{{2}}", "rank_{{2}}"), 
                 # Note the input IDs are specific for the language option
                 input_id = c("favorite_food", "software_R", "software_S+"),
                 dependence = rep(NA, 3),
                 dependence_value = rep(NA, 3),
                 required = rep(F, 3)
)

ui <- fluidPage(
  surveyOutput(df = df,
               survey_title = "Hello, World!",
               survey_description = "Welcome! This is a demo survey showing off the {shinysurveys} package.")
)

server <- function(input, output, session) {
  renderSurvey(df = df)
  
  observeEvent(input$submit, {
    showModal(modalDialog(
      title = "Congrats, you completed your first shinysurvey!",
      "You can customize what actions happen when a user finishes a survey using input$submit."
    ))
  })
}

shinyApp(ui, server)

@mayeulk
Copy link

mayeulk commented Jun 5, 2021

Hello,
yes, it works fine now, thanks a lot!
Mayeul Kauffmann

@jdtrat
Copy link
Owner

jdtrat commented Jun 12, 2021

Note that in the development version, I have superseded the rank input type in favor of a matrix one. @mayeulk, see your example reproduced below:

library(shiny)
#remotes::install_github("jdtrat/shinysurveys@extend-shinysurveys")
library(shinysurveys)
df <- data.frame(question = c("What is your favorite food?", rep("R",2), rep("S+",2)),
                 option = c("Your Answer",  rep(c("Least Favorite", "Favorite"), 2)),
                 input_type = c("text", rep("matrix",4)), 
                 # Note the input IDs are specific for the language option
                 input_id = c("favorite_food", rep("rankLanguage",4)),
                 dependence = rep(NA, 5),
                 dependence_value = rep(NA, 5),
                 required = rep(F, 5)
)

ui <- fluidPage(
  surveyOutput(df = df,
               survey_title = "Hello, World!",
               survey_description = "Welcome! This is a demo survey showing off the {shinysurveys} package.")
)

server <- function(input, output, session) {
  renderSurvey(df = df)
  
  observeEvent(input$submit, {
    showModal(modalDialog(
      title = "Congrats, you completed your first shinysurvey!",
      "You can customize what actions happen when a user finishes a survey using input$submit."
    ))
    print(input$rankLanguage)
  })
}

# When printed in console:
# question_id question_type       response
#           r        matrix                       Favorite
#           s        matrix.                Least Favorite

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants