如何从R shiny中的select输入(multiple = TRUE)动态定义公式

时间:2017-08-31 15:27:54

标签: r shiny linear-regression

我正在尝试定义多项Logistic回归的公式,它应该从下拉列表中获取最多6个独立变量的输入。 R Shiny中的(SelectInput,Multiple = TRUE)。无法弄清楚如何解决这个问题。

以下是示例代码...... 式

Multiformula <- reactive ({ as.formula(paste(input$outcome,'~'input$predictor) })

模型

MultiModel <- reactive({
    multinom(Multiformula(), data = filtered())
  })

以上代码适用于单个变量,但对于多个独立变量,方法可能不同。我试过以下但没有运气

indvar6 <- reactive({
  filter(forest_data_model[,input$predictor])
  })

重新定义公式......但它没有工作

Multiformula <- reactive ({as.formula(paste(input$outcome,'~'indvar6())})

任何指导都将受到高度赞赏......谢谢

1 个答案:

答案 0 :(得分:1)

我们可以试试

library(shiny)
library(nnet)
library(foreign)
fmnom <- function(data  = NULL, depVar, indepVar) {

  ui <- fluidPage(
    headerPanel("Multinomial analysis"), 
    sidebarPanel(
      p("Select inputs for the Dependent Variable"),
      selectInput(inputId = "dep", label = "Dependent Variables", multiple = FALSE, 
                       choices = as.list(depVar)),
      p("Select input for the Independent Variable"),
      selectInput(inputId = "indep", label = "Independent Variables", 
                  multiple = TRUE, choices = as.list(indepVar), selected = indepVar[1])
    ),
    mainPanel(
      verbatimTextOutput(outputId = "RegOut"),
      verbatimTextOutput(outputId = "IndPrint"),
      verbatimTextOutput(outputId = "DepPrint")

    )
  )

  server <- function(input, output) {

    mlt <- reactive(
                     {multinom(reformulate(input$indep, input$dep), data = data)})

    output$DepPrint <- renderPrint({input$dep})
    output$IndPrint <- renderPrint({input$indep})
    output$RegOut <- renderPrint({summary(mlt())})

  }



  shinyApp(ui= ui, server = server)
}

-data

ml <- read.dta("https://stats.idre.ucla.edu/stat/data/hsbdemo.dta")

-run shiny

fmnom(ml, depVar = c("prog", "schtyp"), indepVar = c("ses", "read", "write") )

- 输出单个自变量

enter image description here

- 输出多个独立变量

enter image description here