【发布时间】:2015-03-02 09:07:36
【问题描述】:
我正在尝试使用 Shiny,我喜欢它。我构建了一个小应用程序,学生上传一个 csv 文件,然后选择一个因变量和自变量,然后 R 计算线性回归。它工作正常。我把它上传到了:
http://carlosq.shinyapps.io/Regresion
[如果需要,可以使用this file 进行测试。 “beer”是因变量,除“id”以外的其他变量是自变量]
这里是服务器。R:
# server.R
library(shiny)
shinyServer(function(input, output) {
filedata <- reactive({
infile <- input$file1
if (is.null(infile)){
return(NULL)
}
read.csv(infile$datapath)
})
output$dependent <- renderUI({
df <- filedata()
if (is.null(df)) return(NULL)
items=names(df)
names(items)=items
selectInput("dependent","Select ONE variable as dependent variable from:",items)
})
output$independents <- renderUI({
df <- filedata()
if (is.null(df)) return(NULL)
items=names(df)
names(items)=items
selectInput("independents","Select ONE or MANY independent variables from:",items,multiple=TRUE)
})
output$contents <- renderPrint({
input$action
isolate({
df <- filedata()
if (is.null(df)) return(NULL)
fmla <- as.formula(paste(input$dependent," ~ ",paste(input$independents,collapse="+")))
summary(lm(fmla,data=df))
})
})
})
这里是 ui.R:
# ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Multiple Linear Regression"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose CSV File',
accept=c('text/csv',
'text/comma-separated-values,text/plain',
'.csv')),
tags$hr(),
uiOutput("dependent"),
uiOutput("independents"),
tags$hr(),
actionButton("action", "Press after reading file and selecting variables")
),
mainPanel(
verbatimTextOutput('contents')
)
)
))
我的问题是:我想让按钮“在读取文件并选择变量后按下”以成功上传为条件。
我已尝试使用此处包含的建议:
Make conditionalPanel depend on files uploaded with fileInput
但我就是做不到。
感谢任何帮助。
【问题讨论】: