'Trouble modifying reactive dataframe in Shiny

I'm working on an app which takes up a CSV via reactiveFileReader and applies some functions. I would like to modify the contents of the data before applying these functions.

I understand that reactive objects cannot be modified directly, but I can't even seem to be able to make a new object with the desired modifications (in this case, new column names in the dataframe).

This is where I am at in the server code:

data <- reactiveFileReader(1000, session, "path", read.csv)    
data_new <- reactive({ colnames(data) <- c("Col 1"," Col 2","Col 3") })
output$data <- renderDataTable(data_new())

Unfortunately this yields the error "Error: attempt to set 'colnames' on an object with less than two dimensions".

Any suggestions on how to properly modify and store the data?

Many thanks!



Solution 1:[1]

Try this

ui <- fluidPage(
  uiOutput("data1"),
  uiOutput("data")
)
server <- function(input, output, session) {
  data <- reactiveFileReader(1000, session, "file2.csv", read.csv)    
  data_new <- reactive({ 
    df <- data()
    colnames(df) <- c("Col 1"," Col 2","Col 3") 
    df
  })
  output$data <- renderTable(data_new())
  output$data1 <- renderTable(head(data()))
}

shinyApp(ui, server)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 YBS