Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Shinny R:按两个变量筛选数据帧并运行模型_R_Dataframe_Filter_Shiny - Fatal编程技术网

Shinny R:按两个变量筛选数据帧并运行模型

Shinny R:按两个变量筛选数据帧并运行模型,r,dataframe,filter,shiny,R,Dataframe,Filter,Shiny,如何通过多个变量过滤数据帧的行,在转换的数据帧上运行模型,并呈现结果 我已经了解了如何通过多个变量简单地过滤数据帧并显示为表: server <- function(input, output) { output$summary <- renderTable({ df <- Heating if (input$regiontype != "All") { df <- df[df$region == input$regiontype,] }

如何通过多个变量过滤数据帧的行,在转换的数据帧上运行模型,并呈现结果

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

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
  })
}

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)

            })
输出$summary
### 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)
### 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)
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)

            })