2016-08-21 105 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