更新闪亮的多个输入时的挑战

时间:2018-06-15 07:36:45

标签: r shiny

UpdateSliderInput无效...

大家好,

似乎更新了sliderInput的挑战。所以我想以某种方式开发应用程序,以便可以动态地应用过滤器,其中一个变量需要提供滑块。

任何帮助都可以非常适合。

library(shiny)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      fluidRow(
        column(6, actionButton('addFilter', 'Add filter')),
        offset = 6
      ),
      tags$hr(),
      tags$div(id = 'placeholderAddRemFilt'),
      tags$div(id = 'placeholderFilter'),
      tags$div(id = 'placeholderFilter')
      # width = 4 # sidebar
    ),
    mainPanel(
      tableOutput("data")
    )
  )
)

server <- function(input, output,session) {
  filter <- character(0)

  makeReactiveBinding("aggregFilterObserver")
  aggregFilterObserver <- list()

  observeEvent(input$addFilter, {
    add <- input$addFilter
    filterId <- paste0('Filter_', add)
    colfilterId <- paste0('Col_Filter_', add)
    rowfilterId <- paste0('Row_Filter_', add)
    removeFilterId <- paste0('Remove_Filter_', add)
    headers <- names(mtcars)
    insertUI(
      selector = '#placeholderFilter',
      # ui = tags$div(id = filterId,
      #               actionButton(removeFilterId, label = "Remove filter", style = "float: right;"),
      #               selectInput(colfilterId, label = "Some Filter", choices = as.list(headers), selected = 1),
      #               sliderInput(rowfilterId, label = "Select variable values",
      #                                  min = 1, max = 2, value = 1:4)
      # )
      ui = tags$div(column(9,id = filterId,
                           actionButton(removeFilterId, label = "Remove filter", style = "float: right;"),
                           selectInput(colfilterId, label = "Some Filter", choices = headers, selected = NULL),
                           conditionalPanel(condition = paste0("input.",colfilterId," != 'mpg'"),
                                            checkboxGroupInput(rowfilterId, label = "Select variable values",
                                                               choices = NULL, selected = NULL, width = 4000)),
                           conditionalPanel(condition = paste0("input.",colfilterId," == 'mpg'"),
                                            sliderInput(rowfilterId,
                                                        label = 'select values',
                                                        min = 1,#min(datafile$Age),
                                                        max = 10,#max(datafile$Age),
                                                        value = 1:5))#c(min(datafile$Age),max(datafile$Age))))
      )
      )
    )

    observeEvent(input[[colfilterId]], {

      col <- input[[colfilterId]]
      values <- as.list(unique(mtcars[col]))[[1]]
      print(values)
      print(paste0("example",as.list(unique(mtcars[col]))))
      # 
      # updateCheckboxGroupInput(session, rowfilterId , label = "Select variable    values", 
      #                          choices = values, selected = values, inline = TRUE)
      # 
      updateSliderInput(session, rowfilterId , min = min(values), max = max(values), value = c(min(values),max(values)))
      updateCheckboxGroupInput(session, rowfilterId , label = "Select variable    values", 
                               choices = values, selected = values, inline = TRUE)

      aggregFilterObserver[[filterId]]$col <<- col
      aggregFilterObserver[[filterId]]$rows <<- NULL
    })

    observeEvent(input[[rowfilterId]], {

      rows <- input[[rowfilterId]]

      aggregFilterObserver[[filterId]]$rows <<- rows

    })

    observeEvent(input[[removeFilterId]], {
      removeUI(selector = paste0('#', filterId))

      aggregFilterObserver[[filterId]] <<- NULL

    })
  })

  output$data <- renderTable({

    dataSet <- mtcars

    invisible(lapply(aggregFilterObserver, function(filter){

      dataSet <<- dataSet[which((dataSet[[filter$col]] %in% filter$rows)), ]

    }))

    dataSet
  })
}

shinyApp(ui = ui, server = server)

Mpg值未更新,这是由于conditionalPanel,因为sliderInput没有被更新?

1 个答案:

答案 0 :(得分:2)

除了用于2种输入类型的inputid之外,一切似乎都很完美。

我刚刚为Sliderinput创建了一个变量,它将创建动态输入id。

    private void SetHeader(LinearLayout extraHeader)
    {
        ImageView btnSettings = new ImageView(this);

        btnSettings.SetBackgroundResource(Resource.Drawable.general_btn_dots_horizontal);
        btnSettings.LayoutParameters = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.WrapContent);

        LinearLayout lnLeft = new LinearLayout(this);
        LinearLayout lnCenter = new LinearLayout(this);
        LinearLayout lnRight = new LinearLayout(this);

        extraHeader.Orientation = Orientation.Horizontal;

        LinearLayout.LayoutParams lpWeights = new LinearLayout.LayoutParams(200, 500);
        //lpWeights.Weight = 33;

        lnLeft.SetGravity(GravityFlags.Center);
        lnCenter.SetGravity(GravityFlags.Center);
        lnRight.SetGravity(GravityFlags.Center);

        lnLeft.SetBackgroundColor(Android.Graphics.Color.ParseColor("#222222"));
        lnCenter.SetBackgroundColor(Android.Graphics.Color.ParseColor("#333333"));
        lnRight.SetBackgroundColor(Android.Graphics.Color.ParseColor("#444444"));

        lnLeft.LayoutParameters = lpWeights;
        lnCenter.LayoutParameters = lpWeights;
        lnRight.LayoutParameters = lpWeights;

        lnRight.AddView(btnSettings);

        extraHeader.AddView(lnLeft);
        extraHeader.AddView(lnCenter);
        extraHeader.AddView(lnRight);
    }

请检查并告诉我这是您想要实现的目标。如果需要其他任何东西,请告诉我。