2014-10-10 56 views
1

我開發了一個帶有RStudio的Shiny應用程序,它接受輸入,執行查找表的搜索,然後返回一個值。在用戶點擊提交按鈕之前,不應該執行搜索。但是,啓動後,應用會自動執行並返回查找表中的第一組值。在初次搜索後,該應用程序完全按照預期工作。是否可以抑制這個初始搜索(或者有一個默認值),並且在按下提交按鈕時只進行搜索?我可以不存在重複性的代碼,但這裏是我的輸入(server.R)和我的用戶界面(ui.R)代碼的結構:閃亮的應用程序 - 如何在啓動時抑制功能和渲染?

#server.R snippet 
output$Word <- renderText({ 
    predictWord <- input$gram 
    predict.function(predictWord) #User-defined function in global.r file 
    }) 


#ui.R snippet 

tabPanel("Word Prediction", 
    sidebarPanel(
    textInput("gram", "Enter up to three words"), 
    submitButton("Predict")), 

mainPanel(
    h4("Word Prediction"), 
    textOutput("predictWord"))) 

回答

1

一種方法是到位的替代actionButtonsubmitButton並將其包裝在if語句中的任何組件。這裏是一個簡單的例子,顯示一個只從Shiny Widget Gallery稍微修改的數字。

require(shiny) 
runApp(list(
    ui = pageWithSidebar(
    headerPanel("actionButton test"), 
    sidebarPanel(
     numericInput("n", "N:", min = 0, max = 100, value = 50), 
     br(), 
     actionButton("goButton", "Go!"), 
     p("Click the button to update the value displayed in the main panel.") 
    ), 
    mainPanel(
     verbatimTextOutput("nText") 
    ) 
), 
    server = function(input, output){ 
    output$nText <- renderText({ 
     # Take a dependency on input$goButton 
     if(input$goButton >= 1){ 

     # Use isolate() to avoid dependency on input$n 
     isolate(input$n) 
     } 
    }) 
    } 
) 
)