使一个功能交互闪亮

问题描述:

一个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

是的,这是正确的。 –

希望我理解正确的问题,但这里是你如何能做到这一点:

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)