从上传的数据框

时间:2016-08-09 14:00:31

标签: file shiny

我已经合并了不同的代码源,以制作一个允许用户上传文件(数据框)的应用。

然而,除此之外,我还希望能够从数据框中选择特定列并对其进行分析。然而,这很困难,因为必须预定义给定的数据框,以便能够在ui.R脚本中引用它.... 因此,当先前未定义的数据帧上传到站点时,人们无法在ui.R中尊重它,因为它在服务器中定义....

预定义变量

vchoices <- 1:ncol(mtcars)
names(vchoices) <- names(mtcars)

ui.R

    runApp(
      ui = basicPage(
        h2('The uploaded file data'),
        dataTableOutput('mytable'),
        fileInput('file', 'Choose info-file to upload',
                  accept = c(
                    'text/csv',
                    'text/comma-separated-values',
                    'text/tab-separated-values',
                    'text/plain',
                    '.csv',
                    '.tsv'
                  )
        ),
        actionButton("choice", "incorporate external information"),

        selectInput("columns", "Select Columns", choices=vchoices, inline = T),
        #notice that the 'choices' in selectInput are set to the predefined 
        #variables above whereas I would like to set them equal to the 
        #not yet defined uploaded file below in server.R

        tableOutput("table_display")
      ))

请注意&#39;选择&#39;在selectInput中设置为上面的预定义变量,而我想将它们设置为等于server.R中下面尚未定义的上传文件

server.R

  server = function(input, output) {

      info <- eventReactive(input$choice, {
        inFile <- input$file
        if (is.null(inFile))
          return(NULL)
        isolate(f<-read.table(inFile$datapath, header = T,
                               sep = "\t"))
        f
      })
      output$table_display<-renderTable({
        f<-info()
        f<-subset(f, select=input$columns) #subsetting takes place here
        head(f)
      })
    }

有没有人知道在ui中引用​​服务器中定义的变量的方法,从而允许交互操作?

1 个答案:

答案 0 :(得分:4)

您可以使用一系列功能update*Input - 在本例中为updateSelectInput。它的第一个参数必须是session,您还必须将session添加到server <- function(input, output)才能更新您的小部件。

您可以在点击actionButton后立即更新小部件 - 因此,您必须在updateSelectInput中使用eventReactive

让我们来看看我们如何做到这一点:

首先,您可以将新上传数据集的列名保存在变量中,例如vars,然后将其传递给函数updateSelectInput。 (selectInput的选项最初设置为NULL - 我们之前不需要指定它们,因为它们无论如何都会更新)

info <- eventReactive(input$choice, {
    inFile <- input$file
    # Instead # if (is.null(inFile)) ... use "req"
    req(inFile)

    # Changes in read.table 
    f <- read.table(inFile$datapath, header = input$header, sep = input$sep, quote = input$quote)
    vars <- names(f)
    # Update select input immediately after clicking on the action button. 
    updateSelectInput(session, "columns","Select Columns", choices = vars)

    f
  })

我在您的代码中添加了一个小upload interface

另一种方法是在服务器端定义小部件,然后通过renderUI函数将它们传递到客户端。您可以找到here示例。

完整示例:

library(shiny)

ui <- fluidPage(
  h2('The uploaded file data'),
  dataTableOutput('mytable'),
  fileInput('file', 'Choose info-file to upload',
            accept = c(
              'text/csv',
              'text/comma-separated-values',
              'text/tab-separated-values',
              'text/plain',
              '.csv',
              '.tsv'
            )
  ),
  # Taken from: http://shiny.rstudio.com/gallery/file-upload.html
  tags$hr(),
  checkboxInput('header', 'Header', TRUE),
  radioButtons('sep', 'Separator',
               c(Comma=',',
                 Semicolon=';',
                 Tab='\t'),
               ','),
  radioButtons('quote', 'Quote',
               c(None='',
                 'Double Quote'='"',
                 'Single Quote'="'"),
               '"'),
  ################################################################

  actionButton("choice", "incorporate external information"),

  selectInput("columns", "Select Columns", choices = NULL), # no choices before uploading 

  tableOutput("table_display")
)

server <- function(input, output, session) { # added session for updateSelectInput

  info <- eventReactive(input$choice, {
    inFile <- input$file
    # Instead # if (is.null(inFile)) ... use "req"
    req(inFile)

    # Changes in read.table 
    f <- read.table(inFile$datapath, header = input$header, sep = input$sep, quote = input$quote)
    vars <- names(f)
    # Update select input immediately after clicking on the action button. 
    updateSelectInput(session, "columns","Select Columns", choices = vars)

    f
  })

  output$table_display <- renderTable({
    f <- info()
    f <- subset(f, select = input$columns) #subsetting takes place here
    head(f)
  })
}
shinyApp(ui, server)