【问题标题】:Shiny: Show buttons only after file has been uploaded闪亮:仅在文件上传后显示按钮
【发布时间】: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

但我就是做不到。

感谢任何帮助。

【问题讨论】:

    标签: r shiny uploading


    【解决方案1】:

    这里是 working ShinyApp 以及基于 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))
        })
      })
    
    
    }) 
    

    再次感谢马拉特的帮助。

    【讨论】:

      【解决方案2】:

      这段代码对我有用

      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')
          )
        )
      ))
      

      服务器.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 文件包含检查文件是否上传成功的行。
      • @user23438,我无法使用conditionPanel 获得解决方案,因为我不知道如何正确设置condition。我编辑了答案,现在基于 uiOutput。
      • 再次感谢 Marat 抽出宝贵时间。您的新解决方案让我更接近解决方案。现在按钮出现在正确的时刻,但它给我的 output$contents 带来了问题。在我将本节中的代码包装在由 input$action 激活的“isolate({})”中之前。现在 input$action 消失了,因为按钮消失了。我可以摆脱隔离,但它会打印垃圾,直到选择正确的变量。
      • 如果您想防止在 output$contents 中过早输出,您可以在此处插入几个检查:将 input$action 替换为 if (is.null(input$action)) return()if (input$action==0) return()
      猜你喜欢
      • 2017-08-06
      • 2018-10-09
      • 1970-01-01
      • 1970-01-01
      • 2015-05-16
      • 1970-01-01
      • 2013-12-13
      • 2015-10-14
      • 1970-01-01
      相关资源
      最近更新 更多