【问题标题】:Shiny reactive input add and delete闪亮的反应输入添加和删除
【发布时间】:2020-06-01 05:02:00
【问题描述】:

我正在尝试编写一个闪亮的应用程序,在其中生成一个列表并添加和删除一些元素。

我有一个模块可以将一些东西添加到我的列表中。

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))})

}

然后我调用它两次并连接 2 个不同的输入。现在我想选择要删除的元素,但这不起作用。

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)

也许有人可以告诉我到目前为止出了什么问题?

提前致谢!

【问题讨论】:

    标签: r shiny shiny-reactivity


    【解决方案1】:

    下面是一个似乎可以工作的应用,但我不确定你的应用打算做什么。

    一般来说,比起使用非本地赋值 &lt;&lt;-,更喜欢反应式值 (reactiveVal)。

    代码appended &lt;&lt;- appended()[-input$delete] 不正确。它不会将appended() 的输出替换为其原始值减去input$delete 索引。

    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)
    

    【讨论】:

    • 这正是我试图达到的目标!非常感谢:)
    • @P.G.乐意效劳。请不要忘记accept the answer
    猜你喜欢
    • 2019-07-19
    • 2020-04-11
    • 1970-01-01
    • 2017-11-15
    • 2020-08-18
    • 2020-08-01
    • 1970-01-01
    • 2019-12-19
    相关资源
    最近更新 更多