Coder Social home page Coder Social logo

Comments (6)

pvictor avatar pvictor commented on September 17, 2024 2

Last multi.js release support a limit options, so you can do (that's not exactly what you asked, but this is close):

# install dev version
# devtools::install_github("dreamRs/shinyWidgets")

library("shiny")
library("shinyWidgets")

ui <- fluidPage(
  multiInput(
    inputId = "id", label = "Fruits :",
    choices = c("Banana", "Blueberry", "Cherry",
                "Coconut", "Grapefruit", "Kiwi",
                "Lemon", "Lime", "Mango", "Orange",
                "Papaya"),
    options = list(limit = 1),  #  <-------- here
    width = "350px"
  ),
  verbatimTextOutput(outputId = "res")
)

server <- function(input, output, session) {
  output$res <- renderPrint({
    input$id
  })
}

shinyApp(ui = ui, server = server)

from shinywidgets.

pvictor avatar pvictor commented on September 17, 2024 1

Hi @ViolaPsch ,
No, there's no options to set a minimum number of items to be selected. I don't think I know of any widget with this option.
My solution will be to display an informative text under the widget and then use req(length(input$id) > 3) in reactive context to be sure there more than 3 items selected (you can also look to ?shiny::need).

Here's an example with an alert info:

library(shiny)
library(shinyWidgets)


ui <- fluidPage(
 tags$h3("Fruit selector"),
 fluidRow(
   column(
     width = 4,
     multiInput(
       inputId = "id",
       label = "Fruits :",
       choices = c("Banana", "Blueberry", "Cherry",
                   "Coconut", "Grapefruit", "Kiwi",
                   "Lemon", "Lime", "Mango", "Orange",
                   "Papaya"),
       selected = "Banana", 
       width = "100%"
     ),
     uiOutput(outputId = "info")
   ),
   column(
     width = 8,
     verbatimTextOutput(outputId = "res")
   )
 )
)

server <- function(input, output, session) {
  
  output$info <- renderUI({
    if (length(input$id) < 3) {
      alert(
        icon("info"), 
        "You need to eat at least 3 fruits! (", 3 - length(input$id), "more to pick)",
        status = "info"
      )
    }
  })
  
  output$res <- renderPrint({
    input$id
  })
}

shinyApp(ui = ui, server = server)

Victor

from shinywidgets.

pvictor avatar pvictor commented on September 17, 2024

I'm glad you find the package useful!

I don't think that's possible with this plugin... (without hardcoding it in js, which can be a solution but will take some times).
You can find the JavaScript lib here : https://github.com/Fabianlindfors/multi.js
I'll look into alternative or similar widgets.

Victor

from shinywidgets.

ViolaPsch avatar ViolaPsch commented on September 17, 2024

Hey,
I use multiInput for displaying flags and am wondering whether there is a way to do the opposite @BetweenTwoTests aked: Can I set a minimum for the number of items that should be selected?
Thank you 🙏,
Viola

from shinywidgets.

ViolaPsch avatar ViolaPsch commented on September 17, 2024

Thank you, @pvictor! This is a good idea. 🙂

from shinywidgets.

ccmerry avatar ccmerry commented on September 17, 2024

I believe the "limit" option no longer works. It was working for me a few months ago. After I updated R, it is not working. I ran the example @pvictor provided back in 2018 and experienced the same issue. No items in choices are able to be selected or deselected.

from shinywidgets.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.