0

I have built a report with a Shiny front-end which uses some macros to inform the population of my final table.

Eg - User selects metric 'Revenue' from dropdown, which pulls in the numerical value from the variable 'total_revenue', as per the IF statement below.

    if(metric == 'Revenue') {      
    report_metric <- 'total_revenue'

    } else if (metric == 'Sales'){
    thing_name <- 'total_sales'

    } else if (metric == 'Refunds'){
    thing_name <- 'total_refunds'

    } else if (metric == 'Cancellations'){
    thing_name <- 'total_cancellations'

    }else {


    }

The report itself works fine and end result looks something like that below, but what I'd really like to do is have the name of col4 be dynamically changed according the metric choice, in this case 'Revenue'

|    date   |  store_number  |     manager     |  report_metric  |
|2017-01-01 |      0286      |  john appleseed |     2,309       |
|2017-01-01 |      0761      |    jane doe     |     1,712       |

Is this possible and if so, what is the best way of achieving this?

Spacedman
  • 92,590
  • 12
  • 140
  • 224
jimiclapton
  • 775
  • 3
  • 14
  • 42
  • 2
    Please try to include a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input data and some code we can run to test with. This seems incomplete at best. What exactly have you tried so far and where exactly are you getting stuck? – MrFlick Nov 06 '17 at 18:31
  • You might want to look into `reactive` expressions in Shiny – acylam Nov 06 '17 at 19:45

2 Answers2

0

Hi you can change the name of a Column with

names(dataframe)[4] <- metric

with dataframe being the variable that contains you data.table

Bertil Baron
  • 4,923
  • 1
  • 15
  • 24
0

Here is small example of what you might want:

library(shiny)
library(DT)

values <- c("Revenue","Sales","Refunds","Cancellations")
names <- c("total_revenue","total_sales","total_refunds","total_cancellations")

df <- mtcars[,1:4]

ui <- fluidPage(
  selectInput("metric","metric",values),
  dataTableOutput("Table")
)

server <- function(input, output, session) {

  mydf <- reactive({
    name <- switch(input$metric,
                   "Revenue" = "total_revenue",
                   "Sales" = "total_sales",
                   "Refunds" = "total_refunds",
                   "Cancellations" = "total_cancellations")
    names(df)[4] <- name
    df
  })
  output$Table <- renderDataTable({mydf()})
}

shinyApp(ui,server)
Pork Chop
  • 28,528
  • 5
  • 63
  • 77