2017-03-01 64 views
0
ui <- fluidPage(
    sliderInput("obs", "Number of observations:", 
       min = 0, max = 1000, value = 500 
), 
    plotOutput("distPlot") 
) 

# Server logic 
server <- function(input, output) { 
    output$distPlot <- renderPlot({ 
    hist(rnorm(input$obs)) 
    }) 
} 

# Complete app with UI and server components 
shinyApp(ui, server) 

我有一個簡單的應用程序與sliderInput用戶可以切換和選擇觀察的數量。有沒有辦法對此進行修改,以便在此滑塊功能之上,用戶可以將他/她所需數量的觀察值輸入到一個框中,並且該輸入將反映在生成的直方圖中?我希望用戶能夠靈活地擁有滑塊,並且能夠快速輸入精確值,而無需一直依賴滑塊。如何修改shiny中的sliderInput以便用戶可以直接輸入值?

回答

0

是這樣的?

ui <- fluidPage(
      numericInput("obs_numeric", "Number of observations", min = 0, max = 500, value = 500), 
      sliderInput("obs", "Number of observations:", 
         min = 0, max = 1000, value = 500 
      ), 
      plotOutput("distPlot") 
    ) 

    # Server logic 
    server <- function(input, output, session) { 
      output$distPlot <- renderPlot({ 
        hist(rnorm(input$obs)) 
      }) 
      observeEvent(input$obs, { 
        updateNumericInput(session, "obs_numeric", value = input$obs) 
      }) 
      observeEvent(input$obs_numeric, { 
        updateSliderInput(session, "obs", 
             value = input$obs_numeric) 
      }) 
    } 

    # Complete app with UI and server components 
    shinyApp(ui, server) 
相關問題