I have a problem with object 'input' not found in formula. When I try to use input of selectInput, it throws me this error. T tried everything, but nothing helps. Can someone help me with this issue please ?
SERVER
idk = reactive({
id = sample(2, nrow(df1()), replace = TRUE, prob = c(input$slider, 1-input$slider))
id
})
output$dev <- renderText({
paste0("Training part is: ",input$slider)
})
output$dev2 <- renderText({
paste0("Testing part is: ",1-input$slider)
})
train_datas = reactive({
train_data = df1()[idk() == 1, ]
train_data
})
test_datas = reactive({
test_data = df1()[idk() == 2, ]
test_data
})
outVar = reactive({
mydata = test_datas()
names(mydata)
})
observe({
updateSelectInput(session, "columns1",
choices = outVar()
)})
observe({
updateSelectInput(session, "columns2",
choices = outVar()
)})
observe({
updateSelectInput(session, "columns3",
choices = outVar()
)})
observe({
updateSelectInput(session, "columns4",
choices = outVar()
)})
tree = reactive({
mydata = train_datas()
tree2 = ctree(formula = input$columns1 ~ input$columns2 input$columns3 input$columns4, data = mydata)
tree2
})
output$try <- renderPrint({
return(tree())
})
UI
tabPanel("Data prepare",
sidebarLayout(
sidebarPanel(
numericInput("slider", "Train data (0-1)", 1,
0.1, 1, 0.05),
textOutput("dev"),
textOutput("dev2"),
tags$hr(),
textOutput("dev3"),
textOutput("dev4"),
tags$hr(),
selectInput('columns1', 'Columns1', ""),
selectInput('columns2', 'Columns2', ""),
selectInput('columns3', 'Columns3', ""),
selectInput('columns4', 'Columns4', "")
),
mainPanel(verbatimTextOutput("try"))
))
I add whole code whichh is connect to my question. What I need is get input value of last four select inputs - column1, column2, column3, column4.
CodePudding user response:
This is how to update choices of selectInput
according to the current value of the available variables of the input data frame (outVar
in your case).
Then, the selected choices will be used as response and predictor variables in a reactive model:
library(shiny)
ui <- fluidPage(
selectInput("response", "response", ""),
selectInput("predictor", "predictor", ""),
verbatimTextOutput("model")
)
server <- function(session, input, output) {
# Example data
data <- reactive(iris)
variables <- reactive(colnames(data()))
formula <- reactive({
paste0(input$response, "~", input$predictor) %>% as.formula()
})
# dummy model using reactive formula
model <- reactive({
lm(formula = formula(), data = data())
})
# Display sth about the reactive model
output$model <- renderText({
model() %>%
coefficients() %>%
as.character()
})
observeEvent(
eventExpr = variables,
handlerExpr = {
updateSelectInput(session, "response", choices = variables(), selected = variables()[[1]])
updateSelectInput(session, "predictor", choices = variables(), selected = variables()[[2]])
}
)
}
shinyApp(ui, server)