2016-08-09 8 views
1

ファイル(データフレーム)をアップロードできるアプリを作るために、さまざまなコードソースをマージしました。アップロードされたデータフレームから特定の列を選択する

しかし、これを超えて、データフレームから特定の列を選択して分析することもできます。ただし、ui.Rスクリプトでそれを参照できるように、指定されたデータフレームを事前に定義する必要があるため、難しいです。 これまでに定義されていないデータフレームがサイトにアップロードされると、それがサーバーで定義されているようui.Rで....

事前に定義された変数

vchoices <- 1:ncol(mtcars) 
names(vchoices) <- names(mtcars) 

ui.R

runApp(
     ui = basicPage(
     h2('The uploaded file data'), 
     dataTableOutput('mytable'), 
     fileInput('file', 'Choose info-file to upload', 
        accept = c(
        'text/csv', 
        'text/comma-separated-values', 
        'text/tab-separated-values', 
        'text/plain', 
        '.csv', 
        '.tsv' 
       ) 
     ), 
     actionButton("choice", "incorporate external information"), 

     selectInput("columns", "Select Columns", choices=vchoices, inline = T), 
     #notice that the 'choices' in selectInput are set to the predefined 
     #variables above whereas I would like to set them equal to the 
     #not yet defined uploaded file below in server.R 

     tableOutput("table_display") 
    )) 

お知らせselectInputの '選択肢' はpredefiに設定されています私は同じ

server.R

で下記にまだ定義されていない、アップロードされたファイルにそれらを設定したいのに対し、上記NED変数server.R

server = function(input, output) { 

     info <- eventReactive(input$choice, { 
     inFile <- input$file 
     if (is.null(inFile)) 
      return(NULL) 
     isolate(f<-read.table(inFile$datapath, header = T, 
           sep = "\t")) 
     f 
     }) 
     output$table_display<-renderTable({ 
     f<-info() 
     f<-subset(f, select=input$columns) #subsetting takes place here 
     head(f) 
     }) 
    } 

は定義されています変数を参照する方法のいずれかを知っていますサーバー内で、UI内で実行し、対話型操作を可能にしますか?

答えて

3

update*Inputのファミリを使用できます。この場合はupdateSelectInputです。最初の引数はsessionでなければならず、ウィジェットを更新するにはsessionserver <- function(input, output)に追加する必要があります。

actionButtonをクリックした直後にウィジェットのアップデートを行うことができます。updateSelectInputeventReactiveの範囲で使用しなければなりませんでした。


のは、我々はそれを行うことができる方法を見てみましょう:

まず、あなたは、たとえば、変数の新しいアップロードされたデータセットの列の名前を保存varsして、機能updateSelectInputにそれを渡すことができます。 (selectInputの選択肢が最初NULLに設定されている - 彼らはとにかく更新しようとしているので、私たちは前にそれらを指定する必要はありません)

info <- eventReactive(input$choice, { 
    inFile <- input$file 
    # Instead # if (is.null(inFile)) ... use "req" 
    req(inFile) 

    # Changes in read.table 
    f <- read.table(inFile$datapath, header = input$header, sep = input$sep, quote = input$quote) 
    vars <- names(f) 
    # Update select input immediately after clicking on the action button. 
    updateSelectInput(session, "columns","Select Columns", choices = vars) 

    f 
    }) 

私はあなたに小さなupload interfaceを追加しましたコード。

もう1つの方法は、サーバー側でウィジェットを定義してから、renderUI関数を介してクライアント側に渡すことです。あなたはhereの例を見つけることができます。


全例:完全に私が最初に入力がない列などのデータが少し厄介に見えたselected-ていたとき、私はエラーを取得しているという事実を除いて

library(shiny) 

ui <- fluidPage(
    h2('The uploaded file data'), 
    dataTableOutput('mytable'), 
    fileInput('file', 'Choose info-file to upload', 
      accept = c(
       'text/csv', 
       'text/comma-separated-values', 
       'text/tab-separated-values', 
       'text/plain', 
       '.csv', 
       '.tsv' 
      ) 
), 
    # Taken from: http://shiny.rstudio.com/gallery/file-upload.html 
    tags$hr(), 
    checkboxInput('header', 'Header', TRUE), 
    radioButtons('sep', 'Separator', 
       c(Comma=',', 
       Semicolon=';', 
       Tab='\t'), 
       ','), 
    radioButtons('quote', 'Quote', 
       c(None='', 
       'Double Quote'='"', 
       'Single Quote'="'"), 
       '"'), 
    ################################################################ 

    actionButton("choice", "incorporate external information"), 

    selectInput("columns", "Select Columns", choices = NULL), # no choices before uploading 

    tableOutput("table_display") 
) 

server <- function(input, output, session) { # added session for updateSelectInput 

    info <- eventReactive(input$choice, { 
    inFile <- input$file 
    # Instead # if (is.null(inFile)) ... use "req" 
    req(inFile) 

    # Changes in read.table 
    f <- read.table(inFile$datapath, header = input$header, sep = input$sep, quote = input$quote) 
    vars <- names(f) 
    # Update select input immediately after clicking on the action button. 
    updateSelectInput(session, "columns","Select Columns", choices = vars) 

    f 
    }) 

    output$table_display <- renderTable({ 
    f <- info() 
    f <- subset(f, select = input$columns) #subsetting takes place here 
    head(f) 
    }) 
} 
shinyApp(ui, server) 
+0

作品... 。私は列を選択したような時間まで、テーブルの表示を延期する方法があります。if(input $ columns){f < - subset(f、select = input $ columns)} ???どうもありがとう –

+0

もちろん、 'req'や' validate'関数を使ってやってみるといいです。 'render *'関数の中に単に '' req(input $ xyz) ''と置くだけです。 「入力$ xyzが利用可能であることを要求する」と読むことができます。 [ここ](http://shiny.rstudio.com/articles/req.html)には、欠落している入力を処理する素晴らしい話題があります。 –

関連する問題