使函数在shiny中交互

使函数在shiny中交互,shiny,Shiny,一个txt.filedata包含一个整数矩阵,每列5个整数,1000行 所以如果我们按 data 我们得到这个输出 96520 69850 ... 36884 通过这个我们可以得到一个随机的行 getnumbers <- sample(data,1, replace=FALSE) 我如何在Shinny中实现这个R代码,以便使用ubuntu使其具有交互性 希望我能正确理解你的问题,但以下是你可以做到的: library(shiny) data <- matrix(round(ru

一个txt.file
data
包含一个整数矩阵,每列5个整数,1000行

所以如果我们按

data
我们得到这个输出

96520
69850
...
36884
通过这个我们可以得到一个随机的行

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

我如何在Shinny中实现这个R代码,以便使用ubuntu使其具有交互性

希望我能正确理解你的问题,但以下是你可以做到的:

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)
库(闪亮)

数据,所以您想从数据集中随机选择一行,获取用户输入并查看它是否与数据集中的下一行匹配?
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)