Ethan
Ethan

Reputation: 11

R/ Shiny read csv as data and make some if-then-else statement and show the results?

I am not sure if it makes sense. But I am looking for some help with using Shiny. Here is what I need, first, read some csv file on the shiny server. Second, by clicking a button as an if-then-else statement proceeds each observation's value of data sorted somehow.

csv:

no,money,penalty
1,10000,1000
2,20000,1000
3,30000,2000

if-then-else statement:

if money >10000 then 1 else 0
if penalty >1000 then 1 else 0

here is the r-shiny code I am working on:

# Load packages
library(shiny)

if (interactive()) {

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      fileInput("file1", "Choose CSV File",
        accept = c(
          "text/csv",
          "text/comma-separated-values,text/plain",
          ".csv")
        ),
      tags$hr(),
      checkboxInput("header", "Header", TRUE)
    ),
    mainPanel(
      tableOutput("contents")
    )
  )
)

server <- function(input, output) {
  output$contents <- renderTable({
    # input$file1 will be NULL initially. After the user selects
    # and uploads a file, it will be a data frame with 'name',
    # 'size', 'type', and 'datapath' columns. The 'datapath'
    # column will contain the local filenames where the data can
    # be found.
    inFile <- input$file1

    if (is.null(inFile))
      return(NULL)

    read.csv(inFile$datapath, header = input$header)
  })
}

shinyApp(ui, server)
}

Thank you for your help ahead.

Upvotes: 0

Views: 1412

Answers (1)

Rahul Agarwal
Rahul Agarwal

Reputation: 4100

I have taken below df as an example

df <- data.frame(no =c(1:3),money=c(9999:10001),penalty=c(999:1001))

Below is the complete code which will work if you input any file with column names like the above df

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      fileInput("file1", "Choose CSV File",
                accept = c(
                  "text/csv",
                  "text/comma-separated-values,text/plain",
                  ".csv")
      ),
      tags$hr(),
      checkboxInput("header", "Header", TRUE),actionButton("sort","Do Sorting")
    ),
    mainPanel(
      tableOutput("contents"),tableOutput("sortedcontents")
    )
  )
)

server <- function(input, output) {

  rawInputData = reactive({
    rawData = input$file1


    if(!is.null(rawData)) {
      data = read.csv(rawData$datapath);
    } else {
      return(NULL);
    }

  });


  output$contents <- renderTable({
    newData = rawInputData()  
    if(is.null(newData))
      return();
    newData;
  })

  sorting = reactive({
    if(input$sort){
      newData = rawInputData()
      newData$moneysort <- ifelse(newData$money >=10000, 1, 0)
      newData$penaltysort <- ifelse(newData$penalty >=1000, 1, 0)
       }
    newData

  })
  output$sortedcontents <- renderTable({
    newData = sorting()  
    if(is.null(newData))
      return();
    newData;
  })

}


}
shinyApp(ui, server)

If you are looking something like this, let me know I can fine tune it a bit.

Upvotes: 2

Related Questions