The software that I am developing is a 'Sample Selection Software' which using RStudio. The software will behave like this. User uploads Excel document. Then, the user will click 'Submit" button. After that, the software will automatically select certain number of samples depends on the number of rows in the Excel document, and display it. I already have R code for uploading Excel file interface and 'Submit' button interface. I also have a separate R code that reads specific Excel file and if-else statement that will select a number of samples depending on the number of rows in the Excel file. My problem is I do not know how to combine this two separate codes.
The R code for uploading file and submit button interface are as follows:
library(shiny)
library(xlsx)
ui <- fluidPage(
titlePanel("KPMG"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose xlsx file',
accept = c(".xlsx")
),
actionButton('submit', "Submit")
),
mainPanel(
tableOutput("contents")
)
)
)
server <- function(input, output) {
output$contents <- renderTable({
inFile <- input$file1
if(is.null(inFile))
return(NULL)
file.rename(inFile$datapath,
paste(inFile$datapath, ".xlsx", sep = ""))
read.xlsx(paste(inFile$datapath, ".xlsx", sep = ""), 1)
})
}
shinyApp(ui = ui, server = server)
The R code that reads specific Excel file and if-else statement that will select a number of samples depending on the number of rows in the Excel file are as follows:
library(xlsx)
wb <- read.xlsx("CompanyList.xlsx", sheetIndex = 1, )
nrow(wb) -> rows
if (rows == 1) {
wb[sample(rows, 1), ]
} else
if (rows >= 2 & rows <= 4) {
wb[sample(rows, 1), ]
} else
if (rows >= 5 & rows <= 12) {
wb[sample(rows, 2), ]
} else
if (rows >= 13 & rows <= 52) {
wb[sample(rows, 5), ]
} else
if (rows >= 53 & rows <= 365) {
wb[sample(rows, 15), ]
} else
if (rows > 365) {
wb[sample(rows, 25), ]
}