2016-08-21 52 views
0

我的数据帧:分配动态变量来重塑idvar

structure(list(NEWSEC = c("A1", "A1", "A1", "A1", "A2", "A2", 
"A2"), tiles = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L), .Label = c("1st", 
"2nd", "3rd", "4th"), class = c("ordered", "factor")), AVG = c(360, 
594, 868, 1534, 349, 592, 861)), .Names = c("NEWSEC", "tiles", 
"AVG"), row.names = c(NA, 7L), class = "data.frame") 

它看起来像这样:

NEWSEC tiles AVG 
1  A1 1st 360 
2  A1 2nd 594 
3  A1 3rd 868 
4  A1 4th 1534 
5  A2 1st 349 
6  A2 2nd 592 
7  A2 3rd 861 

当我使用从基础R重塑功能

reshape(df, idvar = "NEWSEC", timevar = "tiles", direction = "wide") 

这工作完全好吧...但我的数据框是通过闪亮的应用程序输入动态生成的,而不是我选择的市场或C,而不是NEWSEC ATEGORY。通常我将输入变量分配给一个对象,并在我的函数中引用该对象。例如:

sec <- input$colvar 

但是,当我尝试类似的方法与重塑它不工作。我的代码到目前为止:

reshape(df, idvar = sec, timevar = "tiles", direction = "wide"). 

我也尝试粘贴变量秒引号,但没有奏效。

reshape(df, idvar = paste(""",sec,""", sep = "), timevar = "tiles", direction = "wide"). 

我不知道这是否正确...但只是尝试过。

回答

1

这部分代码是正确的

sec <- input$colvar 
reshape(df, idvar = sec, timevar = "tiles", direction = "wide") 

前提是你的反应上下文(reactive/render*功能)中做此操作,并规定df已经有了这两个新列。

但是,动态地这两个变量添加到您的数据帧df,让你有一些reactive/eventReactive函数中做到这一点,如例如:

data <- reactive({ 
    # let's pretend that this only happens when some conditions are met 
    new <- df 
    new$Market <- gl(n = 2, k = 4, length = 7, labels = c("Market1", "Market2")) 
    new$Category <- gl(n = 2, k = 4, length = 7, labels = c("Cat1", "Cat2")) 
    new 
    }) 

说,现在要重塑这个根据选定的id变量创建新的动态数据框,然后将其呈现为表格。为此,您必须使用renderTable函数并通过data()访问您的新数据框。

output$new <- renderTable({ 
    # access dynamic data frame "data" via "data()" 
    sec <- input$colvar 
    reshape(data(), idvar = sec, timevar = "tiles", direction = "wide") 
    }) 

完整的示例:

library(shiny) 

df <- structure(list(NEWSEC = c("A1", "A1", "A1", "A1", "A2", "A2", 
          "A2"), tiles = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L), 
          .Label = c("1st", "2nd", "3rd", "4th"), 
          class = c("ordered", "factor")), 
          AVG = c(360,594, 868, 1534, 349, 592, 861)), 
          .Names = c("NEWSEC", "tiles", "AVG"), 
          row.names = c(NA, 7L), class = "data.frame") 

ui <- fluidPage(
    fluidRow(
    column(4, 
      selectInput("colvar", "Variable:", 
         choices = c("NEWSEC", "Market", "Category")) 
    ), 
    column(8, 
      h4("old"), tableOutput("old") 
    ), 
    h4("new"), tableOutput("new")) 
) 


server <- function(input, output) { 

    # dynamic data frame 
    data <- reactive({ 
    new <- df 
    new$Market <- gl(n = 2, k = 4, length = 7, labels = c("Market1", "Market2")) 
    new$Category <- gl(n = 2, k = 4, length = 7, labels = c("Cat1", "Cat2")) 
    new 
    }) 

    output$old <- renderTable({ 
    # access dynamic data frame "data" via "data()" 
    data() 
    }) 

    output$new <- renderTable({ 
    # access dynamic data frame "data" via "data()" 
    sec <- input$colvar 
    reshape(data(), idvar = sec, timevar = "tiles", direction = "wide") 
    }) 
} 

shinyApp(ui = ui, server = server) 
+0

许多感谢详细的解答。我认为它是有效的。由于我的应用程序有太多的输入和过滤器以及支持文件,我错过了它。再次感谢。 – Apricot