Based on the provided R code and the structure of the data.table object, I’m assuming you want to perform a linear regression using the lm() function from the base R package.
The issue is that the lm() function expects a formula object as its first argument. However, in your code, you are passing a character vector of variable names directly to the lm() function.
To fix this, you need to create a formula object by using the ~ symbol and the variable names as arguments. Here’s an updated version of your code:
library(data.table)
library(shiny)
# assume 'mtcars' is your data table
ui <- fluidPage(
selectInput(inputId = "var.x1",
label = strong("X-Variable1"),
choices = colnames(mtcars),
selected = "cyl"),
selectInput(inputId = "var.x2",
label = strong("X-Variable2"),
choices = colnames(mtcars),
selected = "hp"),
selectInput(inputId = "var.y",
label = strong("y-Variable"),
choices = colnames(mtcars),
selected = "mpg"),
verbatimTextOutput('lm')
)
server <- function(input, output, session) {
output$lm <- renderPrint(expr = {
# extract the variable names from input
x1_name <- deparse(substitute(input$var.x1))
x2_name <- deparse(substitute(input$var.x2))
y_name <- deparse(substitute(input$var.y))
# create a formula object
form <- as.formula(paste0(y_name, "~", x1_name, "+", x2_name))
# fit the linear regression model
model <- lm(form, data = mtcars)
return(summary(model))
})
}
# run the shiny app
shinyApp(ui, server)
In this updated code, we use the deparse(substitute()) function to extract the variable names from the input variables. We then create a formula object using these variable names and the paste0() function. Finally, we fit the linear regression model using the lm() function and return the summary of the model.
Note that this code assumes that the variables in your data table have the same names as the input variables. If they do not, you may need to adjust the code accordingly.
Last modified on 2024-12-13