【问题标题】:Defining variable (dynamic) in Shiny在 Shiny 中定义变量(动态)
【发布时间】:2017-06-16 11:18:45
【问题描述】:

我正在创建一个闪亮的应用程序,它允许您上传包含一些数据(因变量和自变量)的 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 是在 server 下定义的,因此会产生错误:object 'var' not found.

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

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])
  })

【问题讨论】:

标签: r rstudio shiny


【解决方案1】:

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

在服务器中:

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

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

}) 

在用户界面中:

uiOutput('selectize1')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-01
    • 2013-08-05
    • 2017-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多