簡體   English   中英

基於條件輸入的閃亮R DT中的列子集

[英]Columns subsetting in shiny R DT based on conditional input

這類似於先前的問題[mine( Shiny R Reactive輸出中的多個表達式 我希望能夠基於selectinput對數據進行子集化。 我拆分了選擇輸入,並在selectinput上添加了一個復選框。 現在,我希望僅使用if子句調整colum向量,但是它不起作用。

library(shiny)
library(datasets)


DT<-rbind(data.table(LP=rep("with.LP",3),Total=seq(6,8)+seq(1,3)/2,Life=seq(1,3)/2),
    data.table(LP=rep("wo.LP",3),Total=seq(6,8),Life=0))

Cols<-c("Total")
server<-shinyServer(function(input, output) {

  # renderUI for conditional checkbox in UI for separately
  output$conditionalInput<-    renderUI({
                                 if(input$life.pension=="with.LP"){
                                    checkboxInput("show.LP", "Show separately", FALSE)
                                        }
                                     }) 
  #Condition if input$show.lp == TRUE
  cond.cols<- reactive({
      if(input$show.lp) {
        c(Cols,"Life")}
          })

   # calculate table
  output$view <- renderTable({
    head(DT[LP==input$life.pension,.SD,.SDcols=Cols])
  })
})

# Define UI for dataset viewer application
ui<-shinyUI(fluidPage(

  # Application title
  titlePanel("Shiny Example"),
  # Sidebar with controls to select a dataset and specify the
  # number of observations to view
  sidebarLayout(
    sidebarPanel(
     selectInput("life.pension", label = h3("Include L&P?"),
                    choices = list("Yes" = "with.LP", "No" = "wo.LP")
                                    ,selected = "with.LP"),
          uiOutput("conditionalInput")
    ),

    # Show a summary of the dataset and an HTML table with the 
     # requested number of observations
    mainPanel(
      tableOutput("view")
     )
  )
))
runApp(list(ui=ui,server=server))

1)輸入名稱中的錯字:“ show.LP”!=“ show.lp”

2)您從不使用cond.cols因此您的checkBox不執行任何操作

3)嘗試

#Condition if input$show.lp == TRUE
  cond.cols<- reactive({
    if(input$show.LP==TRUE & input$life.pension=="with.LP") {
      c(Cols,"Life")
    }else{
        Cols
      }
  })

head(DT[LP==input$life.pension,.SD,.SDcols=cond.cols()])

更新

檢查輸入是否存在

cond.cols<- reactive({
    if(!is.null(input$show.LP)){
    if(input$show.LP==TRUE & input$life.pension=="with.LP") {
      c(Cols,"Life")
    }else{
      Cols
    }}else{
      Cols
    }
  })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM