Alternate control of a sliderInput between a derived value and user selected value

拜拜、爱过 提交于 2019-11-29 17:58:17

I believe this is the code you want. It's not too complicated, I hope it helps

new_customers <- data.frame(age=c(30, 35, 40), score=c(-1.80,  1.21, -0.07))
historic_customers <- data.frame(age=sample(18:55, 500, replace=T), score=(rnorm(500)))

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

  get_selected_customer <- reactive({
    new_customers[input$cust_no, ]
  })

  observe({
    cust <- get_selected_customer()
    updateSliderInput(session, "age", value = c(cust$age - 5, cust$age + 5))
  })

  subset_historic_customers <- reactive({
    DF <- historic_customers[which((historic_customers$age >= input$age[1]) &
                                     (historic_customers$age <= input$age[2])), ]
    DF
  })

  output$distPlot <- renderPlot({
    plotme <- subset_historic_customers()
    p <- ggplot(data=plotme, aes(x=age, y=score))+ geom_point()
    my_cust_age <- data.frame(get_selected_customer())
    p <- p + geom_vline(data=my_cust_age, aes(xintercept=age))
    p
  })
}

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      numericInput(inputId="cust_no", label="Select new customer:", value=1),
      sliderInput(inputId="age", "Age of historic customer:", min=18, max = 55, value=c(18, 55), step=1, ticks=TRUE)
    ),
    mainPanel(plotOutput("distPlot"))
  )
)

shinyApp(ui = ui, server = server)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!