簡體   English   中英

R Shiny 中使用多變量的子集數據

[英]Subset data in R Shiny using Multiple Variables

我是R Shiny的新手。 我正在嘗試創建一個app ,允許用戶根據多個變量對data.frame進行subset化,然后查看結果數據。

這是一個小的示例數據集:

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

我希望用戶能夠指定iterwave的值並查看結果數據。

這是我對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)

output

在此處輸入圖像描述

問題是兩個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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM