如果条件在闪亮的DT renderdatatable中 [英] if condition in shiny DT renderdatatable

查看:88
本文介绍了如果条件在闪亮的DT renderdatatable中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在闪亮的DT :: renderDataTable中使用if条件,但是它不能正常工作.这是一个最小的示例:

I want to use an if condition in a shiny DT::renderDataTable, but it does not work properly. Here is a minimal example:

library(DT)
library(shiny)

ui <- shinyUI(fluidPage(
  titlePanel(""),

  sidebarLayout(
    sidebarPanel(radioButtons("button", "", choices=c("a", "b"))),
      mainPanel(DT::dataTableOutput("table1")) 
  )
))

server <- function(input, output){
 x <- data.frame(col1 = 1:2, col2 = 3:4, col3 =5:6)
 y <- data.frame(col1 = 10:11, col2 = 20:21)

 output$table1 <- DT::renderDataTable({
   if(input$button == "a"){
     datatable(x)
   }
   if(input$button == "b"){
     datatable(y)
   }
   })
}

shinyApp(ui, server)

如果选择"a",则该应用程序不显示任何输出,但如果选择"b",则该应用程序将完美运行.有人有主意吗?谢谢.

The app does not show any output, if "a" is chosen but works perfectly if "b" is chosen. Does anyone have an idea? Thanks.

推荐答案

如果将if结构更改为 else if ,它将起作用.

If you change your if structure to an else if it will work.

server <- function(input, output){   
    x <- data.frame(col1 = 1:2, col2 = 3:4, col3 =5:6)   
    y <- data.frame(col1 = 10:11, col2 = 20:21)
    output$table1 <- DT::renderDataTable({
        if(input$button == "a"){
          datatable(x)
        }
        else if(input$button == "b"){
          datatable(y)
        }   
    }) 
}

反应性元素从{}内部获取最后的东西.我猜你第二个if在选择a时返回NULL.

The reactive element takes the last thing from inside the {}. I guess your second if returns NULL when a is selected.

这篇关于如果条件在闪亮的DT renderdatatable中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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