【问题标题】:Shiny Add and remove to favourites闪亮 添加和删除到收藏夹
【发布时间】:2019-06-23 00:56:55
【问题描述】:

this Q&A 之后,我对扩展感兴趣。我希望能够在收藏夹列表中添加和删除项目。我可以整理一下,看看如何做到这一点,但无法进行。

我认为它需要:

  1. 不在收藏夹列表中时的“添加到收藏夹按钮”
  2. 收藏夹列表中的“从收藏夹中删除按钮”
  3. 我认为最喜欢的需要是被动的
  4. 非收藏夹需要反应式

对吗?我可以展示我尝试过的内容,但我不确定它是否有用......

library(shiny)
library(shinyWidgets)

ALL.options <- apply(expand.grid(LETTERS, LETTERS), 1, function(x){paste(x, collapse="")})
favourites <- sample(ALL.options, 20)

ui <- fluidPage(

    h3("Favourites:"),
    radioGroupButtons(inputId = "radio", 
        choices = sort(favourites), 
        individual = TRUE, 
        selected = character(0), 
        width="20%"),

    selectizeInput(inputId="select", label = "Other options",
        choices = ALL.options,
        options = list(
            placeholder = '<None selected>',
            onInitialize = I('function() { this.setValue(""); }')
        )
    ),  

    h3("THIS IS YOUR SELECTION:"),

    verbatimTextOutput("choice")

)

server <- function(input, output) {

  ## initialize reactive value
  currentSelected <- reactiveVal(NULL)

  ## update based on radioGroupButtons
  observeEvent(input$radio, {

        currentSelected(input$radio)

      })

  ## update based on selectInput
  observeEvent(input$select, {

        currentSelected(input$select)

      })

  output$choice <- renderPrint({

        validate(need(currentSelected(), "None selected"))

        currentSelected()

      })

}

shinyApp(ui, server)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    假设您想将现有选项添加到收藏夹列表中,以下内容可能会有所帮助:

    library(shiny)
    library(shinyWidgets)
    
    ALL.options <- apply(expand.grid(LETTERS, LETTERS), 1, function(x){paste(x, collapse="")})
    favourites <- sample(ALL.options, 20)
    
    ui <- fluidPage(
    
        h3("Favourites:"),
        radioGroupButtons(inputId = "radio", 
            choices = sort(favourites), 
            individual = TRUE, 
            selected = character(0),
            width="20%"),
    
        ## select to remove from favourites
        selectInput(inputId = "selectRemove", label = "Remove from favourites", 
            choices = c("", sort(favourites)), 
            selected = ""
        ),
    
        actionButton(inputId = "remove", label = "Remove from favourites"),
    
        tags$hr(),
    
        selectInput(inputId="select", label = "Other options",
            choices = c("", ALL.options),
            selected = ""
        ),  
    
        ## select to add to favourites
        selectInput(inputId = "selectAdd", label = "Add to favourites", 
            choices = c("", ALL.options),
            selected = ""
    
        ),
    
        actionButton(inputId = "add", label = "Add to favourites"),
    
        tags$hr(),
    
        h3("THIS IS YOUR SELECTION:"),
    
        verbatimTextOutput("choice")
    
    )
    
    server <- function(input, output, session) {
    
      ## initialize current states as reactive values
      currentStates <- reactiveValues(
          selected = NULL,
          favourites = sort(favourites)
      )
    
      observeEvent(input$add, {
    
            req(input$selectAdd)
    
            ## add to favourites
            currentStates$favourites <- union(currentStates$favourites, input$selectAdd)
    
          })
    
      observeEvent(input$remove, {
    
            req(input$selectRemove)
    
            ## remove from favourites
            currentStates$favourites <- setdiff(currentStates$favourites, input$selectRemove)
    
          })
    
      observeEvent(currentStates$favourites, ignoreInit = TRUE, {
    
            req(currentStates$favourites)
    
            ## update favourites list
            updateRadioGroupButtons(session,
                inputId = "radio",
                choices = sort(currentStates$favourites)
            )
    
            ## update remove from favourites list
            updateSelectInput(session,
                inputId = "selectRemove",
                choices = c("", sort(currentStates$favourites)),
                selected = ""
            )
    
          })
    
      ## update based on radioGroupButtons
      observeEvent(input$radio, {
    
            currentStates$selected <- input$radio
    
          })
    
      ## update based on selectInput
      observeEvent(input$select, {
    
            currentStates$selected <- input$select
    
          })
    
      output$choice <- renderPrint({
    
            validate(need(currentStates$selected, "None selected"))
    
            currentStates$selected
    
          })
    
    }
    
    shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 2018-02-14
      • 2020-06-01
      • 2018-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多