how to change column names in rshiny using reactive function

|▌冷眼眸甩不掉的悲伤 提交于 2021-01-29 09:45:47

问题


I am uploading one csv file and to standardize the code I want to change the column names. so I am using following code:

Prv_mnth_so1 <- reactive({data.frame(lapply(data_uploaded1(),trimws))}) colnames(Prv_mnth_so1()) <- c("GST_ward","Category","order_and_section","combo") but this throws an error Warning: Error in <-: invalid (NULL) left side of assignment 52: server [#12] Error in colnames(Prv_mnth_so1()) <- c("GST_ward", "Category", "order_and_section", : invalid (NULL) left side of assignment

it means I can't assign () operator on right side but I am not able to fix this issue


回答1:


You can only change the values of a reactive inside the reactive itself, because it is basically a function you evaluate (therefore you have to use the brackets).

You can either 1. try to change it directly when creating Prv_mnth_so1 or 2. later in another reactive context:

1.

Prv_mnth_so1 <- reactive({
  new_table <- data.frame(lapply(data_uploaded1(),trimws))
  colnames(new_table) <- c("GST_ward","Category","order_and_section","combo")
  new_table
  })
Prv_mnth_so1 <- reactive({data.frame(lapply(data_uploaded1(),trimws))})

output$table <- renderTable({
  table_data <- Prv_mnth_so1()
  colnames(table_data) <- c("GST_ward","Category","order_and_section","combo")
  table_data
})


来源:https://stackoverflow.com/questions/64349166/how-to-change-column-names-in-rshiny-using-reactive-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!