【问题标题】:using ifelse in renderDataTable (Shiny)在 renderDataTable 中使用 ifelse (Shiny)
【发布时间】:2017-11-26 02:41:25
【问题描述】:
 #server

tables <- reactiveValues(XVALUES=data.frame(),
                     YVALUES=data.frame())

output$userinput <- renderDataTable({

  if(input$name=="X" &  input$city=="BOS" & (is.data.frame(tables$XVALUES) && nrow(tables$XVALUES)!=0)){
      cbind(data.frame(Serial=seq(1:5)),tables$XVALUES)}
  else if(input$name=="X" &  input$city=="BOS" & (is.data.frame(tables$XVALUES) && nrow(tables$XVALUES)==0)){}
  else if (input$name=="Y" & input$city=="NYC"  & (is.data.frame(tables$YVALUES) && nrow(tables$YVALUES)!=0)){
    cbind(data.frame(Serial=seq(1:5)),tables$YVALUES)}
  else if (input$name=="Y" & input$city=="NYC"  & (is.data.frame(tables$YVALUES) && nrow(tables$YVALUES)==0)){}

})

基于两个用户输入,我通过向 XVALUES 或 YVALUES 数据框添加额外的 Serial 列来创建并显示一个新数据框。问题是即使 XVALUES/YVALUES 数据框为空,它仍会显示序列列。如果 XVALUES 数据框为空,我不希望显示任何内容。我哪里错了?

【问题讨论】:

    标签: r shiny shiny-server


    【解决方案1】:

    我无法重现该错误。我的机器上的桌子是空的。这是我输入的内容

    server = function( input, output, session ){
      tables <- reactiveValues(XVALUES=data.frame(),
                               YVALUES=data.frame())
    
      output$userinput <- renderDataTable({
        if(input$name=="X" &  input$city=="BOS" & (is.data.frame(tables$XVALUES) && 
          nrow(tables$XVALUES)!=0)){
          cbind(data.frame(Serial=seq(1:5)),tables$XVALUES)}
        else if(input$name=="X" &  input$city=="BOS" & (is.data.frame(tables$XVALUES) && 
           nrow(tables$XVALUES)==0)){}
        else if (input$name=="Y" & input$city=="NYC"  & (is.data.frame(tables$YVALUES) && 
          nrow(tables$YVALUES)!=0)){
          cbind(data.frame(Serial=seq(1:5)),tables$YVALUES)}
        else if (input$name=="Y" & input$city=="NYC"  & (is.data.frame(tables$YVALUES) && 
           nrow(tables$YVALUES)==0)){}
      })
    }
    
    ui = fluidPage(
      selectInput("name","name",c("X","Y")),
      selectInput("city","city",c("BOS","NYC")),
      dataTableOutput("userinput")
    )
    
    shinyApp( ui, server )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-21
      • 2020-05-14
      • 2014-10-09
      • 2020-10-07
      • 1970-01-01
      • 2015-01-07
      • 2015-10-07
      相关资源
      最近更新 更多