我正在创建一个闪亮的应用程序,允许您为了运行线性回归而上传带有一些数据(相关和独立变量)的Excel文件。在Shiny中定义变量(动态)
我正在使用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和预解码都在服务器上定义并因此产生一个错误:找不到对象'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])
})
心不是这个的这个副本:https://stackoverflow.com/questions/44540683/tabs-with-different-侧边栏/ 44541573?noredirect = 1#comment76097350_44541573 – Phi
@Phi,虽然它的一个不同的问题(问题) – John