如何从文本输出值创建数据框?

时间:2016-10-04 22:58:27

标签: r dataframe shiny

首先,我是R的新手,所以我认为可能有一种简单的方法可以做到这一点。我在这里搜索Stack Overflow并找不到类似的问题,所以如果我错过了,我会道歉。

我的程序包含4个标签:" selection"," Base Individus"," Base Logements"和" Tableau"。 在第一个选项卡中,由于chekboxGroupInput,我选择了一个或多个城镇。 第二个&第三个选项卡显示不同的文本输出,这些输出是根据我在第一个选项卡中检查的方框计算的。 因为我要显示很多文本输出,所以我想使用文本输出的值并将它们显示在数据框中,在" Tableau"标签。 但是,我不知道如何收回文本输出值(例如输出$ textHab,输出$ textH以及所有其他值以在数据框中显示它们,包括tabPanel和#34中指示的行名称; Base Individus"和" Base 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;

1 个答案:

答案 0 :(得分: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
                    )
            )
    })