2015-12-30 18 views
5

Tôi có một ứng dụng sáng bóng chọn tệp xlsx/csv và tải tệp lên hệ thống và sau khi tải lên, tôi muốn điền tên cột của bảng dữ liệu mà tôi đã trích xuất vào selectInput() hoặc selectizeInput().Cách chuyển giá trị (lựa chọn) để selectizeInput() sau khi chọn dữ liệu từ giao diện người dùng trong ứng dụng sáng bóng?

Mã dưới đây đại diện cho một sự kiện về thành viên tải lên các tập tin dữ liệu

ui.R

library(markdown) 
    require(XLConnect) 


    shinyUI(navbarPage(img(class="img-polaroid", 
     src=paste0("http://www.iconsdb.com/icons/preview/black/stackoverflow-5-xl.png")), 

     tabPanel("Table", 
      titlePanel("Select Data"), 
      sidebarLayout(
      sidebarPanel(
       selectInput("fileType", "Select File Type:", 
        c("MS Excel Worksheet (xls,xlsx)" = "xls", 
         "Text/CSV (.csv)" = "csv" 
        ) 
        ), 
       fileInput('file1', 'Choose file to upload', 
        accept = c(
         'text/csv', 
         'text/comma-separated-values', 
         'text/tab-separated-values', 
         'text/plain', 
         '.csv', 
         '.tsv', 
         'application/vnd.ms-excel', 
         'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 
         '.xlsx', 
         '.xls' 
        ) 
       ), 
       tags$hr(), 
       checkboxInput('header', 'Header', TRUE), 
       radioButtons('sep', 'Separator', 
        c(Comma=',', 
         Semicolon=';', 
         Tab='\t'), 
        ','), 
       radioButtons('quote', 'Quote', 
        c(None='', 
         'Double Quote'='"', 
         'Single Quote'="'"), 
        '"'), 
       tags$hr() 


      ), 
      mainPanel(
       dataTableOutput('table') 
      ) 
     ) 

     ), 
     tabPanel("Plot", 
      sidebarLayout(
      sidebarPanel(
       radioButtons("plotType", "Plot type", 
        c("Scatter"="p", "Line"="l") 
       ) 
      ), 
      mainPanel(
       plotOutput("plot") 
      ) 
     ) 
     ), 
     navbarMenu("More", 
      tabPanel("Summary", 
      verbatimTextOutput("summary") 
     ) 
     ) 
    )) 

server.R

shinyServer(
    function(input, output, session) 
     { 
     output$plot <- renderPlot({ 
     plot(data$Count,data$Sales, type=input$plotType) 
     }) 

     output$summary <- renderPrint({ 
     summary(data) 
     }) 

     output$table <- renderDataTable({ 
     inFile <- input$file1 

     if (is.null(inFile)) 
      return(NULL) 
     if(input$fileType=='csv'){ 
      table1<-read.csv(inFile$datapath, header = input$header, 
       sep = input$sep, quote = input$quote) 
      return(table1) 
     } else if(input$fileType=='xls'){ 
      table1 <- readWorksheetFromFile(inFile$datapath, sheet=1) 
     } 

     }) 
    } 
) 

Đủ công bằng, bây giờ tôi có một hệ thống làm việc đầu vào có thể lấy dữ liệu từ nguồn Excel hoặc văn bản/CSV. Sau đó, tôi muốn tạo một danh sách năng động sẽ chuyển các giá trị của colnames(output$table) tới ui.R của tôi và đặt nó vào vị trí được chỉ định.

Tôi làm cách nào?


Tôi đã cố gắng sử dụng uiOutput() từ câu trả lời Mason DeCamillis để How to get vector of options from server.R to ui.R for selectInput in Shiny R App nhưng R là ném cho tôi một lỗi "đối tượng không tìm thấy".

Ngoài ra, nó xảy ra với tôi rằng tôi phải sử dụng reactive() để có thể chuyển dữ liệu đến và fro đến giao diện người dùng và máy chủ. Nhưng không có đầu mối, làm thế nào để làm điều đó.

+0

[Bài viết này] (http://shiny.rstudio.com/articles/dynamic-ui.html) là tài liệu tham khảo hữu ích cho bất kỳ ai cố gắng hiểu câu trả lời của @NicE bên dưới hoặc thường bắt đầu với giao diện người dùng phản ứng trong Sáng bóng. – alistaire

Trả lời

8

Bạn có thể lưu trữ bảng đã tải lên trong biến số data trong số server.R và sử dụng mã từ liên kết bạn đã đăng để điền vào selectInput.

Trong server.R của bạn, bạn có thể làm:

data <- reactive({ 
    inFile <- input$file1 
    if (is.null(inFile)) 
     return(NULL) 
    if(input$fileType=='csv'){ 
     table1<-read.csv(inFile$datapath, header = input$header, 
         sep = input$sep, quote = input$quote) 
     return(table1) 
    } else if(input$fileType=='xls'){ 
     table1 <- readWorksheetFromFile(inFile$datapath, sheet=1) 
    } 
    }) 

    output$table <- renderDataTable({ 
    data()  
    }) 

    output$selector <- renderUI({ 
     selectInput("var", "Choose name:", as.list(colnames(data()))) 
    }) 

và trong ui.R của bạn, thêm uiOutput("selector") nơi bạn muốn danh sách để được.

+0

Và làm cách nào để tôi sử dụng lại biến đã chọn trong 'server.R' ?? 'print (output $ selector)' đang ném một lỗi 'Object output not found'. Câu lệnh dưới đây có đúng phương pháp không ?? 'var <-reactive (đầu ra $ selector <- renderUI ({ selectInput (" var "," Chọn tên: ", as.list (tên (dữ liệu())) }))' – sunitprasad1

+0

@ sunitprasad1 Bạn nên sử dụng 'input $ selector' để lấy giá trị của biến được chọn như bình thường. Vì vậy, bạn sẽ có một cái gì đó như: 'var <- reactive ({x <- input $ selector; print (x)})'. – user5029763

Các vấn đề liên quan