2017-01-18 76 views
0

我想建立一個閃亮的應用程序,將能夠加載數據集在服務器功能,然後根據用戶選擇,然後如果有一個因素變量使用conditionalPanel打開復選框。有沒有辦法從服務器輸出變量作爲condtionalPanel的條件?閃亮 - 條件面板 - 設置條件作爲服務器的輸出

這裏是我的嘗試:

library(shiny) 
library(caret) 

ui <- fluidPage(
    selectInput('dataset', 'Select Dataset', 
       list(GermanCredit = "GermanCredit", 
        cars = "cars")), 

    conditionalPanel(
    condition = "output.factorflag == true", 
    checkboxInput("UseFactor", "Add Factor Variable") 
) 
) 


server <- function(input, output) { 
    # Loading the dataset 
    df <- reactive({ 
    if(input$dataset == "GermanCredit"){ 
     data("GermanCredit") 
     df <- GermanCredit 
    }else if(input$dataset == "cars"){ 
     data(cars) 
     df <- cars 
    } 

    return(df) 
    }) 

    # Loading the variables list 
    col_type <- reactive({ 
    col_type <- rep(NA,ncol(df())) 
    for(i in 1:ncol(df())){ 
     col_type[i] <- class(df()[,i]) 
    } 
    return(col_type) 
    }) 

    outputOptions(output, "factorflag", suspendWhenHidden = FALSE) 


    output$factorflag <- reactive({ 
    if("factor" %in% col_type()){ 
     factor.flag <- TRUE 
    } else {factor.flag <- FALSE} 
    } 
) 
} 


shinyApp(ui = ui, server = server) 

Thank you in advance! 

回答

1

你是幾乎沒有,你需要把outputOptionsfactorflag的聲明之後。剛剛重新設計了一下你的代碼:

library(shiny) 
library(caret) 

ui <- fluidPage(
    selectInput('dataset', 'Select Dataset', 
       list(GermanCredit = "GermanCredit", 
        cars = "cars")), 

    conditionalPanel(
    condition = "output.factorflag == true", 
    checkboxInput("UseFactor", "Add Factor Variable") 
) 
) 


server <- function(input, output) { 
    # Loading the dataset 
    df <- reactive({ 
    if(input$dataset == "GermanCredit"){ 
     data("GermanCredit") 
     GermanCredit 
    }else { 
     data("cars") 
     cars 
    } 
    }) 
    output$factorflag <- reactive("factor" %in% sapply(df(),class)) 
    outputOptions(output, "factorflag", suspendWhenHidden = FALSE) 
} 

shinyApp(ui = ui, server = server) 
+0

這很好,也感謝您的其他建議! –