SHINY-ConditionalPanel-将条件设置为服务器输出 [英] Shiny - conditionalPanel - set condition as output from server

查看:8
本文介绍了SHINY-ConditionalPanel-将条件设置为服务器输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在SHILY中构建一个应用程序,它将能够在服务器函数中加载数据集,然后根据用户选择,然后使用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!

推荐答案

您快到了,您需要将outputOptions放在factorflag的声明后面。刚刚重新设计了一点代码:

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)

这篇关于SHINY-ConditionalPanel-将条件设置为服务器输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