2016-12-06 54 views

回答

2

儘量安排喜歡這樣的代碼:

library(shiny) 

ui <- fluidPage(

    # Application title 
    titlePanel("Old Faithful Geyser Data"), 
    actionButton("minus", "Minus"), 
    actionButton("plus", "Plus"), 
    # Sidebar with a slider input for number of bins 
    sidebarLayout(
    sidebarPanel(
     sliderInput("mean2", 
        "Number of bins:", 
        min = 0, 
        max = 1000, 
        value = 500, 
        step= 1) 
    ), 

    # Show a plot of the generated distribution 
    mainPanel(
     plotOutput("distPlot") 
    ) 
) 
) 

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

    v <- reactiveValues(data = 500) 

    observeEvent(input$minus, { 
    v$data <- input$mean2 - 1 
    updateSliderInput(session,"mean2", value = input$mean2 - 1) 
    }) 

    observeEvent(input$plus, { 
    v$data <- input$mean2 + 1 
    updateSliderInput(session,"mean2", value = input$mean2 + 1) 
    }) 

    observeEvent(input$mean2, { 
    v$data <- input$mean2 
    }) 

    output$distPlot <- renderPlot({ 

    # generate bins based on input$bins from ui.R 
    x <- faithful[, 2] 
    bins <- seq(min(x), max(x), length.out = v$data + 1) 

    # draw the histogram with the specified number of bins 
    hist(x, breaks = bins, col = 'darkgray', border = 'white') 

    }) 

} 

shinyApp(ui, server)