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

后端 未结 1 1372
后悔当初
后悔当初 2020-12-22 06:46

I have a very simple Shiny app, wherein I have a set of data on past customers, and a set of data on 3 new customers. All my data consists only of 2 variables: age and scor

相关标签:
1条回答
  • 2020-12-22 07:02

    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)
    
    0 讨论(0)
提交回复
热议问题