Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用selectbox从用户输入数据集创建回归公式_R_Shiny - Fatal编程技术网

R 使用selectbox从用户输入数据集创建回归公式

R 使用selectbox从用户输入数据集创建回归公式,r,shiny,R,Shiny,我正试图在R Shinny中定制一个用户界面回归工具以供实践(即我自己的spss版本,用于我的一般用例)。我在从用户上传的数据集生成回归公式这一关键步骤上遇到了问题。我希望用户能够从下拉菜单中选择因变量(并最终将生成的变量转换为我的服务器代码中的公式) 我尝试在selectInput()函数的choices参数中使用textOutput(names(userdata()),以便用户在上传数据集后可以选择哪些变量应为因变量。但是,这将生成数据集的属性列表,而不是列本身的名称 我已经研究了其他人已经

我正试图在R Shinny中定制一个用户界面回归工具以供实践(即我自己的spss版本,用于我的一般用例)。我在从用户上传的数据集生成回归公式这一关键步骤上遇到了问题。我希望用户能够从下拉菜单中选择因变量(并最终将生成的变量转换为我的服务器代码中的公式)

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

我已经研究了其他人已经做过的反应式数据集的其他用途,但似乎没有人做了我正试图做的事情,或者我正在糟糕地搜索它们。(这似乎是Shiny最常见的可能使用案例,因此我无法想象还没有人发现这一点,但我找不到任何东西)

库(闪亮)
图书馆(有线)

ui
textOutput
用于将文本输出到闪亮的ui。这包括生成适当的HTML。由于
wired\u select(…,choices=???)
需要的是一个R对象而不是HTML代码,因此这不太可能起作用

一种可能有效的方法是使用
updateSelectInput
。我不知道它是否具有与wired library相同的功能,但在base Shining中,我会:

  • 初始化
    选择输入
    ,无需选择

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

请尝试以下操作:

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
库,因此我将建议另一种可能的方法。(我无法在我的环境中安装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
库(闪亮)
图书馆(有线)

不幸的是,这个ui可以工作,但不能与
wired
库一起工作。谢谢
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