2015-01-04 15 views
8

Shinyと実験しています。私はそれを愛しています。学生がcsvファイルをアップロードしてから従属変数と独立変数を選択し、Rが線形回帰を計算する小さなアプリケーションを構築しました。それはうまく動作します。あなたがしたい場合は、[あなたはそれをテストするためにthis fileを使用することができますShiny:ファイルがアップロードされた後にのみボタンを表示します。

http://carlosq.shinyapps.io/Regresion

:私はそれがでアップロードされています。 "ビールは、" "ID" を除く従属変数と変数の残りの部分で独立している]

ここserver.Rです:

# server.R 
library(shiny) 

shinyServer(function(input, output) { 

    filedata <- reactive({ 
    infile <- input$file1 
    if (is.null(infile)){ 
     return(NULL)  
    } 
    read.csv(infile$datapath) 
    }) 

    output$dependent <- renderUI({ 
    df <- filedata() 
    if (is.null(df)) return(NULL) 
    items=names(df) 
    names(items)=items 
    selectInput("dependent","Select ONE variable as dependent variable from:",items) 
    }) 


    output$independents <- renderUI({ 
    df <- filedata() 
    if (is.null(df)) return(NULL) 
    items=names(df) 
    names(items)=items 
    selectInput("independents","Select ONE or MANY independent variables from:",items,multiple=TRUE) 
    }) 


    output$contents <- renderPrint({ 
    input$action 
    isolate({ 
     df <- filedata() 
     if (is.null(df)) return(NULL) 
     fmla <- as.formula(paste(input$dependent," ~ ",paste(input$independents,collapse="+"))) 
     summary(lm(fmla,data=df)) 
    }) 
    }) 

}) 

そして、ここではui.Rです:

# ui.R 
library(shiny) 

shinyUI(fluidPage(
    titlePanel("Multiple Linear Regression"), 
    sidebarLayout(
    sidebarPanel(
     fileInput('file1', 'Choose CSV File', 
       accept=c('text/csv', 
         'text/comma-separated-values,text/plain', 
         '.csv')), 

     tags$hr(), 
     uiOutput("dependent"), 
     uiOutput("independents"), 
     tags$hr(), 
     actionButton("action", "Press after reading file and selecting variables") 

    ), 
    mainPanel(
     verbatimTextOutput('contents') 
    ) 
) 
)) 

私の質問です:成功したアップロードで「ファイルを読み込んだ後に変数を選択」ボタンの外観を条件付きにしたいと思います。

Make conditionalPanel depend on files uploaded with fileInput

をしかし、私はちょうどそれを動作させることはできません。

私はここに含まれる提案を使用しようとしました。

私は何か助けてください。

答えて

7

このコードは

ui.R

# ui.R 
library(shiny) 

shinyUI(fluidPage(
    titlePanel("Multiple Linear Regression"), 
    sidebarLayout(
    sidebarPanel(
     fileInput('file1', 'Choose CSV File', 
       accept=c('text/csv', 
         'text/comma-separated-values,text/plain', 
         '.csv')), 

     tags$hr(), 
     uiOutput("dependent"), 
     uiOutput("independents"), 
     tags$hr(), 
     uiOutput('ui.action') # instead of conditionalPanel 
    ), 
    mainPanel(
     verbatimTextOutput('contents') 
    ) 
) 
)) 

server.R

# server.R 
library(shiny) 

