【问题标题】:how to change column names in rshiny using reactive function如何使用反应函数更改 r shiny 中的列名
【发布时间】:2021-01-28 14:45:54
【问题描述】:

我正在上传一个 csv 文件并标准化我想要更改列名的代码。所以我使用以下代码:

Prv_mnth_so1 <- reactive({data.frame(lapply(data_uploaded1(),trimws))}) colnames(Prv_mnth_so1()) <- c("GST_ward","Category","order_and_section","combo") 但这会引发错误 警告:

这意味着我无法在右侧分配 () 运算符,但我无法解决此问题

【问题讨论】:

  • 试试colnames(Prv_mnth_so1) <- c("GST_ward","Category","order_and_section","combo");左边的双括号可能是问题所在。
  • @Lime no,Prv_mnth_so1reactive,所以双括号是可以的(因为 reactive 基本上是你必须执行的函数)。我怀疑它不起作用,因为您不能在 reactive 本身之外更改 reactive 的值

标签: r shiny shinydashboard shiny-server shiny-reactivity


【解决方案1】:

您只能更改 reactive 本身内部的 reactive 的值,因为它基本上是您评估的函数(因此您必须使用方括号)。

您可以 1. 在创建 Prv_mnth_so1 或 2. 稍后在另一个响应式上下文中尝试直接更改它:

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
})

【讨论】:

  • 非常感谢,我尝试了选项 1,如下:output$state_lst &lt;- reactive({ Prv_mnth_so1 &lt;- data.frame(lapply(data_uploaded1(),trimws)) colnames(Prv_mnth_so1) &lt;- c("GST_ward","Category","order_and_section","combo","Product_Number","Orders_Status","Option_Number","SRN", "Quantity_On_Hand","Invoice_Or_Billing_Id","Lc_Amount","Clearance_date") Prv_mnth_so1$state &lt;- sub("\\-.*", "", Prv_mnth_so1$"GST_ward") state_lst &lt;- data.frame(unique(Prv_mnth_so1$state)) state_lst })
  • 但出现以下错误警告:data_uploaded1 <- reactive({ file1 <- input$file1 if(is.null(file1)){return()} read.table(file=file1$datapath, sep=",", header = T, stringsAsFactors = T) }) file1 是上传文件的ID fileInput("file1", "Previous Month close Sales office")
  • 试过选项 2 也有类似的错误:Prv_mnth_so2 &lt;- reactive({ Prv_mnth_so1 &lt;- data.frame(lapply(data_uploaded1(),trimws)) Prv_mnth_so1 }) output$state_lst1 &lt;- reactive ({ Prv_mnth_so2 &lt;- Prv_mnth_so2() colnames(Prv_mnth_so2) &lt;- c("GST_ward","Category","order_and_section","combo","Product_Number","Orders_Status","Option_Number","SRN", "Quantity_On_Hand","Invoice_Or_Billing_Id","Lc_Amount","Clearance_date") Prv_mnth_so2$state &lt;- data.frame(sub("\\-.*", "", Prv_mnth_so2$"GST_ward")) state_lst &lt;- data.frame(unique(Prv_mnth_so2$state)) state_lst })
  • 那么请在上面编辑您的问题,以包含一个完整的最小可重现示例(运行闪亮的应用程序)。可能原因是你的输入数据还没有准备好,所以尝试用需要的输入数据添加req
  • server
猜你喜欢
  • 2021-12-14
  • 2021-11-29
  • 2014-09-06
  • 1970-01-01
  • 1970-01-01
  • 2018-06-23
  • 1970-01-01
  • 2020-06-26
  • 2020-04-23
相关资源
最近更新 更多