条件RenderUI R闪亮

时间:2015-01-22 15:35:44

标签: r shiny

我遇到了渲染问题,我无法在任何地方找到解决方案。可能我会向谷歌提出错误的问题,而不仅仅是一个闪亮的问题是一个基本的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语句只显示两个中的一个? 在此先感谢您,如果您需要,我会在这里为您提供更多详细信息。

达尼

[编辑]

server.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")   
})

})

2 个答案:

答案 0 :(得分: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)))
            }
   )

 })
})

示例输出: With <code>cuttoffdose</code>= 0

With <code>cutoffdose</code>= 1

With <code>cutoffdose</code>= 1.5

答案 1 :(得分:0)

我会尝试使用函数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)
)}
相关问题