0

I am developing a shiny app for selecting a pair of players from the below dataset

structure(list(player = c("Samson", "Tausif", "charles", "vinod", 
"bhsoley", "sugandhey", "mohit", "ocd", "shankar", "sneha")), class = "data.frame", row.names = c(NA, 
-10L))

I am new to r and shiny by reading from multiple sources I am able to develop an app for random selection. the code is as shown below:

ui<-fluidPage(
  fileInput('datafile', 'Choose CSV file',
            accept=c('csv', 'comma-separated-values','.csv')),
  tableOutput('table'),
  tableOutput("table1")
)

server<- function(input, output,session) {
  dataframe<-reactive({
    if (is.null(input$datafile))
      return(NULL)                
    data<-read.csv(input$datafile$datapath)
    data
  })
  
  dataframe1<-reactive({
    if (is.null(input$datafile))
      return(NULL)                
    data1<-read.csv(input$datafile$datapath)
    y<-split(sample(data1),rep(1:5,each=2)) 
    df<-data.frame(matrix(unlist(y), nrow=length(y), byrow=T))
    df
  })
  output$table <- renderTable({
    dataframe()
  })
  output$table1 <- renderTable({
    dataframe1()
  })
}
shinyApp(ui, server)

but each time i run this, it generate the same result however the results are different pair of player in R terminal.

This is screenshot of my app

I am looking for a solution where each time I upload a CSV file it generate different player names.

Priya
  • 1
  • I don't understand what you are trying to accomplish. Are you trying to upload more than one CSV file at a time? Or are you trying remember certain values each time a new file is chosen in the fileInput? – MrFlick Jul 04 '20 at 07:05
  • my app randomly generate pair of names as soon as i upload a file. but each time i am uploading the file it is generating the same pair of name. But i want different pair of names. – Priya Jul 04 '20 at 07:14
  • I think its related to session. the code split(sample(data1),rep(1:5,each=2)) gives five pair of names at random – Priya Jul 04 '20 at 07:15
  • 1
    Oh. You can't call `sample()` like that on a data.frame to shuffle the rows, that shuffles the columns. Instead see this answer on how to mix up the rows: https://stackoverflow.com/questions/6422273/how-to-randomize-or-permute-a-dataframe-rowwise-and-columnwise – MrFlick Jul 04 '20 at 07:19
  • If you want to sample from `data1` try `y <- split(data1[sample(nrow(data1)), ], rep(1:5, each = 2))`. `sample(data1)` will simply return `data1`, i.e. it is always the same and hence you get the same result. – stefan Jul 04 '20 at 07:24
  • thankyou its working ...but all the tables are dsiplaying at one side , how i can change the display – Priya Jul 04 '20 at 07:39
  • instead of uploading a csv file. Can i add names one by one and crate the dataframe and then perform the operations – Priya Jul 04 '20 at 07:48
  • If you have a new question, then you should start a new post. You should not ask new questions in the comments. – MrFlick Jul 04 '20 at 07:50

0 Answers0