Hello !
Here is my code. I'm not satisfy :
*ols: whatever the nb of explanatory var i choose, only the first one is selected + less importantly i would like the choice to determine which is Y which is X here and for prediction
*predict: predict returns more than 1 value for dep variable while newdata() returns correctly only one + the prediction is not refreshed when i modify ind variables + less importantly: i want to plug a figure not to select
Many thanks for any help !
HERE BELOW IS MY CODE:
set.seed(123)
var1 <- sample(1:15, 50, replace = TRUE)
var2 <- sample(16:30, 50, replace = TRUE)
var3 <- sample(31:45, 50, replace = TRUE)
df<- data.frame( var1 ,var2 ,var3)
ui= fluidPage(
titlePanel(title = h4("SUPERMARCO NEEDS HELP")),
sidebarLayout(
sidebarPanel(
selectInput('varY', 'Select an Dependent Variable', names(df)),
selectInput('varX', 'Select an Explanatory Variable', names(df), multiple=TRUE),
actionButton(inputId = "go1",label="Plot"),
actionButton(inputId = "go2",label="Results"),
selectizeInput('VAR2', 'Select X2 TO FORECAST Y',selected = "20", choices = (df$var2), multiple=FALSE),
selectizeInput('VAR3', 'Insert a value for X3 TO FORECAST Y', selected = "40", choices = (df$var3), multiple=FALSE),
#numericInput("VAR3", "Select X3 TO FORECAST Y", 40),
actionButton("updateF", "FORECAST")
),
mainPanel("main panel", plotOutput("rplot"), verbatimTextOutput("summary"), verbatimTextOutput("x_updated"))
))
server = function(input, output,session) {
lm_fit <- reactive({ lm(as.formula(paste0(input$varY ,"~", input$varX)), data=df) })
summary_stats <- eventReactive(input$go2,{ summary(lm_fit()) })
rplot<- eventReactive(input$go1, { plot(lm(as.formula(paste0(input$varY ,"~", input$varX)), data=df))
})
output$rplot <- renderPlot({ rplot() })
output$summary <- renderPrint({ summary_stats() })
result <- lm(var1 ~ var2 + var3 , data = df)
newdata <- eventReactive( input$updateF, {data.frame(input$VAR2 ,input$VAR3)})
output$x_updated <- renderPrint({ predict(result, newdata()) })
}
shinyApp(ui,server)