R 闪亮:仅在上传文件后显示按钮
我正在尝试闪亮,我喜欢它。我构建了一个小应用程序,学生上传一个csv文件,然后选择一个因变量和自变量,然后R计算一个线性回归。它很好用。我已上载至: [如果需要,可以使用来测试它。“beer”是因变量,除“id”之外的其他变量是独立变量] 这是服务器。R:R 闪亮:仅在上传文件后显示按钮,r,shiny,uploading,R,Shiny,Uploading,我正在尝试闪亮,我喜欢它。我构建了一个小应用程序,学生上传一个csv文件,然后选择一个因变量和自变量,然后R计算一个线性回归。它很好用。我已上载至: [如果需要,可以使用来测试它。“beer”是因变量,除“id”之外的其他变量是独立变量] 这是服务器。R: # server.R library(shiny) shinyServer(function(input, output) { filedata <- reactive({ infile <- input$fil
# 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))
})
})
})
我的问题是:我想让“读取文件并选择变量后按”按钮的外观以成功上传为条件
我尝试使用此处包含的建议:
但我就是做不到
我感谢您的帮助。此代码对我有效 用户界面 服务器.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")
})
})
#server.R
图书馆(闪亮)
shinyServer(功能(输入、输出){
filedata以下是基于Marat提供的所有建议的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')
)
)
))
和服务器.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))
})
})
})
#server.R
图书馆(闪亮)
shinyServer(功能(输入、输出){
filedata谢谢你,Marat。我尝试了你的解决方案。它使按钮看起来不一样…这很好。但上传文件后它不会出现。我估计你的服务器。R文件中有一行检查文件上传是否成功。@user23438,我无法使用条件面板
获得解决方案,因为我不知道如何正确设置条件
。我编辑了答案,该答案现在基于uiOutput。再次感谢Marat为您提供的时间。您的新解决方案使我更接近解决方案。现在按钮出现在正确的时刻,但它会对我的输出$contents产生问题。在此之前,我将本节中的代码包装为“隔离({})”这是由input$action激活的。现在input$action不见了,因为按钮不见了。我可以去掉isolate,但在选择正确的变量之前它会打印垃圾。如果要防止在output$contents中过早输出,可以在那里插入两个检查:替换input$action
为If(is.null(输入$action))return()
和if(输入$action==0)return()
# 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
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))
})
})
})