【问题标题】:Is there an R function in Shiny for if else statement如果 else 语句在 Shiny 中是否有 R 函数
【发布时间】:2019-10-18 08:40:02
【问题描述】:

我有 2 个条件让数组以闪亮的方式显示。我尝试在 R 中使用以下代码闪亮但不是输出

library(shiny)

ui <- fluidPage(
sidebarLayout(
sidebarPanel(selectInput("x","Value of x",choices = 
c("Array1","Array2"))),
  mainPanel(h6("Here it is"),
            textOutput("message")
  )
)
)

server <- function(input, output, session) {
output$message <- renderUI(
{
  if(input$x == "Array1")
  {
    renderTable(array(1:20, dim=c(4,5)))
  } else 
    {
    if(input$x == "Array2")
    {
    renderTable(array(1:25, dim=c(5,5)))
    }
  }
  }
 )
}

shinyApp(ui, server)

代码中是否存在问题。请指教

【问题讨论】:

    标签: r shiny


    【解决方案1】:
    library(shiny)
    
    ui <- fluidPage(
        sidebarLayout(
            sidebarPanel(selectInput("x","Value of x",choices = 
                                         c("Array1","Array2"))),
            mainPanel(h6("Here it is"),
                      tableOutput("message")
            )
        )
    )
    
    server <- function(input, output, session) {
        output$message <- renderTable(
            {
                if(input$x == "Array1")
                {
                       array(1:20, dim=c(4,5))
                } else 
                {
                    if(input$x == "Array2")
                    {
                       array(1:25, dim=c(5,5))
                    }
                }
            }
        )
    }
    
    shinyApp(ui, server)
    
    1. 您混淆了多种 UI 类型。例如 RenderUI 和 textOutput。
    2. 您在 renderUI 中使用了 renderTable。 RenderUI 需要tableOutput 才能正确显示。但我认为无论如何你都不需要它,因为你想要的只是显示表格。
    3. 在 R 中,使用 data.frames 或 matrix 而不是多维数组更为常见。

    【讨论】:

    • 非常感谢。但是我仍然不相信您对 renderUI 和 renderTable 的定义。我们不能将 RenderUI 用于 textOutput 吗?
    • 不行,你必须使用相应的类型才能正确显示输出。 textOutput 也仅适用于“这是一个数组”之类的文本,但不适用于显示多维数据。一般来说,我总是使用renderUI 和uiOutput 来支持textOutput,因为您可以使用带有uiOutput 功能的HTML 文本。如果我的回答有帮助,请采纳:-)
    猜你喜欢
    • 2018-02-28
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    • 1970-01-01
    • 1970-01-01
    • 2017-03-21
    • 1970-01-01
    相关资源
    最近更新 更多