R Shiny:fileInput,NULL出现在str()控制台输出的末尾

时间:2014-09-08 08:29:19

标签: r shiny random-forest

使用R shiny,我正在开发一个简单的应用程序,允许用户从文件中输入数据并进行一些简单的分析。我的第一步是允许输入并根据给定用户的输入选项使用反应包装器。以下将解释我在做什么:

server.R:

require(shiny)

shinyServer(function(input,output){
    Data<-reactive({
        datFile<-input$datFile
        path<-as.character(datFile$datapath)
        df<-read.csv(path,
                     header=input$datHeader,
                     sep=input$datSep,
                     quote=input$datQuote,
                     stringsAsFactors=F)

        print(str(df)) # <---- print to console
        return(df)
   })

output$dataPreview<-renderTable({
    if (is.null(Data())) return(NULL)
    data.frame(Data()[1:input$previewRows,])
},digits=3)
})

ui.R:

shinyUI(pageWithSidebar(

  titlePanel("My app"),

  #sidebar panel
  sidebarPanel(

    tags$hr(),

    #data input
    fileInput('datFile',
              tags$h5(tags$strong('Choose .csv or .txt file to upload local file')),
              accept=c('text/csv', 'text/comma-separated-values,text/plain')),

    tags$hr(),

    #checkbox to indicate header == true or false
    checkboxInput('datHeader','Header contains attribute names', TRUE),

    tags$hr(),

    #file type
    radioButtons('datSep',
                 tags$h5(tags$strong("Separator")),
                 c("Comma(.csv)"=',',
                   "Tab(.txt/.tsv)"='\t'),
                 ','),

    tags$hr(),

    #quotation used in data file
    radioButtons('datQuote',
                 tags$h5(tags$strong('Quotes used in data file')),
                 c(None='',
                   'Double Quotes'='"',
                   'Single Quotes'="'"),
                 '"'),

    tags$hr(),

    #transpose data
    checkboxInput('datTranspose','Transpose data?',FALSE),
    width=3), #end sidebar panel

  mainPanel(
    tags$h4("Displaying a preview of your data"),
    sliderInput("previewRows","Number of rows to display",
                min=1,max=20,value=10,step=1,animate=TRUE),
    tableOutput("dataPreview"))
))

问题是当

print(str(df)) # <---- print to console

行运行,我的控制台输出结束时出现NULL:

'data.frame':   32561 obs. of  15 variables:
 $ age           : int  39 50 38 53 28 37 49 52 31 42 ...
 $ workclass     : Factor w/ 9 levels " ?"," Federal-gov",..: 8 7 5 5 5 5 5 7 5 5 ...
 $ fnlwgt        : int  77516 83311 215646 234721 338409 284582 160187 209642 45781 159449 ...
 $ education     : Factor w/ 16 levels " 10th"," 11th",..: 10 10 12 2 10 13 7 12 13 10 ...
 $ education.num : int  13 13 9 7 13 14 5 9 14 13 ...
 $ marital.status: Factor w/ 7 levels " Divorced"," Married-AF-spouse",..: 5 3 1 3 3 3 4 3 5 ...
 $ occupation    : Factor w/ 15 levels " ?"," Adm-clerical",..: 2 5 7 7 11 5 9 5 11 5 ...
 $ relationship  : Factor w/ 6 levels " Husband"," Not-in-family",..: 2 1 2 1 6 6 2 1 2 1 ...
 $ race          : Factor w/ 5 levels " Amer-Indian-Eskimo",..: 5 5 5 3 3 5 3 5 5 5 ...
 $ sex           : Factor w/ 2 levels " Female"," Male": 2 2 2 2 1 1 1 2 1 2 ...
 $ capital.gain  : int  2174 0 0 0 0 0 0 0 14084 5178 ...
 $ capital.loss  : int  0 0 0 0 0 0 0 0 0 0 ...
 $ hours.per.week: int  40 13 40 40 40 40 16 45 50 40 ...
 $ native.country: Factor w/ 42 levels " ?"," Cambodia",..: 40 40 40 40 6 40 24 40 40 40 ...
 $ income        : Factor w/ 2 levels " <=50K"," >50K": 1 1 1 1 1 1 1 2 2 2 ...
NULL

数据集来自http://archive.ics.uci.edu/ml/datasets/Adult

以下是我使用的确切文件的链接: https://dl.dropboxusercontent.com/u/36842028/linkouts/adult.txt

这个NULL显然影响了我的下游分析,特别是randomForest()。我必须从机器学习步骤到数据输入步骤一直跟踪这个NULL。知道为什么这个小家伙在这里?感谢任何帮助,非常感谢你!

1 个答案:

答案 0 :(得分:1)

没有NULL这似乎是print.default对结果采取行动的结果 属于str的{​​{1}}。

NULL

您可以删除> print(str(data.frame(a=1:2, b= 3:4))) 'data.frame': 2 obs. of 2 variables: $ a: int 1 2 $ b: int 3 4 NULL > str(data.frame(a=1:2, b= 3:4)) 'data.frame': 2 obs. of 2 variables: $ a: int 1 2 $ b: int 3 4 以便更改为

print
str(df) # <---- print to console return(df) 中的

您将观察到:

server.R

在控制台中。