闪亮的模态用作过滤器-重新打开时值重置

时间:2018-11-13 13:00:35

标签: r shiny modal-dialog shinydashboard

我试图将我的所有过滤器容纳在一个模态中(出于外观目的),但它没有达到我的期望。

  1. 最初,在调用模态之前,图不使用其值
  2. 模态不会保存当前状态,因此当我再次打开模态时,所有输入都返回其原始起始值

下面是一个示例代码,用于比较两种方法(侧边栏中的常规过滤器和模式中的过滤器)。玩模态,看看:

rm(list=ls())

library(shiny)
library (shinydashboard)

header <- dashboardHeader(title = "Test")

sidebar <- dashboardSidebar(
  sliderInput(inputId = "bins1",
              label = "Number of bins:",
              min = 1,
              max = 50,
              value = 30),
  actionButton("filterBT", "Filters")
)

body <- dashboardBody(
  fluidRow(
      box(
        title = "Graph using sidebar filter", status = "primary", solidHeader = TRUE, width = 6,
        plotOutput("distPlot1")
      ),
      box(
        title = "Graph using button filter", status = "primary", solidHeader = TRUE, width = 6,
        plotOutput("distPlot2")
      )
  )
)

ui <- dashboardPage(header, sidebar, body)


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

  myModal <- function() {
    div(id = "test",
        modalDialog(uiOutput("Network"),
                    br(),
                    br(),
                    easyClose = TRUE, title = "Filter Selections")
    )
  }

  # open modal on button click
  observeEvent(input$filterBT,
               ignoreNULL = TRUE,   # Show modal on start up
               showModal(myModal())
  )

  output$Network <- renderUI ({
    sliderInput(inputId = "bins2",
                label = "Number of bins:",
                min = 1,
                max = 50,
                value = 30)
  })

  output$distPlot1 <- renderPlot({

    x    <- faithful$waiting
    bins <- seq(min(x), max(x), length.out = input$bins1 + 1)

    hist(x, breaks = bins, col = "#75AADB", border = "white",
         xlab = "Waiting time to next eruption (in mins)",
         main = "Histogram of waiting times")

  })

  output$distPlot2 <- renderPlot({

    x    <- faithful$waiting
    bins <- seq(min(x), max(x), length.out = input$bins2 + 1)

    hist(x, breaks = bins, col = "#75AADB", border = "white",
         xlab = "Waiting time to next eruption (in mins)",
         main = "Histogram of waiting times")

  })


}

shinyApp(ui, server)
  1. 最初,直到您打开“过滤器”按钮,右边的图形才会出现
  2. 如果选择30以外的任何值,请关闭模态,然后打开模态备份。它恢复为默认值30。

0 个答案:

没有答案