根据输入变量动态更改R列名称

时间:2017-11-06 18:27:22

标签: r shiny report

我已经制作了一个带有Shiny前端的报告,该报告使用了一些宏来通知我的决赛桌的人口。

例如 - 用户从下拉列表中选择指标“收入”,根据下面的IF语句从变量“total_revenue”中提取数值。

    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 {


    }

报告本身运行正常,最终结果如下所示,但我真正想做的是根据度量标准选择动态更改col4的名称,在本例中为“收入”

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

这是否可能,如果可行,实现这一目标的最佳方式是什么?

2 个答案:

答案 0 :(得分:0)

您好,您可以使用

更改列的名称
params = new WindowManager.LayoutParams( 
    WindowManager.LayoutParams.WRAP_CONTENT, 
    WindowManager.LayoutParams.WRAP_CONTENT, 
    WindowManager.LayoutParams.TYPE_PRIORITY_PHONE, 
    WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | 
    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | 
    WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | 
    WindowManager.LayoutParams.FLAG_SPLIT_TOUCH | 
    WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM, 
    PixelFormat.TRANSLUCENT);

数据框是包含data.table的变量

答案 1 :(得分:0)

以下是您可能需要的小例子:

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)