2013-08-29 50 views
0

我想在UI中做選擇選項,它應該自動將變量名稱從輸入數據中提取到列表中。這裏我選擇選項使用列表(LS(input.file1),但它不工作閃亮:選擇選項會自動獲取數據變量

請幫我

ui.R:

library(shiny) 
shinyUI(pageWithSidebar(
    headerPanel("Demand Forecast", "Flowserve"), 
    sidebarPanel(
    fileInput('file1', 'Select csv file', 
       accept=c('text/csv') 
      ), 
    checkboxInput('header', 'Header', TRUE), 
    radioButtons('sep', 'Separator', 
       c(Comma=',', Semicolon=';', Tab='\t') 
       ), 
    tags$hr(), 
    selectInput("product", "Select Product", 
        list(ls(input.file1)) 

       ) 
)) 

server.R:

library(shiny) 
shinyServer(function(input,output){ 

#Assigning data to a variable "data1" 
    data1 = reactive({ 
    inFile<-input$file1 
    if(is.null(inFile)) 
    return(NULL) 
    read.csv(inFile$datapath, header=input$header, sep=input$sep) 
    }) 

    sub=reactive({ 
     subset(data1(), select=paste0(input$product)) 
    }) 

output$contents<-renderTable({ 
     if (is.null(input$file1)) { return() }        
      sub() 
     }) 
}) 

這裏是CSV樣品:

Product1 Product2 Product3 
5    10    17 
8    16    26 
10   20    32 
16   32    50 
18   36    56 
20   40    62 
+0

當您提供簡短樣本csv時,獲得答案的機會會更好。 –

+0

好的...我編輯了我的問題 – Punith

回答

4

如果您在代碼中看到ls(),則幾乎總是錯誤的。然而,棘手的部分是從服務器設置ui項目:您需要更新----系列功能。

以下是將csv的名稱填充到產品表中的部分。你必須添加更多的標準代碼來完成填充。

#server.r 
library(shiny) 
shinyServer(function(input,output,session){ 

    observe({ 
    inFile<-input$file1 
    print(inFile) 
    if(is.null(inFile)) 
     return(NULL) 
    dt = read.csv(inFile$datapath, header=input$header, sep=input$sep) 
    ## Decide later what to do with the data, here we just fill 
    updateSelectInput(session, "product", choices = names(dt)) 
    }) 
}) 

#ui.r 
library(shiny) 
shinyUI(pageWithSidebar(
    headerPanel("Demand Forecast", "Flowserve"), 
    sidebarPanel(
    fileInput('file1', 'Select csv file', 
       accept=c('text/csv') 
    ), 
    checkboxInput('header', 'Header', TRUE), 
    radioButtons('sep', 'Separator', 
       c(Tab='\t', Comma=',', Semicolon=';') 
    ), 
    tags$hr(), 
    selectInput("product", "Select Product","") 
    ), 
    mainPanel(tableOutput('contents')) 

)) 
+0

現在,在「選擇產品」選項中,它將所有產品集中在一個選項中。但我想分開。 – Punith

+0

不適合我;但可能我不明白你想要什麼。 –

+0

我使用了您提供的代碼。在這個應用程序的網頁中,只有一個選擇在「選擇選項」中選擇。
如果我選擇上面的csv樣本,它會選擇「Product1.Product2.Product3」。但我想要3個單獨的選項(「Product1」「Product2」Product3「) – Punith