【发布时间】:2019-06-23 00:56:55
【问题描述】:
在this Q&A 之后,我对扩展感兴趣。我希望能够在收藏夹列表中添加和删除项目。我可以整理一下,看看如何做到这一点,但无法进行。
我认为它需要:
- 不在收藏夹列表中时的“添加到收藏夹按钮”
- 收藏夹列表中的“从收藏夹中删除按钮”
- 我认为最喜欢的需要是被动的
- 非收藏夹需要反应式
对吗?我可以展示我尝试过的内容,但我不确定它是否有用......
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)
【问题讨论】: