[英]conditional RenderUI R shiny
我的 renderUI 有问题,我在任何地方都找不到解决方案。 可能我向 google 提出了错误的问题,而不仅仅是一个闪亮的问题,而是一个基本的 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 语句仅显示两者之一? 如果您需要,我会在这里为您提供更多详细信息。
[编辑]
服务器
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")
})
})
您在这里遇到了一些问题,该函数将返回不同的类,包括带有解释的错误和警告。 这是此函数可能发生的情况的独立示例,鼓励您在代码中包含 TryCatch:
用户界面
shinyUI(
pageWithSidebar(
headerPanel("drsmooth"), sidebarPanel(
numericInput("num", label = h3("Choose 'cutoffdose'"), value = 0)
),
mainPanel(
verbatimTextOutput('current')
)
)
)
服务器
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)))
}
)
})
})
示例输出:
我会尝试使用函数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)
)}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.