2016-08-24 42 views
1

我對Shiny非常陌生。滑塊跳過值:使用文本框作爲替代?

考慮以下幾點:

ui.R

library(shiny) 
shinyUI(fluidPage(

    titlePanel("The Linear Equation y = 0.1x"), 

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
    sidebarPanel(
     sliderInput("x", 
        "X-value:", 
        min = 0, 
        max = 100, 
        value = 0, 
        step = 0.1) 
    ), 

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

server.R

library(shiny) 
library(ggplot2) 

shinyServer(function(input, output, session) { 

    f <- function(x){ 
    0.1*x 
    } 


    output$linPlot <- renderPlot({ 

    point_f <- data.frame(x = input$x, 
         y = f(input$x)) 

    ggplot(NULL, aes(x = x)) + 
     stat_function(data = data.frame(x = c(0, 100)), 
        fun = f, geom = 'line', col = 'blue') + 
     geom_point(data = point_f, aes(x = x, y = y), 
       size = 4, col = 'blue') 

    }) 

    session$onSessionEnded(function() { stopApp() }) 

}) 

enter image description here

我遇到的主要問題是滑塊不允許選擇每個0.1。也就是說,當用戶使用滑塊時,會跳過各種值。我期望這個應用程序的用戶將要選擇一個特定的十分之一作爲一個值,而滑塊不能選擇十分之一的事實對於我的目的是不可接受的。

我認爲這是不使用滑塊任意選擇一個解決的問題,所以我也想一個文本框添加到具有以下的事情側邊欄面板:

  • 的價值文本框可以用作輸入來執行滑塊的操作(因此值input$x應該在滑塊和文本輸入中相同)。
  • 隨着滑塊移動,文本框更改其值以匹配滑塊的值。
  • 隨着文本框值的變化,滑塊會移動以匹配文本框的值。
  • 區間[0,100]以外的值對於文本框是不可接受的,並且理想地會吐出錯誤。
  • 文本框的輸入必須是數字,並且格式爲ab.c,其中a,b和c中的每一個都是單位數字。

理想情況下,如果滑塊可以自己處理這個問題,那就太好了,但我還沒有發現任何暗示它可以的東西。

我知道這涉及到textInput,但我不知道如何超越這一步。

+0

你試圖讓1000倍的值(100 * 0.1)成widget小於1000個像素寬,這就是爲什麼它跳過值。如果您將滑塊設置爲瀏覽器的整個寬度,它可能會工作,但它仍然非常複雜。爲什麼不使用一個NumericInput小部件,它提供文本輸入和向上/向下按鈕? – Spacedman

+0

@Spacedman'numericInput'閃亮它像'SpinBox',但它也需要極限範圍manualy(使用可以任意數值 - 超過極限的增長按鈕) – Batanichek

+0

@Spacedman我希望用戶能夠看到趨勢隨着值(因此滑塊)而變化並且能夠放入他們感興趣的確切值(因此是文本框)。 – Clarinetist

回答

0

如果我理解你的權利,你需要觀察和更新,如:

library(shiny) 

ui=shinyUI(fluidPage(

    titlePanel("The Linear Equation y = 0.1x"), 

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
    sidebarPanel(
     sliderInput("x", 
        "X-value:", 
        min = 0, 
        max = 100, 
        value = 0, 
        step = 0.1), 
     numericInput("x_txt","X-value:",min = 0, 
        max = 100, 
        value = 0, 
        step = 0.1) 
    ), 

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

library(shiny) 
library(ggplot2) 

server=shinyServer(function(input, output, session) { 

    f <- function(x){ 
    0.1*x 
    } 
    observeEvent(input$x,{ 
    if(input$x!=input$x_txt){ 
    updateNumericInput(session,"x_txt",value = input$x) 
    } 
    }) 

    observeEvent(input$x_txt,{ 
    if(is.numeric(input$x_txt) & input$x_txt>=0 & input$x_txt<=100){ 


    if(input$x!=input$x_txt){ 
    updateNumericInput(session,"x",value = input$x_txt) 
    }}else{ 
     updateNumericInput(session,"x_txt",value = input$x) 
    } 
    }) 

    point_x=reactive({ 

    }) 


    output$linPlot <- renderPlot({ 

    point_f <- data.frame(x = input$x, 
          y = f(input$x)) 

    ggplot(NULL, aes(x = x)) + 
     stat_function(data = data.frame(x = c(0, 100)), 
        fun = f, geom = 'line', col = 'blue') + 
     geom_point(data = point_f, aes(x = x, y = y), 
       size = 4, col = 'blue') 

    }) 

    session$onSessionEnded(function() { stopApp() }) 

}) 

shinyApp(ui,server) 
+0

這是基本*我在找什麼,但它有一個問題。比方說,用戶在文本框中點擊箭頭的速度太快了。 (嘗試單擊其中一個箭頭並保持一段時間,然後放開。)然後它會導致滑塊和文本框不規律地移動。 – Clarinetist

+0

當我關閉應用程序時,它也給我一個錯誤:'ERROR:[on_request_read] connection reset by peer' – Clarinetist