Блестящее приложение с Dynami c bs_accordion_sidebar () с использованием insertUI - PullRequest
0 голосов
/ 30 января 2020

Я пытаюсь создать динамическое c блестящее приложение, которое вставляет ползунки в bs_accordion_sidebar.

Кнопка «Добавить» работает хорошо, но я не могу понять, что мне нужно добавить в код кнопки «Удалить», чтобы обновить барплот?

Кроме того, при нажатии на панель название, я думаю, это должно рухнуть и изменить его цвет, но ничего не происходит?

Спасибо за любую помощь!

library(shiny)
library(bsplus)

# global button counter 
cpt <- 0

# function to create a new slider input
newinput <- function(ID, tag){ 

  div(id=ID,

      bs_append(
        tag = tag,
        title_side = ID,
        content_side = NULL,
        content_main = sliderInput( inputId = paste0("slider_",ID),
                                    label = paste0("slider_",ID),
                                    value = 0,
                                    min=0,
                                    max=10)
      )
  )

}

# UI
ui <- shinyUI(fluidPage(

  titlePanel("bs_append and insertUI"),

  sidebarPanel(

    fluidRow(
      actionButton("add", "+"),
      mytag <- bs_accordion_sidebar(id = "accordion",
                                     spec_side = c(width = 4, offset = 0),
                                     spec_main = c(width = 8, offset = 0)),
      div(id = "placeholder"),
      actionButton("delete", "-")
    )
  ),

  mainPanel(
    plotOutput('show_inputs')
  ),

  use_bs_accordion_sidebar() 

))

# SERVER
server <- shinyServer(function(input, output) {

  # reactive function to collect all input values
  AllInputs <- reactive({
    myvalues <- sapply(names(input)[!names(input) %in% c("add", "delete")], function(x) input[[x]])
    print(myvalues)
    return(myvalues)
  })

  # simple output barplot 
  output$show_inputs <- renderPlot({
    barplot(AllInputs())
  })

  # take a dependency on 'add' button
  observeEvent(input$add, {

    cpt <<- cpt + 1 

    insertUI(
      selector ='#placeholder',
      where = "beforeEnd",
      ui = newinput(ID = cpt, 
                    tag = mytag)
    )
  })

  # take a dependency on 'delete' button
  observeEvent(input$delete, {

    removeUI(selector = paste0('#', cpt))

    cpt <<- cpt - 1

  })

})


shinyApp(ui, server)

1 Ответ

0 голосов
/ 31 января 2020

Я нашел ответ здесь: { ссылка } необходимо было обнулить ввод, удаленный с помощью removeUI

вот исправленный код с использованием блестящего js:

library(shiny)
library(bsplus)
library(shinyjs)

# global button counter 
cpt <- 0

# function to create a new slider input
newinput <- function(ID, tag){ 

  div(id=ID,
      bs_append(
        tag = tag,
        title_side = ID,
        content_side = NULL,
        content_main = sliderInput( inputId = paste0("slider_",ID),
                                    label = paste0("slider_",ID),
                                    value = 0,
                                    min=0,
                                    max=10)
      )
  )

}

# UI
ui <- shinyUI(fluidPage(

  titlePanel("bs_append and insertUI"),

  sidebarPanel(

    fluidRow(
      actionButton("add", "+"),
      mytag <- bs_accordion_sidebar(id = "accordion",
                                     spec_side = c(width = 4, offset = 0),
                                     spec_main = c(width = 8, offset = 0)),
      div(id = "placeholder"),
      actionButton("delete", "-")
    )
  ),

  mainPanel(
    plotOutput('show_inputs')
  ),

  useShinyjs(debug = TRUE),
  use_bs_accordion_sidebar() 

))

# SERVER
server <- shinyServer(function(input, output) {

  # reactive function to collect all input values
  AllInputs <- reactive({
    myvalues <- sapply(names(input)[!names(input) %in% c("add", "delete")], function(x) input[[x]])
    myvalues <- unlist(myvalues[!unlist(lapply(myvalues, is.null))])
    print(myvalues)
    return(myvalues)
  })

  # simple output barplot 
  output$show_inputs <- renderPlot({
    barplot(AllInputs())
  })

  # take a dependency on 'add' button
  observeEvent(input$add, {

    cpt <<- cpt + 1 

    insertUI(
      selector ='#placeholder',
      where = "beforeEnd",
      ui = newinput(ID = cpt, 
                    tag = mytag)
    )

  })

  # take a dependency on 'delete' button
  observeEvent(input$delete, {

    removeUI(selector = paste0('#', cpt))
    runjs(paste0('Shiny.onInputChange("slider_',cpt,'", null)'))
    cpt <<- cpt - 1

  })

})


shinyApp(ui, server)

...