2016-05-26 8 views
3

csvファイルをアップロードする前にエラーinvalid first argumentが表示されます。アップロードしたファイルのデータを使用してグラフをプロットする際にエラーが発生しました

csvファイルをアップロードした後、アプリが正常に動作します。このエラーを取り除く方法はありますか?

server.R

library(ggplot2) 
library(shiny) 
shinyServer(func <- function(input,output){ 


    data <- reactive({ 
    file1 <- input$file 
    if(is.null(file1)){return()} 
    read.csv(file=file1$datapath, header=TRUE) 
    }) 

    output$xselect <- renderUI({ 
    selectInput("xcol","X variable",names(data())) 
    }) 

    output$yselect <- renderUI({ 
    selectInput("ycol","Y variable", names(data())) 
    }) 



    output$p <- renderPlot({ 
    data() 
    plot(get(input$xcol), get(input$ycol)) 
    }) 


    } 
) 
+0

を得:ここではそれを使用して、より完全な例があります。 –

答えて

2

オブジェクトが存在するかどうか私たちは、チェックすることができ:

validate(need(data(), "Dataframe not found")) 

それとも私たちはすべてのエラーメッセージ隠すことができます:正確に

tags$style(type="text/css", 
      ".shiny-output-error { visibility: hidden; }", 
      ".shiny-output-error:before { visibility: hidden; }") 
+0

私はその最初の答えが好きなので、私はupvoted。それについて知らなかった。しかし、おそらく最後の手段として使用されるべきではありません。 –

+0

@MikeWise最初のオプションは、カーペットの下にゴミを一掃するようなものです。私は解決策の順序を入れ替えます。 – zx8754

+1

「展開前の最後のステップ」としても知られています:) –

2

しません完全な例ですが、近いenoug私は推測する。

私はあなたがvalidateコマンドを探していると思います。

library(ggplot2) 
library(shiny) 

ui <- fluidPage(
    textInput("xcol", "xcol:", "wt"), 
    textInput("ycol", "ycol:", "mpg"), 
    fileInput("file", label = "csv file"), 
    plotOutput("p") 
) 

server <- function(input,output){ 
    data <- reactive({ 
    file1 <- input$file 
    if(is.null(file1)){return()} 
    read.csv(file=file1$datapath, header=TRUE) 
    }) 
    output$xselect <- renderUI({ 
    selectInput("xcol","X variable",names(data())) 
    }) 
    output$yselect <- renderUI({ 
    selectInput("ycol","Y variable", names(data())) 
    }) 
    output$p <- renderPlot({ 
    validate(need(input$file,"need filename")) 
    df <- data() 
    plot(df[[input$xcol]], df[[input$ycol]], 
     xlab=input$xcol,ylab=input$ycol) 
    }) 
} 
shinyApp(ui=ui, server=server) 

実際 `ggplot2`を使用していない

enter image description here

関連する問題