【问题标题】:Use selectbox to create regression formula from user input dataset使用选择框从用户输入数据集创建回归公式
【发布时间】:2019-05-09 23:30:35
【问题描述】:

我正在尝试在 R Shiny 中自定义构建用户界面回归工具以进行练习(即我自己的 spss 版本用于我的一般用例)。我在从用户上传的数据集生成回归公式的关键步骤时遇到了麻烦。我希望用户能够从下拉菜单中选择一个因变量(并最终将这些生成的变量转换为我的服务器代码中的公式)。

我尝试在 selectInput() 函数的选择参数中使用 textOutput(names(userdata())),以便用户在上传数据集后可以选择哪些变量应该是因变量。但是,这会生成数据集的 属性 列表,而不是列本身的名称。

我已经研究过其他人已经做过的反应式数据集的其他用途,但似乎没有人完全按照我正在尝试做的事情去做,或者我正在搜索它们很糟糕。 (这似乎是 Shiny 最常见的可能用例,所以我无法想象如何还没有人发现这一点,但我找不到任何东西)

library(shiny)
library(wired)

ui <- fluidPage(
  sidebarLayout(sidebarPanel(
    fileInput("FileInput", "Input Your Data Set (Must be .csv)"),
    wired_select(inputId = "responsevar",
                 label = "Dependent Varibale:", 
                 choices = textOutput(outputId = "variable_names")
    )

  ), #sidebar panel
  mainPanel(
    tabsetPanel(
      tabPanel("Table",
               DT::dataTableOutput("table")
      )
    ) #tabset Panel
  ) #main panel
  ) #sidebarlayout
) #fluidpage


server <- function(input, output, session) {
  datasetInput <- reactive({
    infile <- input$FileInput
    if (is.null(infile))
      return(NULL)
    read.csv(infile$datapath, header = TRUE)
  })

  output$table = DT::renderDataTable(datasetInput())

  output$variable_names <- reactive({
    if (is.null(datasetInput()))
      return(NULL)
    names(datasetInput()) 
  })
} #server


shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    textOutput 用于将文本输出到 Shiny UI。这包括生成适当的 HTML。由于wired_select(..., choices = ???) 期望的是 R 对象而不是 HTML 代码,因此这不太可能起作用。

    一种可能有效的方法是使用updateSelectInput。我不知道这是否具有与有线库等效的功能,但在基本闪亮中我会:

    • 在没有选择的情况下初始化selectInput

    • 选择数据后更新下拉列表中的选项

    尝试以下方法:

    library(shiny)
    
    ui <- fluidPage(
      sidebarLayout(sidebarPanel(
        fileInput("FileInput", "Input Your Data Set (Must be .csv)"),
        selectInput(inputId = "responsevar",
                     label = "Dependent Varibale:", 
                     choices = NULL)
      ), #sidebar panel
      mainPanel(
        tabsetPanel(
          tabPanel("Table",
                   DT::dataTableOutput("table")
          )
        ) #tabset Panel
      ) #main panel
      ) #sidebarlayout
    ) #fluidpage
    
    server <- function(input, output, session) {
      datasetInput <- reactive({
        infile <- input$FileInput
        if (is.null(infile))
          return(NULL)
        read.csv(infile$datapath, header = TRUE)
      })
    
      output$table = DT::renderDataTable(datasetInput())
    
      observeEvent(datasetInput(),{
        updateSelectInput(session, "responsevar", choices = names(datasetInput()))
      })
    
    } #server
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

    • 这有效,但不适用于wired 库,不幸的是。谢谢!
    【解决方案2】:

    嗯...由于上述内容不适用于wired 库,我将建议另一种可能的方法。 (我无法在我的环境中安装有线,如果没有更好的地方,请道歉)。

    这里的想法是让选择器成为动态 R 对象(UI 对象)的一部分。然后,如果一个文件被加载,依赖于该文件的 UI 对象也会更新。

    library(shiny)
    library(wired)
    
    ui <- fluidPage(
      sidebarLayout(sidebarPanel(
        fileInput("FileInput", "Input Your Data Set (Must be .csv)"),
        uiOutput("selector")
      ), #sidebar panel
      mainPanel(
        tabsetPanel(
          tabPanel("Table",
                   DT::dataTableOutput("table")
          )
        ) #tabset Panel
      ) #main panel
      ) #sidebarlayout
    ) #fluidpage
    
    server <- function(input, output, session) {
      datasetInput <- reactive({
        infile <- input$FileInput
        if (is.null(infile))
          return(NULL)
        read.csv(infile$datapath, header = TRUE)
      })
    
      output$table = DT::renderDataTable(datasetInput())
    
      output$selector <- renderUI({
        choices <- NULL
        if(!is.null(datasetInput()))
            choices <- names(datasetInput())
        wired_select(inputId = "responsevar",
                     label = "Dependent Varibale:", 
                     choices = choices)
     })
    
    } #server
    

    与我的原始答案的主要区别是 uiOutput 替换 selectInputrenderUI 输出组件而不是观察者。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-29
      • 2019-04-17
      • 1970-01-01
      • 1970-01-01
      • 2018-07-26
      相关资源
      最近更新 更多