2015-10-19 73 views
-1

一個txt.file data包含整數的矩陣,每列和1000行有5個整數。使一個功能交互閃亮

因此,如果我們按

data 

我們得到如下的輸出

96520 
69850 
... 
36884 

我們可以通過這個

getnumbers <- sample(data,1, replace=FALSE) 

得到一個隨機行通過data任務得到一個隨機行是進入下一行(按a,b,c,d,e)並檢查它是否正確。因此,如果我們有data中的第k個條目,我們想要通過按數字並查看它是否正確來獲得data中的k + 1條目。

check <- function(a,b,c,d,e){ 
if(identical(data[k+1] , c(a,b,c,d,e)) == TRUE) { 
return("Correct") } 
else{return("Not correct")} 

如何在Shiny中實現這個R代碼,以便使用ubuntu進行交互?

+0

所以,你想從你的數據集選擇一行隨機,得到了用戶的輸入,看看它是否下一行匹配數據集? –

+0

是的,這是正確的。 –

回答

1

希望我理解正確的問題,但這裏是你如何能做到這一點:

library(shiny) 
data <- matrix(round(runif(5*3)),ncol=3) 

ui <- shinyUI(fluidPage(
    fluidRow(
    column(6, h4("Randomly Selected Row [k]")), 
    column(6, h4("Nex Row [k+1]")) 
), 
    fluidRow(
    column(6, textOutput("selRow")), 
    column(6, textOutput("nxtRow")) 
), 
    fluidRow(
    column(8, textInput("guessStr","Gues row: ")), 
    column(4, actionButton("guess","guess")) 
), 
    textOutput("guessRes") 
)) 

server <- shinyServer(function(input, output, session) { 
    # Make the current rownumber a reactive 
    r.num <<- 0 
    makeReactiveBinding('r.num') 

    # If rownumber changes update UI 
    observe({ 
    if(is.null(r.num)) return(NULL) 
    output$selRow <- renderPrint({data[r.num,]}) 
    output$nxtRow <- renderPrint({data[r.num+1,]}) 
    }) 

    # Get a row number by random, can't select last row 
    randomRow <- function(){ 
    r.num <<- sample(1:nrow(data)-1, 1) 
    } 

    # If user presses guess button 
    observeEvent(input$guess, { 
    # I convert to numerical but this can be modified to work with characters to 
    input.str <- as.numeric(strsplit(input$guessStr,',')[[1]]) 

    msg <- sprintf("You guessed that the next row is: %s",input$guessStr) 
    if(identical(data[r.num+1,], input.str)){ 
     msg <- paste(msg," , this was correct!") 
    } 
    else{ 
     msg <- paste(msg," , this was wrong") 
    } 
    output$guessRes <- renderPrint({msg}) 
    }) 

    # Initiate the guessing by randmozing a row 
    randomRow() 
}) 

shinyApp(ui = ui, server = server)