【问题标题】:How to define formula dynamically from select input ( multiple = TRUE) in R shiny如何从R闪亮中的选择输入(multiple = TRUE)动态定义公式
【发布时间】:2018-02-09 15:03:42
【问题描述】:

我正在尝试为多项逻辑回归定义一个公式,它应该从下拉列表中获取最多 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())})

任何指导将不胜感激...谢谢

【问题讨论】:

  • 试试as.formula(paste(input$outcome,'~', paste(input$predictor, collapse = "+")))
  • 非常感谢您的快速解决方案...工作正常

标签: r shiny linear-regression


【解决方案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)
}

-数据

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

-运行闪亮

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

-输出单个自变量

-输出多个自变量

【讨论】:

  • 我们可以绘制所选变量的效果图吗?我将以下代码用于一个预测器 output$plot
  • @nab 能否请您作为一个新问题发布,因为我不清楚
  • 确定我要发布一个新问题
  • @nab 谢谢,我没有完全按照你想要的那些 cmets 的原因
  • 得到了解决方案。我指的是系数的效果图。我在下面的代码中使用了相同的输出$plot
猜你喜欢
  • 2020-02-05
  • 2016-11-21
  • 2019-06-14
  • 2014-12-15
  • 1970-01-01
  • 2021-07-16
  • 1970-01-01
  • 2016-12-25
相关资源
最近更新 更多