【问题标题】:Link the max sliderInput value to the max value within a table column将最大滑块输入值链接到表格列中的最大值
【发布时间】:2017-06-28 21:10:33
【问题描述】:

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

根据下拉列表过滤表格,表格中有一个名为“价格指数”的列。例如,如果用户选择“面包”,我希望最大滑块输入值根据表中“价格指数”列的最大值进行更改。

这是我的 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

非常感谢任何帮助。

【问题讨论】:

    标签: shiny dt


    【解决方案1】:

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

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

    但是您遇到的特定错误是因为上面返回的行似乎是一个列表。 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])
    ))
    

    希望这会有所帮助!

    【讨论】:

    • 谢谢!我稍微调整了一下:as.integer(max(unlist(pricing_data[section_lower == input$subsec, .(Price Index)])))
    猜你喜欢
    • 2016-11-25
    • 2020-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多