简体   繁体   English

闪亮的反应式输入添加和删除

[英]Shiny reactive input add and delete

I'm trying to write a shiny app where I produce a list and add and delete some elements.我正在尝试编写一个闪亮的应用程序,在其中生成一个列表并添加和删除一些元素。

I have a module to add somethind to my list.我有一个模块可以将某些内容添加到我的列表中。

find_inputUI <- function(id){
  ns <- NS(id)
  tagList(
  sliderInput(ns("first"), "Choose a number:", min=0, max=100, 30),
  radioButtons(ns("second"), "Choose a colour:", choices=c("red", "green", "black")),
  actionButton(ns("press"), "Add to queue"))

}

find_input <- function(input, output, session){
  queue <- list()
 observeEvent(input$press, {
  queue_append <- list(input$first, input$second)
queue <<- append(queue, queue_append )})
 queue_ret <- eventReactive(input$press,{return(list(queue=queue, add=input$press))})

}

Then I call it twice and connect the 2 different inputs.然后我调用它两次并连接两个不同的输入。 Now I want to choose the elements to delete but this doesn't work.现在我想选择要删除的元素,但这不起作用。

source('/cloud/project/Queue/find_input.R')
library(shiny)

ui <- fluidPage(
  tagList(tabsetPanel(
    tabPanel("INPUT 1",
             find_inputUI("input1"),
             verbatimTextOutput("test")),
    tabPanel("INPUT 2",
             find_inputUI("input2")
    )
  ),
  actionButton("combine", "Show combined input"),
  verbatimTextOutput("combination"),
  uiOutput("del")
  )
)

# Define server logic required to draw a histogram
server <- function(input, output, session) {

  input_manual1 <- callModule(find_input,"input1")
  input_manual2 <- callModule(find_input, "input2")
  output$test <- renderPrint({input_manual1()$queue})

  appended <- eventReactive(input$combine, {
    return(append(input_manual1()$queue, input_manual2()$queue))
  })

  output$combination <- renderPrint({appended()})

  output$del <- renderUI({
    input$combine
    tagList(checkboxGroupInput("delete", "Choose do delete", seq(1:length(appended()))),
            actionButton("dodelete", "Delete selected"))
  })
  observeEvent(input$dodelete,{
    appended <<- appended()[-input$delete]
  })

}

# Run the application 
shinyApp(ui = ui, server = server)

Maybe anybody can tell me what's wrong so far?也许有人可以告诉我到目前为止出了什么问题?

Thanks in advance!提前致谢!

Below is an app which seems to work but I'm not sure to understand what your app is intended to do.下面是一个似乎可以工作的应用程序,但我不确定您的应用程序打算做什么。

In general, prefer reactive values ( reactiveVal ) instaed of using the non-local assignment <<- .一般来说,更喜欢使用非局部赋值<<-反应值( reactiveVal )。

The code appended <<- appended()[-input$delete] is not correct. appended <<- appended()[-input$delete]的代码appended <<- appended()[-input$delete]不正确。 It does not replace the output of appended() by its originalvalue minus the input$delete index.它不会用原始值减去input$delete索引来替换appended()的输出。

library(shiny)

find_inputUI <- function(id){
  ns <- NS(id)
  tagList(
    sliderInput(ns("first"), "Choose a number:", min=0, max=100, 30),
    radioButtons(ns("second"), "Choose a colour:", choices=c("red", "green", "black")),
    actionButton(ns("press"), "Add to queue"))

}

find_input <- function(input, output, session){

  queue <- reactiveVal(list())

  observeEvent(input$press, {
    queue_append <- list(input$first, input$second)
    queue(append(queue(), queue_append))
  })

  queue_ret <- eventReactive(input$press, {
    list(queue=queue(), add=input$press)
  })

}

ui <- fluidPage(
  tagList(tabsetPanel(
    tabPanel("INPUT 1",
             find_inputUI("input1"),
             verbatimTextOutput("test")),
    tabPanel("INPUT 2",
             find_inputUI("input2")
    )
  ),
  actionButton("combine", "Show combined input"),
  verbatimTextOutput("combination"),
  uiOutput("del")
  )
)


server <- function(input, output, session) {

  input_manual1 <- callModule(find_input,"input1")
  input_manual2 <- callModule(find_input, "input2")
  output$test <- renderPrint({input_manual1()$queue})

  appended <- reactiveVal(list())
  observeEvent(input$combine, {
    appended(append(input_manual1()$queue, input_manual2()$queue))
  })

  output$combination <- renderPrint({appended()})

  output$del <- renderUI({
    input$combine
    tagList(checkboxGroupInput("delete", "Choose do delete", seq_along(appended())),
            actionButton("dodelete", "Delete selected"))
  })

  observeEvent(input$dodelete,{
    appended(appended()[-as.integer(input$delete)])
  })

}

# Run the application 
shinyApp(ui = ui, server = server)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM