2016-12-06 156 views
0

我正在尝试用R(第一次使用)在R中构建应用程序。闪亮的R选择输入不起作用

步骤1:读取Csv数据 步骤2:每个变量的直方图 步骤3:进一步分析。

我能够读取csv数据,但无法在selectinput中显示数据的colname。

代码正在运行,但变量名称的显示不起作用。 我的代码是:

library(shiny) 
ui<-navbarPage("Model Developement by Subhasish", 
      tabPanel("Data Import",sidebarLayout(sidebarPanel(fileInput("file","Upload your CSV",multiple = FALSE), 
                   tags$hr(), 
                   h5(helpText("Select the read.table parameters below")), 
                   checkboxInput(inputId = 'header', label = 'Header', value = FALSE), 
                   checkboxInput(inputId = "stringAsFactors", "stringAsFactors", FALSE), 
                   radioButtons(inputId = 'sep', label = 'Separator', choices = c(Comma=',',Semicolon=';',Tab='\t', Space=''), selected = ',') 
      ), 
      mainPanel(uiOutput("tb1")) 
      )), 
      tabPanel("Histogram",sidebarLayout(sidebarPanel(
      selectInput("headers","Select variable to view Histogram",choices =as.list(names(data)),multiple = FALSE)),mainPanel("mainpanel"))), 
      tabPanel("More") 
) 
server<-function(input,output) { data <- reactive({ 
    file1 <- input$file 
    if(is.null(file1)){return()} 
    read.table(file=file1$datapath, sep=input$sep, header = input$header, stringsAsFactors = input$stringAsFactors) 

}) 
output$table <- renderTable({ 
    if(is.null(data())){return()} 
    data() 
}) 
output$tb1 <- renderUI({ 
    tableOutput("table") 
}) 
} 
shinyApp(ui=ui,server=server) 

感谢您的帮助。

+0

基本上问题是,你正试图从'ui'访问'data'文件的列名,但你不能这样做,除非你从'服务器上通过名字'。你有两个选择:1)在ui中创建'selectInput',然后在服务器中使用'updateSeletctInput',2)在服务器上创建'selectInput',然后使用'renderUI'和'uiOutput'传递它。 –

+0

感谢@JhonPaul的指导。我会记住这一点,将来会使用它。在这种情况下,看起来第二种方式更好,因为它更具动态性。 – Subhasish1315

回答

1

在这里我添加了一个例子,你可以继续。我用我的虚拟.csv文件进行了测试,所以它应该可以工作。我还为直方图添加了一些测试用例,因此您不会看到打印的错误。

添加以下改变

  1. 我用renderUI创建selectInput称为
  2. 表输出是最好使用直接呈现的tableOutput("table")
  3. 我添加的直方图作为你想要一个

例如:

library(shiny) 
ui<-navbarPage("Model Developement by Subhasish", 
       tabPanel("Data Import",sidebarLayout(sidebarPanel(fileInput("file","Upload your CSV",multiple = FALSE), 
                    tags$hr(), 
                    h5(helpText("Select the read.table parameters below")), 
                    checkboxInput(inputId = 'header', label = 'Header', value = FALSE), 
                    checkboxInput(inputId = "stringAsFactors", "stringAsFactors", FALSE), 
                    radioButtons(inputId = 'sep', label = 'Separator', choices = c(Comma=',',Semicolon=';',Tab='\t', Space=''), selected = ',') 
       ), 
       mainPanel(tableOutput("table")))), 
       tabPanel("Histogram",sidebarLayout(sidebarPanel(
       uiOutput("ui1")), 
       mainPanel(plotOutput("myhist")))), 
       tabPanel("More") 
) 
server<-function(input,output) { 

    data <- reactive({ 
    file1 <- input$file 
    if(is.null(file1)){return()} 
    read.table(file=file1$datapath, sep=input$sep, header = input$header, stringsAsFactors = input$stringAsFactors) 
    }) 

    output$table <- renderTable({ 
    if(is.null(data())){return()} 
    data() 
    }) 
    output$ui1 <- renderUI({ 
    selectInput("headers","Select variable to view Histogram",choices =as.list(names(data())),multiple = FALSE) 
    }) 

    output$myhist <- renderPlot({ 
    histdata <- suppressWarnings(as.numeric((data()[,names(data()) %in% input$headers]))) 
    histdata <- histdata[!is.na(histdata)] 
    if(length(histdata) == 0){ 
     return() 
    } 
    hist(histdata,breaks=10) 
    box();grid() 
    }) 
} 
shinyApp(ui=ui,server=server) 

enter image description here

+0

谢谢@Pork Chop的答案。我将在下一次工作中使用你的方式。因为我是闪亮的新人。你能指导我为什么使用box(),grid();? – Subhasish1315

+0

'box(),grid();'只是添加一个框和一个网格到情节,就是这样,只是为了让它变得漂亮 –

+0

感谢@猪扒......其实我是R的新手,所以从未使用过它...很高兴并且很高兴能够学习一件新事物 – Subhasish1315