【问题标题】:conditional RenderUI R shiny有条件的 RenderUI R 闪亮
【发布时间】:2019-11-21 04:09:49
【问题描述】:

我遇到了 renderUI 的问题,在任何地方都找不到解决方案。可能我向谷歌提出了错误的问题,而不仅仅是一个闪亮的问题是一个基本的 R 问题。

我在 R 中有一个函数,根据输入将返回表格或文本。所以我以这种方式在我的 server.R 中创建了这两个选项:

output$table <- renderTable {(
   x <- function (y)
   print(x)
)}
output$text <- renderText {(
   x <- function (y)
   print(x)
)}

如果我将两个输出都放在 renderUI 中,一个总是会给我一个错误。如果输出是表格,则在 textOutput 的情况下:

 Error: argument 1 (type 'list') cannot be handled by 'cat'

 Error:no applicable method for 'xtable' applied to an object of class "character"

反之亦然。

我的问题是有没有办法捕捉这个错误并在 renderUI 中使用 if 语句来仅显示两者之一? 如果您需要,我在这里为您提供更多详细信息。

[编辑]

服务器.R

 library(shiny)
 library(drsmooth)

 shinyServer(function(input, output,session) {

-- upload dataframe and input management goes here --

 output$nlbcd <- renderTable({
    nlbcd<-nlbcd(dosecolumn="Dose", targetcolumn=response(),cutoffdose=cutoff(),data=data1())
    print(nlbcd)
 })

 output$nlbcdText <- renderText({
    nlbcd<-nlbcd(dosecolumn="Dose", targetcolumn=response(),cutoffdose=cutoff(),data=data1())
   print(nlbcd)
 }) 

 output$tb <- renderUI({
 tableOutput("nlbcd"),
 textOutput("nlbcdText")   
})

})

【问题讨论】:

  • 我确实认为这是一个很好的问题,但你能更具体地谈谈你的情况吗?为什么要输出列表(而不是表格——您也应该解决这个问题)或文本字符串之一?发布您的 ui.Rserver.R 代码的最简单的可重现示例也会非常有帮助。
  • 好的,我正在使用一个名为 drsmooth 的包。在这个包中有一个函数'nlbcd',它根据传递给它的参数之一返回一个可由renderTable显示的列表(对不起,术语错误)或可由renderText显示的字符串。

标签: r shiny


【解决方案1】:

这里有一些问题,该函数将返回不同的类,包括带有解释的错误和警告。以下是使用此函数可能发生的情况的独立示例,建议您在代码中包含 TryCatch:

ui.R

shinyUI(
  pageWithSidebar(   
    headerPanel("drsmooth"),   sidebarPanel(
      numericInput("num", label = h3("Choose 'cutoffdose'"), value = 0)
    ),
    mainPanel(
      verbatimTextOutput('current')
    ) 
  )
)

server.R

library(drsmooth)

shinyServer(function(input, output, session) {
 output$current <- renderPrint({
   dose <- input$num

   tryCatch(isolate(nlbcd("dose", "MF_Log", cutoffdose=dose, data=DRdata)), 
            error=function(e) {
              cat(isolate(conditionMessage(e)))
            }
   )

 })
})

示例输出:

【讨论】:

  • 非常有用,谢谢@mkemp6。但我想拥有一个闪亮的应用程序的全部意义在于让它“闪亮”,而逐字文本是可以做的不那么闪亮的事情。这很有帮助,因为我学会了如何使用 trycatch(),但仍然不是我想要的闪亮应用程序。
【解决方案2】:

我会尝试使用函数class()

output$table <- renderTable {(
   x <- function (y)
   if(class(x) == "table")
     print(x)

)}
output$text <- renderText {(
   x <- function (y)
   if(class(x) == "list")
     print(x)
)}

【讨论】:

  • 我明白你的意思,我认为我们的方向是正确的,但我得到了:the condition has length &gt; 1 and only the first element will be used。我必须错过一些东西,但非常感谢@Mikael。我会添加任何进一步的开发。
  • @DanieleAvancini 看看我下面的解决方案,它将处理错误、警告和实际输出
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-02
  • 2018-03-23
  • 1970-01-01
  • 2017-06-29
  • 2018-02-03
  • 2015-08-03
  • 1970-01-01
相关资源
最近更新 更多