R storing reactive data as a vector

I am try build an R Shiny app that calculates and average grade based on user inputs in the UI. The UI reads in a data set of predefined grades which will be over-written by the user using an 'Editable' data table in the UI. This data table looks like the below:

Subject Include Test_One_Grade Test_Two_Grade Test_Three_Grade
Math YES 67 66 63
Biology YES 62 65 70
English YES 72 71 72
French NO 53 52 56
Chemistry YES 57 54 60
Physics YES 46 44 39

The 'Include' column is intend to allow users to group subjects together, for example if I wanted to run analysis on the three science subjects I would type 'YES' next to Biology, Chemistry and Physics, leaving the rest as 'NO'.

A problem arises when I try to carry out this grouping due to the reactivity of the values. I am wondering is it possible to have the table update and remain reactive to a point but once updated the value become static vectors again?

Below is the code I am using for the app:

# Importing libraries needed
library(shiny)
library(dplyr)
library(DT)
library(readxl)

# Importing Data and converting to data frame
input_raw <- read_excel("Grade Inputs Dataset loaded in here")
input_data <- data.frame(input_raw)


### pre-server Side
modFunction <- function(input, output, session, data,reset) {
  
  v <- reactiveValues(data = data)
  
  proxy = dataTableProxy("mod_table")
  
  observeEvent(input$mod_table_cell_edit, {
    print(names(v$data))
    info = input$mod_table_cell_edit
    str(info)
    i = info$row
    j = info$col
    k = info$value
    str(info)
    
    #Isolate stops tables updating automatically when something is changed, allows for actual editing
    isolate(
      if (j %in% match(c("Include","Test_One_Grade","Test_Two_Grade","Test_Three_Grade"), names(v$data))) {
        print(match(c("Include","Test_One_Grade","Test_Two_Grade","Test_Three_Grade"), names(v$data)))
        v$data[i, j] <<- DT::coerceValue(k, v$data[i, j])
        print(v$data)
        
      } else {
        stop("You are not supposed to change this column.") # check to stop the user from editing only few columns
      }
    )
    replaceData(proxy, v$data, resetPaging = FALSE)  # replaces data displayed by the updated table
  })
  
  ### Reset Table
  observeEvent(reset(), {
    v$data <- data # default data
  })
  
  print(isolate(colnames(v$data)))
  output$mod_table <- DT::renderDataTable({
    DT::datatable(v$data, editable = TRUE)
    
  })
  
  return(v)
}

modFunctionUI <- function(id) {
  ns <- NS(id)
  DT::dataTableOutput(ns("mod_table"))
}

server = function(input, output) {
  demodata<-input_data
  edited <- callModule(modFunction,"editable", demodata,
                       reset = reactive(input$reset))
  grades_base <- reactiveValues()
  observe(
    {grades_base$data <- edited$data}
  )
  #Create Vectors of inputs
  subjects <- reactive({(grades_base$data[1])})
  include <- reactive({(grades_base$data[2])})
  grade_t_one <- reactive({(grades_base$data[3])})
  grade_t_two <- reactive({(grades_base$data[4])})
  grade_t_three <- reactive({(grades_base$data[5])})
  
  #create subset of Grades from Test One from Subjects defined in UI
  K<-match(grades_base$`Subject`,names(Grade_Test_One))
  K
  
  #Generate Chart to be displayed by UI
  output$plot1 <- renderPlot({
    plot(grade_t_one())
  })
  
}

## Ui
ui <- navbarPage("Pages",
                 tabPanel("Inputs",
                          basicPage(mainPanel(
                            
                            actionButton("reset", "Reset"),
                            tags$hr(),
                            modFunctionUI("editable")
                          ))),
                 tabPanel("Graph 1",
                          basicPage(mainPanel(plotOutput("plot1", height = 350)))),
                 tabPanel("Graph 2")
)

### Shiny App  
shinyApp(ui, server)  

I'd really appreciate any help you may be able to off with this issue.

Thanks a million in advance.



Read more here: https://stackoverflow.com/questions/66282089/r-storing-reactive-data-as-a-vector

Content Attribution

This content was originally published by JP1999JP at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: