Shasha
Shasha

Reputation: 7

How to put if-else condition inside Shiny action button code?

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), ]
                  } 

Upvotes: 0

Views: 2430

Answers (1)

Parfait
Parfait

Reputation: 107652

Simply place your if/else logic inside the renderTable({...}) method using dataframe objects, wb and outdf, to build outputted table through each conditional statement:

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=""))          
      wb <- read.xlsx(paste(inFile$datapath, ".xlsx", sep = ""), 1)

      nrow(wb) -> rows

      if (rows == 1) {
        outdf <- wb[sample(rows, 1), ]
      } else 
        if (rows >= 2 & rows <= 4) {
          outdf <- wb[sample(rows, 1), ]
        } else 
          if (rows >= 5 & rows <= 12) {
            outdf <- wb[sample(rows, 2), ]
          } else 
            if (rows >= 13 & rows <= 52) {
              outdf <- wb[sample(rows, 5), ]
            } else
              if (rows >= 53 & rows <= 365) {
                outdf <- wb[sample(rows, 15), ]
              } else
                if (rows > 365) {
                  outdf <- wb[sample(rows, 25), ]
                } 
      outdf          
    })
}


shinyApp(ui = ui, server = server)

Upvotes: 1

Related Questions