使用选定的输入在 shiny 中构建具有反应式公式的模型

Building a model with a reactive formula in shiny using selected inputs

我有一个问题,在 公式 中找不到对象 'input'。当我尝试使用 selectInput 的输入时,它会抛出此错误。 T 尝试了一切,但没有任何帮助。有人可以帮我解决这个问题吗?

服务器

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"))
                    ))

我添加了与我的问题相关的完整代码。我需要的是获取最后四个 select 输入的输入值 - column1、column2、column3、column4。

这是如何根据输入数据框可用变量的当前值更新 selectInput 的选择(在您的情况下为 outVar)。 然后,所选选项将用作反应模型中的响应变量和预测变量:

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)