Create data frames and plots within shiny using reactive function objects

I am trying to make a simple shiny app where based on the input from selectInput I subset an existing data frame (code below). From this data frame that has been subset, I want to create a few more data frames with some calculations as show in the example below.

The Shiny app

ui = pageWithSidebar(
  headerPanel(""),
  sidebarPanel(
    selectInput('var1', "IP SLNO", choices = rev_21$serial_number)
  ),
  
  mainPanel(
    tableOutput("table")
  )
)
server = function(input, output, session){
  tab = reactive({subset(rev_21,serial_number == input$var1, select = c("serial_number","order_number","follow_up","created_date","completed_data", "which_month",
                                                                              "category","failure_categories","sub_failure_category",
                                                                              "cause","criticallity","order_reason","country","region" ,"...."))})
  
  
  # overview = reactive({ tab()
  #   
  # })
  
  output$table = renderTable({tab()})
  
}


shinyApp(ui, server)

In the app I have been able to subset and create a new data frame from a larger data frame. (all columns are not listed to keep the example df short. I can provide the data if needed)

Using data in this I want to now create two data frames and display them. I am not sure how to access the data passed to the reactive function object tab() to do this.

Examples of the DF I want from this are below (created manually for example purpos)

overview = data.frame(
                      "location" = p1$country[1],
                      "region" = p1$region[1
                      "slno" = p1$serial_number[1]
                      "start_date" = p1$main_start_date[1],
                      "total_order_created" = NROW(unique(p1$order_number)),
                      "days_to_first_order" = p1$first_date[1]-min(p1$created_date),
                      "days_last_recorded_order" = max(p1$created_date) - p1$completed_date[1],
                      "types_of_order_created" = NROW(unique(p1$order_category)),
                      "max_time" = max(p1$completed_on - p1$created_date),
                      "min_time" = min(p1$completed_on - p1$created_date),
                      "total_faults" = NROW((p1$failure_categories[which(!is.na(p1$failure_categories) & p1$failure_categories!="NPF")])),
                      "total_unique_faults" = NROW(unique(p1$failure_categories[which(p1$failure_categories!="NPF")]))
                      )

for (i in 1:length(failure_list) )
  
  {
  days_to_failure$order_number[i] = failure_list[i]
  days_to_failure$failure_name[i] = p1$failure_categories[which(p1$order_number == failure_wo_list[i])]
  days_to_failure$days[i] =  p1$created_date[which(p1$order_number == failure_wo_list[i])]-
    p1$created_date[which(p1$order_number == failure_list[i])]
  days_to_failure$criticality[i] = p1$criticallity[which(p1$order_number == failure_list[i])]

}```


I would like help in understanding how to access the reactive function object (tab) to 
1) create additional DF based on the original subset data frame (tab())
2) how can I have a looping structure as show above to create another data frame,
the looping condition for which is also taken from one of the columns of the subset dataframe. 

Thanks in Advance!!

Hello,

Here is a dummy example to showcase a way of subsetting reactive data and creating new data frames.

library(shiny)

ui <- fluidPage(
  
  selectInput("input1", "Choose", choices = c('A', 'B', 'C')),
  tableOutput("table1"),
  tableOutput("table2")

)

server <- function(input, output) {
  
  set.seed(1) #Only needed for reproducibility 
  myData = data.frame(
    letter = sample(LETTERS[1:3], 20, replace = T),
    value = runif(20)
  )
  
  #Subset data based-on user input
  subsetData = reactive({
    myData[myData$letter %in% input$input1,]
  })

  #Create new df based on first reactive one
   #For loop can be inside this
  summaryData = reactive({
    data.frame(
      #subsetData() is reactive data frame (note brackets)
      letter = subsetData()$letter[1],
      meanVal = mean(subsetData()$value)
    )
  })
  
  #Render the tables
  output$table1 = renderTable({
    subsetData()
  })
  
  output$table2 = renderTable({
    summaryData()
  })
  
}

shinyApp(ui, server)

Hope this helps,
PJ

Thanks for the response @pieterjanvc . I will work through my solution and let you know :slight_smile:

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.