r - 在 Shiny 中使用多个条目对数据集进行子集化会产生不正确的输出

标签 r ggplot2 shiny data.table

我正在创建一个 Shiny 应用程序,在 selectizeInput 框中选择多个输入并尝试使用这些选择对我的数据进行子集化时遇到了问题。

这是预期的输出

testDT <- data.table(
    L = (1:32), 
    M = rep(letters[23:26], each = 64), 
    N = rep(LETTERS[1:2], times = 2, each = 512),
    O = rnorm(2048, 1))

testDT$L <- factor(testDT$L, levels = seq(from = 1, to = 32, by = 1))

ggplot(testDT, aes(L,O)) +
    geom_boxplot(aes(fill = N)) +  
    theme_bw() +
    theme(legend.position = "top", legend.title=element_blank()) + 
    facet_grid(M ~ ., scales = "free") +
    labs(x = "L", y = "O")

但是,当我创建应用程序时,图形输出并不符合预期。我选择的选项越多,例如在 N 中,数据似乎开始交替。

这是 ui.R 文件:

# ui.R

shinyUI(fluidPage(

            titlePanel("Test Application"),

            sidebarLayout(
                    sidebarPanel(

                            selectizeInput("N",
                                    label = ("N"),
                                    multiple = TRUE,
                                    choices = NULL,
                                    options = list(
                                            placeholder = 'Select All Desired, Type to Search',
                                            onInitialize = I('function() { this.setValue(""); }')
                                    )),

                            selectizeInput("M", 
                                    label = "M",
                                    multiple = TRUE,
                                    choices = NULL,
                                    options = list(
                                            placeholder = 'Select All Desired, Type to Search',
                                            onInitialize = I('function() { this.setValue(""); }')
                                    ))
                    ),

                    mainPanel(
                            tabsetPanel(
                                    tabPanel("Test Plot 1",
                                            plotOutput("testPlot1")),
                                    tabPanel("Test Plot 2",
                                            plotOutput("testPlot2"))


                    )
            ))))

这是server.R 文件:

# server.R

library(data.table)
library(ggplot2)

testDT <- data.table(
    L = (1:32), 
    M = rep(letters[23:26], each = 64), 
    N = rep(LETTERS[1:2], times = 2, each = 512),
    O = rnorm(2048, 1))

testDT$L <- factor(testDT$L, levels = seq(from = 1, to = 32, by = 1))

shinyServer(function(input, output, session) {

        updateSelectizeInput(session, "N", 
                server = TRUE, 
                choices = sort(unique(testDT$N)),
        )

        updateSelectizeInput(session, "M", 
                server = TRUE, 
                choices = unique(testDT$M),
        )       

        testDT1 <- reactive({
                    subset(testDT, N == input$N)
                })

        testDT2 <- reactive({
                    subset(testDT, N == input$N & M == input$M)
                })

        output$testTable <- renderDataTable(testDT1())

        output$testPlot1 <- renderPlot({

                    p <- ggplot(testDT1(), aes(L,O)) +
                            geom_boxplot(aes(fill = N)) +  
                            theme_bw() +
                            theme(legend.position = "top", legend.title=element_blank()) + 
                            facet_grid(M ~ ., scales = "free") +
                            labs(x = "L", y = "O")
                    print(p)
                })

        output$testPlot2 <- renderPlot({

                    p <- ggplot(testDT2(), aes(L,O)) +
                            geom_boxplot(aes(fill = N)) +  
                            theme_bw() +
                            theme(legend.position = "top", legend.title=element_blank()) + 
                            facet_grid(M ~ ., scales = "free") +
                            labs(x = "L", y = "O")
                    print(p)
                })
    })

我强烈怀疑我错误地对数据进行了子集化,但由于我是 Shiny 环境的新手,因此我不完全理解使用 input$ 进行子集化时的行为_____ 调用,如下所示。

testDT1 <- reactive({
      subset(testDT, N == input$N)
      })

最佳答案

我建议使用 [ 运算符的子集化,而不是 subset 函数。
阅读SO问题In R, why is [ better than subset?了解更多详细信息。
在您的示例中:

testDT1 <- reactive({
      testDT[eval(call("==", as.name("N"), input$N))]
})

将多值子集的 == 更改为 %in%

还要注意,使用 data.table 索引可能是值得的,因为它可以显着加快过滤速度,从而为您的 Shiny 应用程序提供实时过滤。有关更多详细信息,请参阅我的博文 Scaling data.table using index .
事实上索引应该在第一次过滤期间自动创建,您可以在使用 set2keyv 函数加载数据集后准备它。

关于r - 在 Shiny 中使用多个条目对数据集进行子集化会产生不正确的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34123988/

相关文章:

r - 如何将tapply()的输出放入data.frame中

r - 当选择不同的 tabItem 时, Shiny 的仪表板侧边栏会发生变化

r - ggplot2 添加缩写指南

r - ggforce facet_zoom 如何仅注释缩放的图形

css - 在 R Shiny Heading 中编辑句子中单个单词的字体

r - 将不规则的 x,y 数据点插入到规则网格中以进行轮廓映射

r - 从R中的文件读取数据帧时如何跳过无效行?

R:ggplot在x轴上显示所有日期

r - Shiny - 移动设备上没有并排布局

r - slider 输入最大/最小文本标签