r - 如何从R Shiny 的选择输入(multiple = TRUE)动态定义公式

标签 r shiny linear-regression

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

这里是示例代码... 公式

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

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

最佳答案

我们可以尝试

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

-数据

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

-运行 Shiny

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

-输出单个自变量

enter image description here

-输出多个自变量

enter image description here

关于r - 如何从R Shiny 的选择输入(multiple = TRUE)动态定义公式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45985140/

相关文章:

python-3.x - 置信度分数太低

python - 梯度下降实现python——轮廓线

r - 堆积条形图上的单个误差条 ggplot

mysql - 使用数据库结果绘制 R 图形

python - 跨时间暗淡的每个网格单元的线性回归

reshape Shiny 的 table

r - ggplot2 图形质量在shinyapps.io 上 Shiny

r - 在R中将文件夹从一个目录复制到另一个目录

r - OSX 安装 Rsymphony - 链接 header 和库

r - 在 R Shiny 应用程序中自动删除临时文件 - 文件 : cannot open the connection 中的错误