你的例子不够完整,我无法理解你遇到了什么错误,但我认为这应该可以解决问题:
lm(as.formula(paste(input$dependent," ~ ",paste(input$independent,collapse="+"))),data=dat)
一个垃圾测试表明这能够创建动态模型(尽管需要在其他领域进行一些调整):
用户界面
library(shiny)
shinyUI(pageWithSidebar(
headerPanel("Test Shiny App"),
sidebarPanel(
selectInput("dependent", "Dependent Variable:", c("x","y","z")),
uiOutput("independent")
),
mainPanel(tableOutput("regTab"))
))
服务器.R
library(shiny)
dat <- data.frame(x=rnorm(100),y=rnorm(100),z=rnorm(100))
shinyServer(function(input, output, session) {
output$independent <- renderUI({
checkboxGroupInput("independent", "Independent Variables:",names(dat)[!names(dat) %in% input$dependent],names(dat)[!names(dat) %in% input$dependent])
})
runRegression <- reactive({
lm(as.formula(paste(input$dependent," ~ ",paste(input$independent,collapse="+"))),data=dat)
})
output$regTab <- renderTable({
if(!is.null(input$independent)){
summary(runRegression())$coefficients
} else {
print(data.frame(Warning="Please select Model Parameters."))
}
})
})