闪亮:仅在文件上传后显示按钮
Shiny: Show buttons only after file has been uploaded
我正在试用 Shiny,我喜欢它。我构建了一个小应用程序,学生可以在其中上传一个 csv 文件,然后选择因变量和自变量,然后 R 计算线性回归。它工作正常。我把它上传到:
http://carlosq.shinyapps.io/Regresion
[你可以用this file测试一下。 "beer" 是因变量,除 "id" 之外的其余变量是自变量]
这里是server.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')
)
)
))
我的问题是:我想让按钮的外观 "Press After Reading File and selecting variables " 以上传成功为条件。
我已尝试使用此处包含的建议:
Make conditionalPanel depend on files uploaded with fileInput
但我就是做不到。
我很感激任何帮助。
这段代码对我有用
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(),
uiOutput('ui.action') # instead of conditionalPanel
),
mainPanel(
verbatimTextOutput('contents')
)
)
))
server.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))
})
})
output$ui.action <- renderUI({
if (is.null(input$file1)) return()
actionButton("action", "Press after reading file and selecting variables")
})
})
根据 Marat 提供的所有建议,working ShinyApp 和 ui.R 和 server.R 的最终版本。
首先是ui.R
# ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Multiple Linear Regression with R/Shiny"),
sidebarLayout(
sidebarPanel(
p("Please upload a CSV formatted file with your data."),
fileInput('file1', label='Click button below to select the file in your computer.',
accept=c('text/csv',
'text/comma-separated-values,text/plain',
'.csv')),
tags$hr(),
uiOutput("dependent"),
uiOutput("independents"),
tags$hr(),
uiOutput('ui.action') # instead of conditionalPanel
),
mainPanel(
p("Here's the output from your regression:"),
verbatimTextOutput('contents')
)
)
))
和server.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$ui.action <- renderUI({
if (is.null(filedata())) return()
actionButton("action", "Run regression")
})
output$dependent <- renderUI({
df <- filedata()
if (is.null(df)) return(NULL)
items=names(df)
names(items)=items
selectInput("dependent","Now 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","Also select ONE or MANY independent variables in the box below. You can change your selection several times:",items,multiple=TRUE)
})
output$contents <- renderPrint({
if (is.null(input$action)) return()
if (input$action==0) return()
isolate({
df <- filedata()
if (is.null(df)) return(NULL)
fmla <- as.formula(paste(input$dependent," ~ ",paste(input$independents,collapse="+")))
summary(lm(fmla,data=df))
})
})
})
再次感谢马拉特的帮助。
我正在试用 Shiny,我喜欢它。我构建了一个小应用程序,学生可以在其中上传一个 csv 文件,然后选择因变量和自变量,然后 R 计算线性回归。它工作正常。我把它上传到:
http://carlosq.shinyapps.io/Regresion
[你可以用this file测试一下。 "beer" 是因变量,除 "id" 之外的其余变量是自变量]
这里是server.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')
)
)
))
我的问题是:我想让按钮的外观 "Press After Reading File and selecting variables " 以上传成功为条件。
我已尝试使用此处包含的建议:
Make conditionalPanel depend on files uploaded with fileInput
但我就是做不到。
我很感激任何帮助。
这段代码对我有用
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(),
uiOutput('ui.action') # instead of conditionalPanel
),
mainPanel(
verbatimTextOutput('contents')
)
)
))
server.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))
})
})
output$ui.action <- renderUI({
if (is.null(input$file1)) return()
actionButton("action", "Press after reading file and selecting variables")
})
})
根据 Marat 提供的所有建议,working ShinyApp 和 ui.R 和 server.R 的最终版本。
首先是ui.R
# ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Multiple Linear Regression with R/Shiny"),
sidebarLayout(
sidebarPanel(
p("Please upload a CSV formatted file with your data."),
fileInput('file1', label='Click button below to select the file in your computer.',
accept=c('text/csv',
'text/comma-separated-values,text/plain',
'.csv')),
tags$hr(),
uiOutput("dependent"),
uiOutput("independents"),
tags$hr(),
uiOutput('ui.action') # instead of conditionalPanel
),
mainPanel(
p("Here's the output from your regression:"),
verbatimTextOutput('contents')
)
)
))
和server.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$ui.action <- renderUI({
if (is.null(filedata())) return()
actionButton("action", "Run regression")
})
output$dependent <- renderUI({
df <- filedata()
if (is.null(df)) return(NULL)
items=names(df)
names(items)=items
selectInput("dependent","Now 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","Also select ONE or MANY independent variables in the box below. You can change your selection several times:",items,multiple=TRUE)
})
output$contents <- renderPrint({
if (is.null(input$action)) return()
if (input$action==0) return()
isolate({
df <- filedata()
if (is.null(df)) return(NULL)
fmla <- as.formula(paste(input$dependent," ~ ",paste(input$independents,collapse="+")))
summary(lm(fmla,data=df))
})
})
})
再次感谢马拉特的帮助。