Reputation: 483
I'm trying to build a shiny app using multiple linear regression that allows selecting more than one independent variables but currently stuck on a server-side (I guess). Data from here (table1_1).
Data:
# A tibble: 1,289 x 6
wage female nonwhite union education exper
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 11.55 1 0 0 12 20
2 5.00 0 0 0 9 9
3 12.00 0 0 0 16 15
4 7.00 0 1 1 14 38
5 21.15 1 1 0 16 19
6 6.92 1 0 0 12 4
7 10.00 1 0 0 12 14
8 8.00 1 1 0 12 32
9 15.63 0 0 0 18 7
10 18.22 1 0 0 18 5
# ... with 1,279 more rows
Codes:
library(shiny)
library(shinydashboard)
ui <- shinyUI(
dashboardPage(
dashboardHeader(),
dashboardSidebar(
selectInput(inputId = "y", label = "Dependent Variable:", choices = names(table1_1[,c(2:7)])),
selectInput(inputId = "x", label = "Independent Variable(s):",
choices = names(table1_1[,c(2:7)]), multiple = TRUE),
sidebarMenu(
menuItem(text = "Summary", tabName = "summary_lm")
)
),
dashboardBody(
tabItems(
tabItem(verbatimTextOutput("summary"), tabName = "summary_lm")
)
)
)
)
server <- shinyServer(function(input,output){
output$summary <- renderPrint({
model <- reactive({
lm(table1_1[,names(table1_1) %in% input$y] ~ table1_1[,names(table1_1) %in% input$x])
})
summary(model())
})
})
shinyApp(ui,server)
Error: invalid type (list) for variable 'table1_1[, names(table1_1) %in% input$y]'
Anyone help me in where my code is going wrong?
Upvotes: 1
Views: 360
Reputation: 263301
There was a clue to what part of the code was at fault and you were correct that it was in the server-side section. The page that gets displayed (once you fixed the error in the code that references a non-existent 7th name) and after also fixed the erroneous construction of a formula for the call to lm then displayed without error:
Call:
lm(formula = form, data = table1_1)
Residuals:
1
1
No Coefficients
Residual standard error: 1 on 1 degrees of freedom
Because there was nothing in the client side code to require a selection of variable on the x-side. So here's how to fix the first substantial error regarding formula construction:
server <- shinyServer(function(input,output){
output$summary <- renderPrint({
model <- reactive({ form <- as.formula( paste( names(table1_1)[names(table1_1) %in% input$y], "~", paste(names(table1_1)[names(table1_1) %in% input$x], collapse="+")))
print(form)
lm(form, data=table1_1)
})
summary(model())
})
})
shinyApp(ui,server)
The formula objects need to be built either syntactically correctly and they often need to be R sanitized by passing through as.formula
if the arguments need to be calculated. Formula objects are not really supposed to look up values from the calling environment.
(And after pasting together that expression for the formula construction I thought it would be a good idea to define a variable that holds the names of the tibble
object.)
It now works although the first time the page that comes up, it is not informative about that fact. If you click a y-variable the reactive loop then succeeds in adding it to the formula and calculating a result.
Upvotes: 1