shinyServer(function(input, output) { 

    filedata <- reactive({ 
    infile <- input$file1 
    if (is.null(infile)){ 
     return(NULL)  
    } 
    read.csv(infile$datapath) 
    }) 

    output$dependent <- renderUI({ 
    df <- filedata() 
    if (is.null(df)) return(NULL) 
    items=names(df) 
    names(items)=items 
    selectInput("dependent","Select ONE variable as dependent variable from:",items) 
    }) 


    output$independents <- renderUI({ 
    df <- filedata() 
    if (is.null(df)) return(NULL) 
    items=names(df) 
    names(items)=items 
    selectInput("independents","Select ONE or MANY independent variables from:",items,multiple=TRUE) 
    }) 


    output$contents <- renderPrint({ 
    input$action 
    isolate({ 
     df <- filedata() 
     if (is.null(df)) return(NULL) 
     fmla <- as.formula(paste(input$dependent," ~ ",paste(input$independents,collapse="+"))) 
     summary(lm(fmla,data=df)) 
    }) 
    }) 


    output$ui.action <- renderUI({ 
    if (is.null(input$file1)) return() 
    actionButton("action", "Press after reading file and selecting variables") 
    }) 

}) 
+0

ありがとうございました。私はあなたの解決策を試しました。それはボタンが消えるようにする...そしてそれは良いです。しかし、ファイルをアップロードした後は表示されません。あなたのserver.Rファイルには、ファイルが正常にアップロードされたかどうかをチェックする行が含まれています。 – user23438

+0

@ user23438、 'conditionPanel'を使って解決策を得ることができませんでした。なぜなら、' condition'を正しく設定する方法がわからなかったからです。答えを編集しました。これは現在uiOutputに基づいています。 –

+0

もう一度マラートに感謝します。あなたの新しいソリューションは私をソリューションに近づけます。今すぐボタンが表示されますが、出力$の内容に問題があります。このセクションのコードを入力$ actionによってアクティブ化された "isolate({})"で囲む前に、ボタンがなくなったので、入力$アクションはなくなりました。私は隔離を取り除くことができましたが、正しい変数が選択されるまでガベージを印刷します。 – user23438

9

はここworking ShinyAppとui.Rとserver.Rベースの両方の最終版だ私のために働きましたMaratが提供したすべての提案に基づいています。

まずui.R

# ui.R 

library(shiny) 

shinyUI(fluidPage(
    titlePanel("Multiple Linear Regression with R/Shiny"), 
    sidebarLayout(
    sidebarPanel(
     p("Please upload a CSV formatted file with your data."), 
     fileInput('file1', label='Click button below to select the file in your computer.', 
       accept=c('text/csv', 
         'text/comma-separated-values,text/plain', 
         '.csv')), 

     tags$hr(), 
     uiOutput("dependent"), 
     uiOutput("independents"), 
     tags$hr(), 
     uiOutput('ui.action') # instead of conditionalPanel 
    ), 
    mainPanel(
     p("Here's the output from your regression:"), 
     verbatimTextOutput('contents') 
    ) 
) 
)) 

とserver.R

# server.R 

library(shiny) 

shinyServer(function(input, output) { 

    filedata <- reactive({ 
    infile <- input$file1 
    if (is.null(infile)){ 
     return(NULL)  
    } 
    read.csv(infile$datapath) 
    }) 

    output$ui.action <- renderUI({ 
    if (is.null(filedata())) return() 
    actionButton("action", "Run regression") 
    }) 

    output$dependent <- renderUI({ 
    df <- filedata() 
    if (is.null(df)) return(NULL) 
    items=names(df) 
    names(items)=items 
    selectInput("dependent","Now select ONE variable as dependent variable from:",items) 
    }) 


    output$independents <- renderUI({ 
    df <- filedata() 
    if (is.null(df)) return(NULL) 
    items=names(df) 
    names(items)=items 
    selectInput("independents","Also select ONE or MANY independent variables in the box below. You can change your selection several times:",items,multiple=TRUE) 
    }) 


    output$contents <- renderPrint({ 
    if (is.null(input$action)) return() 
    if (input$action==0) return() 
    isolate({ 
     df <- filedata() 
     if (is.null(df)) return(NULL) 
     fmla <- as.formula(paste(input$dependent," ~ ",paste(input$independents,collapse="+"))) 
     summary(lm(fmla,data=df)) 
    }) 
    }) 


}) 

もう一度あなたの助けマラーに感謝。

+0

あなたは大歓迎です! –

関連する問題