gpt4 book ai didi

r - Shiny :仅在文件上传后显示按钮

转载 作者:行者123 更新时间:2023-12-04 02:58:40 25 4
gpt4 key购买 nike

我正在尝试Shiny,我喜欢它。我构建了一个小应用程序,学生可以在其中上传一个csv文件,然后选择一个因变量和自变量,然后R计算线性回归。它工作正常。我将其上传到:

http://carlosq.shinyapps.io/Regresion

[如果需要,可以使用this file对其进行测试。 “啤酒”是因变量,除“ 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')
)
)
))


我的问题是:我要使按钮“在读取文件并选择变量后按”的外观以成功上传为条件。

我尝试使用此处包含的建议:

Make conditionalPanel depend on files uploaded with fileInput

但是我只是无法使其工作。

我感谢您的帮助。

最佳答案

这段代码对我有用

用户界面

 # 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
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")
})

})

关于r - Shiny :仅在文件上传后显示按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27769186/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com