2017-02-10 61 views
0

我有一個可以生成圖表和數據表的Shiny應用程序,圖表上的y軸鏈接到表列中的最大值通過一些用戶輸入過濾。我希望這個相同的值是sliderInput的最大值,所以它會是動態的,因爲每當用戶在下拉列表中選擇其他內容時,值都會改變。將最大sliderInput值鏈接到表列中的最大值

表格基於下拉列表進行過濾,表格內有一個名爲「價格指數」的列。例如,如果用戶選擇「麪包」,我希望最大sliderInput值根據表格中「價格指數」列的最大值進行更改。

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

任何幫助深表感謝。

回答

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]) 
)) 

希望這有助於!

+0

謝謝!我微調了一下: 'as.integer(max(unlist(pricing_data [section_lower == input $ subsec,。('Price Index')])))'' – MidnightDataGeek