2016-10-04 86 views
0

首先,我是R新手,所以我想這可能是一個簡單的方法。我在這裏搜索了Stack Overflow,找不到類似的問題,所以我很抱歉如果我錯過了。如何從文本輸出值創建數據框?

我的程序包含4個選項卡:「selection」,「Base Individus」,「Base Logements」和「Tableau」。 在第一個選項卡中,我選擇了一個或多個城鎮,感謝chekboxGroupInput。 第二個&第三個選項卡顯示從我在第一個選項卡中檢查過的框中計算出的不同文本輸出。 由於我將要顯示大量文本輸出,因此我想使用文本輸出的值並將其顯示在「Tableau」選項卡的數據框中。 但是,我不知道如何取回文本輸出值(如輸出$ textHab,輸出$ textH和所有其他文件以在數據框中顯示它們,包括在tabPanel「Base Individus」中指示的行名稱) 。與 「基地Logements」

非常感謝您的任何幫助

這是Ui.R代碼:

library(shiny) 

shinyUI(navbarPage("RP 2014", 
    tabPanel("Sélection", 
    checkboxGroupInput("dynamic", label = "Communes", 
     choices = list("01. Bélep" =  "01", "02. Boulouparis" = "02", 
        "03. Bourail" = "03", "04. Canala" = "04", 
        "05. Dumbéa" = "05", "06. Farino" = "06", 
        "07. Hienghène" = "07", "08. Houaïlou" = "08", 
        "09. Ile des Pins" = "09", "10. Kaala Gomen" = "10", 
        "11. Koné" = "11", "12. Koumac" = "12", 
        "13. La Foa" = "13", "14. Lifou" = "14", 
        "15. Maré" = "15", "16. Moindou" = "16", 
        "17. Mont Dore" = "17", "18. Nouméa" = "18", 
        "19. Ouégoa" = "19", "20. Ouvéa" = "20", 
        "21. Païta" = "21", "22. Poindimié" = "22", 
        "23. Ponérihouen" = "23", "24. Pouébo" = "24", 
        "25. Pouembout" = "25", "26. Poum" = "26", 
        "27. Poya" = "27", "28. Sarraméa" = "28", 
        "29. Thio" = "29", "30. Touho" = "30", 
        "31. Voh" = "31", "32. Yaté" = "32")), 
        "33. Kouaoua" = "33", width = 3), 


     tabPanel("Base Individus", 
       "Nombre d'habitants : ", textOutput("textHab"), 
       "Hommes : ", textOutput("textH"), 
       "Femmes : ", textOutput("textF"), 
       "Mineurs : ", textOutput("textMineurs"), 
       "Mineurs Hommes : ", textOutput("textMineursH"), 
       "Mineurs Femmes : ", textOutput("textMineursF"), 
       "Majeurs : ", textOutput("textMajeurs"), 
       "Majeurs Hommes : ", textOutput("textMajeursH"), 
       "Majeurs Femmes : ", textOutput("textMajeursF")), 
     tabPanel("Base Logements", 
       "Nombre de logements : ", textOutput("textCL"), 
       "Résidences Principales : ", textOutput("textCL1"), 
       "Logements occasionnels : ", textOutput("textCL2"), 
       "Résidences Secondaires : ", textOutput("textCL3"), 
       "Logements vacants : ", textOutput("textCL4"), 
       "Maisons : ", textOutput("textTC1"), 
       "Appartements : ", textOutput("textTC2"), 
       "Cases : ", textOutput("textTC3"), 
       "Const. prov./cabanes : ", textOutput("textTC4"), 
       "Bateaux : ", textOutput("textTC5"), 
       "Autres types de logements : ", textOutput("textTC6"), 
       "Propriétaires : ", textOutput("textSO1"), 
       "Logés gratuitement : ", textOutput("textSO2"), 
       "Locataires : ", textOutput("textSO3"), 
       "Logements sociaux : ", textOutput("textSIC1")), 
     tabPanel("Tableau")) 
    ) 

而且Sever.R

