简体   繁体   中英

Reactive column names in reactive data frame shiny

I want to create a reactive data frame with a reactive column name in shiny. However this is throwing error. I have provided the code below.. The error is being caused by an () followed by =, but I cant find a way around. Any help will be appreciated

ui.R

library(shiny)
shinyUI(fluidPage(
  titlePanel("Tool"),
  sidebarLayout(
    sidebarPanel(
      textInput("Item","Enter Item Name"),
      div(class='row-fluid',
          div(class='span6', numericInput("sales1","Enter Sales",value=0),numericInput("sales2","Enter Sales",value=0)),
          div(class='span6', numericInput("prices1","Enter price",value=0),numericInput("prices2","Enter price",value=0))
      )),
    mainPanel(
      dataTableOutput("table")
      )
  )
  ))

server.R

library(shiny)
shinyServer(function(input, output) {
  prices<-reactive({
    c(input$prices1,input$prices2)
  })
  sales<-reactive({
    c(input$sales1,input$sales2)
  })
  combined<-reactive({
    data.frame(prices(),sales())
  })
  combined_final<-reactive({
    mutate(combined(),Rev=prices()*sales())
  })
  namerev<-reactive({
    as.character(paste("Rev",input$Item,sep="_"))
  })
  combined_final_rename<-reactive({
    rename_(combined_final(),namerev() ="Rev")
  })
  output$table<-renderDataTable({
    combined_final_rename()
  })
  })

If I understood the question correctly, you might need something like that:

  combined_final_rename<-reactive({
    d <- combined_final()
    colnames(d)[colnames(d)=='Rev'] <- namerev()
    d
  })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM