如何通过多个变量过滤数据帧的行,在变换的数据框上运行模型,并渲染结果?
我已经想出如何通过多个变量简单地过滤数据框并显示为表格:
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)
答案 0 :(得分:0)
你可以像这样组合它们:
我更喜欢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)
})