library(shiny) 
library(dplyr) 

shinyServer(function(input, output) { 
selectionBI <- reactive({ 
filter(BI14, PC %in% input$dynamic) 
}) 
selectionBI2 <- reactive({ 
filter(BI14_Mineurs, PC %in% input$dynamic) 
}) 
selectionBI3 <- reactive({ 
filter(BI14_Majeurs, PC %in% input$dynamic) 
}) 
output$textHab <- renderText({ 
NROW(selectionBI()) 
}) 
output$textH <- renderText({ 
tmp <- selectionBI() 
NROW(filter(tmp, S == "1")) 
}) 
output$textF <- renderText({ 
tmp <- selectionBI() 
NROW(filter(tmp, S == "2")) 
}) 
output$textMineurs <- renderText({ 
NROW(selectionBI2()) 
}) 
output$textMineursH <- renderText({ 
tmp <- selectionBI2() 
NROW(filter(tmp, S == "1")) 
}) 
output$textMineursF <- renderText({ 
tmp <- selectionBI2() 
NROW(filter(tmp, S == "2")) 
}) 
output$textMajeurs <- renderText({ 
NROW(selectionBI3()) 
}) 
output$textMajeursH <- renderText({ 
tmp <- selectionBI3() 
NROW(filter(tmp, S == "1")) 
}) 
output$textMajeursF <- renderText({ 
tmp <- selectionBI3() 
NROW(filter(tmp, S == "2")) 
}) 
selectionLOG <- reactive({ 
filter(LOG14, PC %in% input$dynamic) 
}) 
output$textCL <- renderText({ 
NROW(selectionLOG()) 
}) 
output$textCL1 <- renderText({ 
tmp <- selectionLOG() 
NROW(filter(tmp, CL == "1")) 
}) 
output$textCL2 <- renderText({ 
tmp <- selectionLOG() 
NROW(filter(tmp, CL == "2")) 
}) 
output$textCL3 <- renderText({ 
tmp <- selectionLOG() 
NROW(filter(tmp, CL == "3")) 
}) 
output$textCL4 <- renderText({ 
tmp <- selectionLOG() 
NROW(filter(tmp, CL == "4")) 
}) 
output$textTC1 <- renderText({ 
tmp <- selectionLOG() 
NROW(filter(tmp, TC == "1")) 
}) 
output$textTC2 <- renderText({ 
tmp <- selectionLOG() 
NROW(filter(tmp, TC == "2")) 
}) 
output$textTC3 <- renderText({ 
tmp <- selectionLOG() 
NROW(filter(tmp, TC == "3")) 
}) 
output$textTC4 <- renderText({ 
tmp <- selectionLOG() 
NROW(filter(tmp, TC == "4")) 
}) 
output$textTC5 <- renderText({ 
tmp <- selectionLOG() 
NROW(filter(tmp, TC == "5")) 
}) 
output$textTC6 <- renderText({ 
tmp <- selectionLOG() 
NROW(filter(tmp, TC == "6")) 
}) 
output$textSO1 <- renderText({ 
tmp <- selectionLOG() 
NROW(filter(tmp, SO == "1")) 
}) 
output$textSO2 <- renderText({ 
tmp <- selectionLOG() 
NROW(filter(tmp, SO == "2")) 
}) 
output$textSO3 <- renderText({ 
tmp <- selectionLOG() 
NROW(filter(tmp, SO == "3")) 
}) 
output$textSIC1 <- renderText({ 
tmp <- selectionLOG() 
NROW(filter(tmp, SIC == "1")) 
    }) 
}) 

非常感謝您的幫助。

這是LOG14可變的樣本數據中,所有的數據都是字符格式:

