上传数据,将列名放在radiobutton中,并在Shiny

时间:2017-03-19 01:59:30

标签: shiny

我想上传.csv文件。然后,将单选按钮选项更新为上载文件的列名称,然后通过该单选按钮选择要显示的列。问题是每当我运行代码时,它都会给我这个错误。

P.S.1。在运行此应用程序之前有没有办法读取数据?喜欢在另一个应用程序?

enter image description here

 library(shiny)
 ui = basicPage(
    fileInput('uploadedcsv', "", accept = '.csv'),

    radioButtons(
      "column1",
      "select columns",
      choices = "",
      inline = T
    ),
    radioButtons(
      "column2",
      "select columns",
      choices = "",
      inline = T
    ),
    dataTableOutput('mytable')
  )

 server = function(session,input, output) {

    z <- reactive({
      infile <- input$uploadedcsv

      if (is.null(infile))
        return(NULL)

      read.csv(infile$datapath, header = TRUE, sep = ",")
    })

    observe({
      vchoices <- names(z())
      updateRadioButtons(session, "column1", choices = vchoices)
      updateRadioButtons(session, "column2", choices = vchoices)
    })
    z <- reactive(z[,c(input$column1,input$column2)])
    output$mytable = renderDataTable(z())
  }
shinyApp(ui = ui, server = server)

1 个答案:

答案 0 :(得分:0)

z是不可进行子设置的闭包:

z <- reactive(z[,c(input$column1,input$column2)])

z 是您的第一个作业返回的反应函数。它不是子集(不能索引它),因为它是一个函数。您可以调用 z 并将结果编入索引,如下面的 renderDataTable renderDataTable 将调用 z(),并对 z 的输出 input $ column1 和输入$ column2

server = function(input, output, session) {

  # z is reactive to a change in the input data

  z <- reactive({
    infile <- input$uploadedcsv

    if (is.null(infile))
      return(NULL)

    read.csv(infile$datapath, header = TRUE, sep = ",")
  })

  observe({
    vchoices <- names(z())
    updateRadioButtons(session, "column1", choices = vchoices)
    updateRadioButtons(session, "column2", choices = vchoices)
  })

  # renderDataTable is reactive to a change in the input data
  # or the selected columns

  output$mytable = renderDataTable({
    z()[,c(input$column1, input$column2)]
  })
}