Link 最大 sliderInput 值到 table 列中的最大值

Link the max sliderInput value to the max value within a table column

我有一个闪亮的应用程序,它生成一个图表和一个 table 数据,图表上的 y 轴链接到 table 列中的最大值,该列由一些人过滤用户输入。我希望这个相同的值成为 sliderInput 上的最大值,因此它是动态的,因为每次用户在下拉列表中选择其他内容时,该值都会改变。

table 是根据下拉列表过滤的,在 table 中有一个名为 'Price Index' 的列。例如,如果用户选择 'Bread',我希望最大 sliderInput 值根据 table.

中 'Price Index' 列的最大值进行更改

这是我的 Shiny 代码减去了位于服务器函数之上的函数。

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


  output$priceplot <- renderPlot(
    {
      Price_Score(input$s_ranks[1], input$s_ranks[2], input$s_index[1], input$s_index[2], input$subsec)
    }
  )

  output$table <- DT::renderDataTable(
    DT::datatable(
      pricing_data[pricing_data$section_lower == input$subsec]
    )
  )

  session$onSessionEnded(
    function() {
      stopApp()
    }
  )
  onSessionEnded = function(callback) {

    return(.closedCallbacks$register(callback))
  }
}

####
ui <- fluidPage(

  titlePanel("Price Score Optimisation"),
  fluidRow(
    column(3,
           wellPanel(
             h4("Filters"),
             sliderInput("s_index", "Select Price Index Values",
                         0, 350, c(0, 50), step = 10),

             sliderInput("s_ranks", "Select ranks", 0, 22000, value = c(1000, 15000)),

             selectInput(
               "subsec",
               "Subsections",
               choices = unique(as.character(pricing_data$section_lower)),
               selected = TRUE,
               multiple = FALSE,
               selectize = FALSE
             )
           )
    ),
    column(9,
           plotOutput("priceplot")
    )
  ),
  fluidRow(DT::dataTableOutput("table")
  )
)


shinyApp(ui = ui, server = server)

我在服务器功能中试过这个,但我在控制台中遇到错误:

  observe({
    val <- max(DT::datatable(
      pricing_data[pricing_data$section_lower == input$subsec, .(`Price Index`)][1])
    )
    # Control the value, min, max, and step.
    # Step size is 2 when input value is even; 1 when value is odd.
    updateSliderInput(session, "s_index", 
                      min = 0, max = val+50, step = 10)
  })

错误是Warning: Error in max: invalid 'type' (list) of argument

非常感谢任何帮助。

我不确定这背后是否还有其他问题,而且我显然对您的数据了解不够深,无法理解这是什么 returns:

DT::datatable(
  pricing_data[pricing_data$section_lower == input$subsec, .(`Price Index`)][1])

BUT 您遇到的特定错误是因为上面的行返回的内容似乎是一个列表。 max 函数不喜欢列表。例如,这两项工作:

max(1,2,3)
max(c(1,2,3))

但以下有效:

max(list(1,2,3))

在那些情况下(如果你希望保持第一个代码块不变),使用 unlist 可能就足够了(就像这样,在这种情况下显然很愚蠢,也可以工作:max(unlist(list(1,2,3)))) :

val <- max(unlist(DT::datatable(
  pricing_data[pricing_data$section_lower == input$subsec, .(`Price Index`)][1])
))

希望对您有所帮助!