如何按多个变量过滤数据框的行,在转换后的数据框上运行模型并呈现结果?
我已经想出了如何简单地按多个变量过滤数据框并显示为表格:
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)
你可以像这样组合它们:
我更喜欢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)
})