PC;CL;TC;SO;SIC 
01;1;1;1; 
01;1;1;1; 
01;1;1;1; 
01;1;1;1; 
01;1;1;1; 
01;1;1;1; 
01;1;1;1; 
01;3;1;9;0 
01;3;1;9;0 
01;1;1;1; 
01;1;1;1; 
01;1;1;1; 
01;1;1;1; 
01;1;3;2; 
01;1;1;1; 
01;1;1;1; 
01;1;1;1; 
01;1;1;1; 
01;1;1;1; 
01;1;1;1; 
01;1;1;1; 
01;1;1;1; 
01;1;1;1; 
01;1;1;1; 
02;1;1;1; 
02;1;1;1; 
02;1;1;1; 
02;1;1;1; 
02;1;1;1; 
02;3;1;;0 
02;1;1;1; 
02;1;1;1; 
02;1;3;1; 
02;3;1;;0 
02;1;1;1; 
02;1;1;1; 
02;1;1;1; 
02;1;1;1; 
02;1;1;1; 
02;1;1;1; 
02;1;1;1; 
02;3;1;;0 
02;1;3;1; 
02;1;1;1; 
02;1;1;1; 
02;3;1;;0 
02;1;1;1; 
02;4;1;;0 
03;1;1;1; 
03;1;1;1; 
03;1;1;2; 
03;1;1;1; 
03;1;1;1; 
03;1;1;1; 
03;1;1;1; 
03;3;1;1;0 
03;1;1;1; 
03;1;1;1; 
03;1;1;1; 
03;1;4;1; 
03;1;1;1; 
03;1;1;1; 
03;3;1;1;0 
03;1;4;1; 
03;1;1;1; 
03;1;1;1; 
03;1;1;1; 
03;1;1;1; 
03;1;1;1; 
03;1;1;1; 
+0

你可以提供一些樣本數據來創建BI14變量? –

+0

您好,Valter,我提供了LOG14變量而不是BI14。非常感謝您的幫助。 –

回答

0

看看該溶液中。對於Tableau選項卡,我使用了DataTables。

ui.R

library(shiny) 
    library(DT) 

    shinyUI(navbarPage("RP 2014", 
         tabPanel("Sélection", 
           checkboxGroupInput("dynamic", label = "Communes", 
                choices = list("01. Bélep" =  "01", "02. Boulouparis" = "02", 
                    "03. Bourail" = "03", "04. Canala" = "04", 
                    "05. Dumbéa" = "05", "06. Farino" = "06", 
                    "07. Hienghène" = "07", "08. Houaïlou" = "08", 
                    "09. Ile des Pins" = "09", "10. Kaala Gomen" = "10", 
                    "11. Koné" = "11", "12. Koumac" = "12", 
                    "13. La Foa" = "13", "14. Lifou" = "14", 
                    "15. Maré" = "15", "16. Moindou" = "16", 
                    "17. Mont Dore" = "17", "18. Nouméa" = "18", 
                    "19. Ouégoa" = "19", "20. Ouvéa" = "20", 
                    "21. Païta" = "21", "22. Poindimié" = "22", 
                    "23. Ponérihouen" = "23", "24. Pouébo" = "24", 
                    "25. Pouembout" = "25", "26. Poum" = "26", 
                    "27. Poya" = "27", "28. Sarraméa" = "28", 
                    "29. Thio" = "29", "30. Touho" = "30", 
                    "31. Voh" = "31", "32. Yaté" = "32")), 
           "33. Kouaoua" = "33", width = 3), 


         tabPanel("Base Individus", 
           "Nombre d'habitants : ", textOutput("textHab"), 
           "Hommes : ", textOutput("textH"), 
           "Femmes : ", textOutput("textF"), 
           "Mineurs : ", textOutput("textMineurs"), 
           "Mineurs Hommes : ", textOutput("textMineursH"), 
           "Mineurs Femmes : ", textOutput("textMineursF"), 
           "Majeurs : ", textOutput("textMajeurs"), 
           "Majeurs Hommes : ", textOutput("textMajeursH"), 
           "Majeurs Femmes : ", textOutput("textMajeursF")), 
         tabPanel("Base Logements", 
           "Nombre de logements : ", textOutput("textCL"), 
           textOutput("textCL1"), 
           textOutput("textCL2"), 
           textOutput("textCL3"), 
           textOutput("textCL4"), 
           textOutput("textTC1"), 
           textOutput("textTC2"), 
           textOutput("textTC3"), 
           textOutput("textTC4"), 
           textOutput("textTC5"), 
           textOutput("textTC6"), 
           textOutput("textSO1"), 
           textOutput("textSO2"), 
           textOutput("textSO3"), 
           textOutput("textSIC1")), 
         tabPanel("Tableau", 
           fluidRow(
             column(width = 6, 
           dataTableOutput("df")), 
           column(width = 6, 
             dataTableOutput("df2")) 
           ) 
            ) 

         ) 
    ) 

