Reputation: 938
I have a Shiny app that generates a chart and a table of data, the y-axis on the chart is linked to the maximum value in a table column which is filtered by some user input. I want this same value to be the maximum value on the sliderInput, so it would be dynamic in that every time a user selects something else in the dropdown, the value would change.
The table is filtered based on a dropdown and within the table is a column called 'Price Index'. If the user selects 'Bread' for example I want the max sliderInput value to change based on the max of the 'Price Index' column within the table.
Here is my Shiny code minus the function which sits above the server function.
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)
I tried this within the server function but I had an error in the console:
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)
})
The error was Warning: Error in max: invalid 'type' (list) of argument
Any help is much appreciated.
Upvotes: 0
Views: 273
Reputation: 919
I'm not sure if there's another problem underlying this and I obviously don't know your data well enough to understand what this returns:
DT::datatable(
pricing_data[pricing_data$section_lower == input$subsec, .(`Price Index`)][1])
BUT the particular error you're getting is because whatever the line above is returning seems to be a list. The max
function doesn't like lists. For instance, both of these work:
max(1,2,3)
max(c(1,2,3))
But the following does not work:
max(list(1,2,3))
In those situations (if you prefer to leave the first code chunk unaltered), using unlist
might be enough (just like this, obviously silly in this case, also works: max(unlist(list(1,2,3)))
):
val <- max(unlist(DT::datatable(
pricing_data[pricing_data$section_lower == input$subsec, .(`Price Index`)][1])
))
Hope this helps!
Upvotes: 1