使用多个变量在R Shiny中子集数据

马克·米勒

我是新来的R Shiny我试图创建一个app允许用户在subset一个data.frame基于多个变量,然后看看结果数据。

这是一个小的示例数据集:

iter,wave,apples
1,1,600
1,1,500
1,1,400
1,2,300
1,2,200
1,2,100
2,1,1000
2,1,1100
2,1,1200
2,2,1300
2,2,1400
2,2,1500
3,1,1100
3,1,2200
3,1,3300
3,2,4400
3,2,5500
3,2,6600

我希望用户能够指定iterand的值wave并查看结果数据。

这是我尝试的Shiny代码。我意识到我一定犯了一些愚蠢的错误。

编辑

这是我的修改代码。现在的最终结果非常接近我想要的结果。sidebar仍然没有被完全显示。

library(shiny)

setwd('C:/Users/mark_/Documents/simple_RShiny_files/explore')

apple.data <- read.csv('subset_data_based_on_multiple_variables.csv', 
                        header = TRUE, stringsAsFactors = FALSE)

ui <- fluidPage(

     titlePanel("Subsetting Apple Dataset"),

     sidebarLayout(
          sidebarPanel(
               uiOutput("codePanel")
          ),

          mainPanel(
               tableOutput("view")
          )
     ),

     selectInput("codeInput", inputId ="data1", label = "Choose Iter", choices = unique(apple.data$iter)),
     selectInput("codeInput", inputId ="data2", label = "Choose Wave", choices = unique(apple.data$wave))

)

server <- function(input, output) {

     output$codePanel <- renderUI({

     })

     dataset <- reactive({

          subset(apple.data, (iter == input$data1 & wave == input$data2))

     })

     output$view <- renderTable(dataset())

}

shinyApp(ui = ui, server = server)

输出

在此处输入图片说明

多米尼克·迈耶(Dominik S. Meier)

问题是两者selectInputs都相同inputId这有效:

library(shiny)

apple.data <- data.frame(
        iter = c(1L,1L,1L,1L,1L,1L,2L,2L,2L,2L,2L,
                 2L,3L,3L,3L,3L,3L,3L),
        wave = c(1L,1L,1L,2L,2L,2L,1L,1L,1L,2L,2L,
                 2L,1L,1L,1L,2L,2L,2L),
      apples = c(600L,500L,400L,300L,200L,100L,1000L,
                 1100L,1200L,1300L,1400L,1500L,1100L,2200L,3300L,4400L,
                 5500L,6600L)
)

ui <- fluidPage(
    titlePanel("Subsetting Apple Dataset"),
    sidebarLayout(
        sidebarPanel(
            selectInput("codeInput1", label = "Choose Iter", choices = unique(apple.data$iter)),
            selectInput("codeInput2", label = "Choose Wave", choices = unique(apple.data$wave))
        ),
        mainPanel(
            tableOutput("view")
        )
    )
)

server <- function(input, output) {


    dataset <- reactive({
        return(subset(apple.data, (iter == input$codeInput1 & wave == input$codeInput2)))
    })

    output$view <- renderTable(dataset())
}

shinyApp(ui = ui, server = server)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章