server.R

library(shiny) 
    library(dplyr) 
    library(DT) 

    shinyServer(function(input, output) { 
      selectionBI <- reactive({ 
        filter(BI14, PC %in% input$dynamic) 
      }) 
      selectionBI2 <- reactive({ 
        filter(BI14_Mineurs, PC %in% input$dynamic) 
      }) 
      selectionBI3 <- reactive({ 
        filter(BI14_Majeurs, PC %in% input$dynamic) 
      }) 
      output$textHab <- renderText({ 
        NROW(selectionBI()) 
      }) 
      output$textH <- renderText({ 
        tmp <- selectionBI() 
        NROW(filter(tmp, S == "1")) 
      }) 
      output$textF <- renderText({ 
        tmp <- selectionBI() 
        NROW(filter(tmp, S == "2")) 
      }) 
      output$textMineurs <- renderText({ 
        NROW(selectionBI2()) 
      }) 
      output$textMineursH <- renderText({ 
        tmp <- selectionBI2() 
        NROW(filter(tmp, S == "1")) 
      }) 
      output$textMineursF <- renderText({ 
        tmp <- selectionBI2() 
        NROW(filter(tmp, S == "2")) 
      }) 
      output$textMajeurs <- renderText({ 
        NROW(selectionBI3()) 
      }) 
      output$textMajeursH <- renderText({ 
        tmp <- selectionBI3() 
        NROW(filter(tmp, S == "1")) 
      }) 
      output$textMajeursF <- renderText({ 
        tmp <- selectionBI3() 
        NROW(filter(tmp, S == "2")) 
      }) 
      selectionLOG <- reactive({ 
        filter(LOG14, PC %in% input$dynamic) 
      }) 
      selectionLOGTable <- reactive({ 
        tmp <- selectionLOG() 
        df <- data.frame(
          names = c("Résidences Principales : ", 
          "Logements occasionnels : ", 
          "Résidences Secondaires : ", 
          "Logements vacants : ", 
          "Maisons : ", 
          "Appartements : ", 
          "Cases : ", 
          "Const. prov./cabanes : ", 
          "Bateaux : ", 
          "Autres types de logements : ", 
          "Propriétaires : ", 
          "Logés gratuitement : ", 
          "Locataires : ", 
          "Logements sociaux : "), 
          values = c(
            NROW(filter(tmp, CL == "1")), 
            NROW(filter(tmp, CL == "2")), 
            NROW(filter(tmp, CL == "3")), 
            NROW(filter(tmp, CL == "4")), 
            NROW(filter(tmp, TC == "1")), 
            NROW(filter(tmp, TC == "2")), 
            NROW(filter(tmp, TC == "3")), 
            NROW(filter(tmp, TC == "4")), 
            NROW(filter(tmp, TC == "5")), 
            NROW(filter(tmp, TC == "6")), 
            NROW(filter(tmp, SO == "1")), 
            NROW(filter(tmp, SO == "2")), 
            NROW(filter(tmp, SO == "3")), 
            NROW(filter(tmp, SIC == "1")) 


          ), 
          refCode = c("CL1", "CL2", "CL3", "CL4", "TC1", "TC2", "TC3", 
             "TC4", "TC5", "TC6", "SO1", "SO2", "SO3", "SIC") 
        ) 
        df 
      }) 
      output$textCL <- renderText({ 
        NROW(selectionLOG()) 
      }) 
      output$textCL1 <- renderText({ 
        tmp <- selectionLOGTable()[1,1:2] 
        paste(tmp[,1], tmp[,2], sep = "\n") 

      }) 
      output$textCL2 <- renderText({ 
        tmp <- selectionLOGTable()[2,1:2] 
        paste(tmp[,1], tmp[,2], sep = "\n") 
      }) 
      output$textCL3 <- renderText({ 
        tmp <- selectionLOGTable()[3,1:2] 
        paste(tmp[,1], tmp[,2], sep = "\n") 
      }) 
      output$textCL4 <- renderText({ 
        tmp <- selectionLOGTable()[4,1:2] 
        paste(tmp[,1], tmp[,2], sep = "\n") 
      }) 
      output$textTC1 <- renderText({ 
        tmp <- selectionLOGTable()[5,1:2] 
        paste(tmp[,1], tmp[,2], sep = "\n") 
      }) 
      output$textTC2 <- renderText({ 
        tmp <- selectionLOGTable()[6,1:2] 
        paste(tmp[,1], tmp[,2], sep = "\n") 
      }) 
      output$textTC3 <- renderText({ 
        tmp <- selectionLOGTable()[7,1:2] 
        paste(tmp[,1], tmp[,2], sep = "\n") 
      }) 
      output$textTC4 <- renderText({ 
        tmp <- selectionLOGTable()[8,1:2] 
        paste(tmp[,1], tmp[,2], sep = "\n") 
      }) 
      output$textTC5 <- renderText({ 
        tmp <- selectionLOGTable()[9,1:2] 
        paste(tmp[,1], tmp[,2], sep = "\n") 
      }) 
      output$textTC6 <- renderText({ 
        tmp <- selectionLOGTable()[10,1:2] 
        paste(tmp[,1], tmp[,2], sep = "\n") 
      }) 
      output$textSO1 <- renderText({ 
        tmp <- selectionLOGTable()[11,1:2] 
        paste(tmp[,1], tmp[,2], sep = "\n") 
      }) 
      output$textSO2 <- renderText({ 
        tmp <- selectionLOGTable()[12,1:2] 
        paste(tmp[,1], tmp[,2], sep = "\n") 
      }) 
      output$textSO3 <- renderText({ 
        tmp <- selectionLOGTable()[13,1:2] 
        paste(tmp[,1], tmp[,2], sep = "\n") 
      }) 
      output$textSIC1 <- renderText({ 
        tmp <- selectionLOGTable()[14,1:2] 
        paste(tmp[,1], tmp[,2], sep = "\n") 
      }) 
      output$df <- renderDataTable(
        selectionLOGTable()[,1:2], 
        colnames = c("Description", "Count"), 
        rownames = FALSE, 
        options = list(
        paging = FALSE, 
        ordering = FALSE, 
        searching = FALSE, 
        info = FALSE 
        ) 
      ) 
      output$df2 <- renderDataTable(
        selectionLOGTable()[,1:2], 
        colnames = c("Description", "Count"), 
        rownames = FALSE, 
        options = list(
          paging = FALSE, 
          ordering = FALSE, 
          searching = FALSE, 
          info = FALSE 
        ) 
      ) 
    }) 
+0

如此偉大的Valter!這是你第二次救我,非常感謝你。我將搜索有關此功能的更多信息以深入使用它。還有幾個問題:是否有可能不顯示左側或refCode的行數?是否有可能將我的tabPanel分爲兩個區域(例如「列」指令),並在每個區域使用數據框,因爲我只有兩列:要顯示的名稱和值?如果這是不可能的,不要介意。但是非常感謝Valter幫助新手成爲我,你是一位國王。 –

+0

使用兩個表格檢查修改的代碼,並在服務器端爲數據表格選擇一些有用的選項。 –

+0

是的,你說得對。我設法將2個數據幀放在同一頁面上。我仍在搜索如何不顯示不必要的變量。再次感謝你Valter,祝你有美好的一天。 –