【问题标题】:Shiny R: Filter data frame by two variables and run modelShiny R:通过两个变量过滤数据框并运行模型
【发布时间】:2018-05-07 14:21:10
【问题描述】:

如何通过多个变量过滤数据框的行,在转换后的数据框上运行模型并渲染结果?

我已经想出了如何通过多个变量简单地过滤数据框并显示为表格:

server <- function(input, output) {
  output$summary <- renderTable({  
  df <- Heating

  if (input$regiontype != "All") {
    df <- df[df$region == input$regiontype,]
  }
  if (input$roomsize != "All") {
    df <- df[df$rooms == input$roomsize,]
  }

  df
  })
}

我还想出了如何通过一个变量过滤数据框,运行模型并打印结果:

#### PART 3 - Define server logic
server <- function(input, output) {

  output$summary <- renderPrint({

    df <- Heating

    ### Subset data
    df.subset <- reactive({ a <- subset(df, region == input$regiontype)
    return(a)})

    ### Model 
    estimates <- mlogit(depvar ~ ic + oc | 0, data= df.subset(), shape = "wide", choice = "depvar", varying = c(3:12))
    summary(estimates)

  })      
}

但是如何将这些结合起来,在已被一个或多个变量过滤的数据框上运行模型?

我将在下面提供两个版本的脚本: 1。通过多个变量过滤数据框

### PART 1 - Load Libraries and Data
library(shiny)           # For running the app
library(mlogit)

#### data
data("Heating", package = "mlogit")

#### PART 2 - Define User Interface for application
ui <- fluidPage(

  ## Application title
  titlePanel("Housing Preference"),

  ## Sidebar with user input elements
  sidebarLayout(
    sidebarPanel(
      p("Select the inputs"), # Header
      #Reg
      selectInput('regiontype', 'Region', choices = c("All",
                                                      "Northern coastal region"= "ncostl", 
                                                      "Southern coastal region" = "scostl", 
                                                      "Mountain region"  = "mountn",
                                                      "Central valley region"= "valley") #, 
                  #multiple=TRUE, 
                  #selectize=TRUE
      ),

      #Room Size
      selectInput('roomsize', 'Room Size', choices = c("All",
                                                       "2"= 2, 
                                                       "3" = 3, 
                                                       "4"  = 4,
                                                       "5"= 5 ,
                                                       "6"=6,
                                                       "7"=7)
                  #multiple=TRUE, 
                  #selectize=TRUE
      )



    ),

    ## Show a plot
    mainPanel(
      tableOutput("summary")
    )
  )
)

#### PART 3 - Define server logic
  server <- function(input, output) {
    output$summary <- renderTable({  
      df <- Heating

      if (input$regiontype != "All") {
        df <- df[df$region == input$regiontype,]
      }
      if (input$roomsize != "All") {
        df <- df[df$rooms == input$roomsize,]
      }

      df
    })
  }

### PART 4 - Run the application 
shinyApp(ui = ui, server = server)

2。按一个变量过滤,运行模型,打印结果:

### PART 1 - Load Libraries and Data
library(shiny)           # For running the app
library(mlogit)

#### data
data("Heating", package = "mlogit")

#### PART 2 - Define User Interface for application
ui <- fluidPage(

  ## Application title
  titlePanel("Housing Preference"),

  ## Sidebar with user input elements
  sidebarLayout(
    sidebarPanel(
      p("Select the inputs"), # Header
      #Reg
      selectInput('regiontype', 'Region', choices = c("All",
                                                      "Northern coastal region"= "ncostl", 
                                                      "Southern coastal region" = "scostl", 
                                                      "Mountain region"  = "mountn",
                                                      "Central valley region"= "valley") #, 
                  #multiple=TRUE, 
                  #selectize=TRUE
      ),

      #Room Size
      selectInput('roomsize', 'Room Size', choices = c("All",
                                                       "2"= 2, 
                                                       "3" = 3, 
                                                       "4"  = 4,
                                                       "5"= 5 ,
                                                       "6"=6,
                                                       "7"=7)
                  #multiple=TRUE, 
                  #selectize=TRUE
      )



    ),

    ## Show a plot
    mainPanel(
      verbatimTextOutput("summary")
    )
  )
)

#### PART 3 - Define server logic
server <- function(input, output) {

  output$summary <- renderPrint({

    df <- Heating

    ### Subset data
    df.subset <- reactive({ a <- subset(df, region == input$regiontype)
    return(a)})

    ### Model 
    estimates <- mlogit(depvar ~ ic + oc | 0, data= df.subset(), shape = "wide", choice = "depvar", varying = c(3:12))
    summary(estimates)

  })      
}

### PART 4 - Run the application 
shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: r dataframe filter shiny


    【解决方案1】:

    您可以像这样组合它们:
    我更喜欢dplyr 的过滤方式,只是因为我感觉更舒服。

    output$summary <- renderPrint({
                                region_type_selected <- input$regiontype
                                room_size_selected <- input$roomsize
    
                                ### Subset data
                                library(dplyr)
                                if(region_type_selected != "All"){
                                                df <- Heating %>% filter(region == region_type_selected)               
                                }
    
                                if(room_size_selected != "All"){
                                                df <- Heating %>% filter(rooms == room_size_selected)               
                                }
    
                                ### Model 
                                estimates <- mlogit(depvar ~ ic + oc | 0, data= df, shape = "wide", choice = "depvar", varying = c(3:12))
                                summary(estimates)
    
                })
    

    【讨论】:

      猜你喜欢
      • 2017-11-17
      • 2021-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-25
      • 1970-01-01
      • 1970-01-01
      • 2019-10-11
      相关资源
      最近更新 更多