2016-08-24 195 views
0

这与[mine(Multiple expressions in Shiny R Reactive output))的先前问题类似!我希望能够根据selectinput来对数据进行子集化。我分割了选定的输入并添加了一个有选择输入条件的复选框。现在我希望只用一个if子句来调整colum向量,但它不起作用。在输入姓名根据条件输入在闪亮的R DT中进行子集划分

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)) 

回答

4

1)错字: “show.LP”= “show.lp”

2)你从来不会使用cond.cols所以你的复选框,什么都不做

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 
    } 
    }) 
+0

谢谢你的工作,但对于我在R个接收 警告一个错误:错误的,如果:变量是长度为零 堆栈跟踪(最内侧的第1号): 86 :反应性cond.cols [#12] 75:cond.cols 74:EVAL 73:EVAL 72:[.data.table 71:[ 70:头 69:renderTable [#22] 68: func 67:output $ view 1:runApp 感谢您找到我的排字错误8(我真的需要== TRUE吗?我认为输入$ show.LP已经是TRUE/FALSE?第二个条件是多余的,因为只有第二个条件为TRUE时,输入$ show.LP才存在。 –

+0

这是因为你没有'输入$ show.LP'当应用程序启动(见更新) – Batanichek

+0

你不需要== TRUE,但以这种方式容易承受。第二个条件需要(在我的R),因为如果你检查输入$ show.LP,然后改变输入$ life.pension你看到两列 – Batanichek