在Shiny

时间:2017-06-16 11:18:46

标签: r rstudio shiny

我正在创建一个闪亮的应用程序,允许您上传带有一些数据(相关和独立变量)的excel文件,以便运行线性回归。

我正在使用read.xlsx加载excel文件,该文件将在第一个选项卡中输出一个表。在第二个选项卡中,我想要一个小部件,它允许您选择因变量和另一个输入小部件,要求您选择自变量。这些变量将是加载的数据列标题的函数。因此,假设加载的数据如下所示:

A       B       C       D       E       F   G
22000   0.605   0.352   1.125   103.5   162 7107.263017
22000   0.495   0.352   1.375   126.5   162 4569.734496
22000   0.495   0.352   1.375   103.5   198 4649.524562
18000   0.495   0.352   1.125   126.5   198 4495.776272

其中G是自变量。我希望列标题可以用作第二个选项卡上变量选择的基础。 我面临的问题是将加载的excel文件头(列名)链接为我的变量选择器,因为我在我的UI中将它们定义为我选择的变量:

selectizeInput('inv',"In", choices = var, multiple = TRUE),
selectizeInput('out',"Out", choices = pred, multiple = FALSE)

因此var和pred在服务器下定义,因此会产生错误:object' var'没找到。

我该如何解决这个问题。这是到目前为止的代码:

ui <- fluidPage(

  titlePanel("Hi"),
  sidebarLayout(position = "left",
                sidebarPanel(
                  conditionalPanel(condition = "input.tabs1==1",
                                   tags$style(type='text/css', ".well { max-width: 20em; }"),
                                   # Tags:
                                   tags$head(
                                     tags$style(type="text/css", "select[multiple] { width: 100%; height:10em}"),
                                     tags$style(type="text/css", "select { width: 100%}"),
                                     tags$style(type="text/css", "input { width: 19em; max-width:100%}")
                                   ),

                                   # Select filetype:
                                   selectInput("readFunction", "Function to read data:", c(
                                     # Base R:
                                     "read.table",
                                     "read.csv",
                                     "read.csv2",
                                     "read.delim",
                                     "read.delim2",
                                     "readWorksheet",
                                     "read_excel",
                                     "read.xlsx"

                                   )),

                                   # Argument selecter:
                                   htmlOutput("ArgSelect"),

                                   # Argument field:
                                   htmlOutput("ArgText"),

                                   # Upload data:
                                   fileInput("file", "Upload data-file:"),

                                   # Variable selection:
                                   htmlOutput("varselect"),

                                   br(),

                                   textInput("name","Dataset name:","Data")),
                  conditionalPanel(condition = "input.tabs1==2",
                                   #fileInput('file', 'Choose file to upload.'),
                                   selectizeInput('invar',"Select Regression Input Variables", choices = varnames, multiple = TRUE),
                                   selectizeInput('outvar',"Select Regression Output Variable", choices = predictors, multiple = FALSE)

                ),
                mainPanel(
                  tabsetPanel(id="tabs1",
                              tabPanel("Data File",value = 1,tableOutput("table")),
                              tabPanel("LM Plot", value=2, plotOutput("PlotLM")))

                  )
                )

  ))




  server<-function(input, output) {
  options(shiny.maxRequestSize=30*1024^2)

  ### Argument names:
  ArgNames <- reactive({
    Names <- names(formals(input$readFunction)[-1])
    Names <- Names[Names!="..."]
    return(Names)
  })

  # Argument selector:
  output$ArgSelect <- renderUI({
    if (length(ArgNames())==0) return(NULL)

    selectInput("arg","Argument:",ArgNames())
  })

  ## Arg text field:
  output$ArgText <- renderUI({
    fun__arg <- paste0(input$readFunction,"__",input$arg)

    if (is.null(input$arg)) return(NULL)

    Defaults <- formals(input$readFunction)

    if (is.null(input[[fun__arg]]))
    {
      textInput(fun__arg, label = "Enter value:", value = deparse(Defaults[[input$arg]])) 
    } else {
      textInput(fun__arg, label = "Enter value:", value = input[[fun__arg]]) 
    }
  })


  ### Data import:
  Dataset <- reactive({
    if (is.null(input$file)) {
      # User has not uploaded a file yet
      return(data.frame())
    }

    args <- grep(paste0("^",input$readFunction,"__"), names(input), value = TRUE)

    argList <- list()
    for (i in seq_along(args))
    {
      argList[[i]] <- eval(parse(text=input[[args[i]]]))
    }
    names(argList) <- gsub(paste0("^",input$readFunction,"__"),"",args)

    argList <- argList[names(argList) %in% ArgNames()]

    Dataset <- as.data.frame(do.call(input$readFunction,c(list(input$file$datapath),argList)))
    return(Dataset)
  })

  # Select variables:
  output$varselect <- renderUI({

    if (identical(Dataset(), '') || identical(Dataset(),data.frame())) return(NULL)

    # Variable selection:    
    selectInput("vars", "Variables to use:",
                names(Dataset()), names(Dataset()), multiple =TRUE)            
  })

  # Show table:
  output$table <- renderTable({

    if (is.null(input$vars) || length(input$vars)==0) return(NULL)

    return(Dataset()[,input$vars,drop=FALSE])
  })

1 个答案:

答案 0 :(得分:1)

您可以尝试使用uiOutputrenderUI来执行此操作。

在服务器中:

varnames <- reactive({
    colnames(Dataset())
})

output$selectize1 <- renderUI({
    selectizeInput('invar',"Select Regression Input Variables", choices = varnames(), multiple = TRUE)

}) 
中的

uiOutput('selectize1')