如何在R中以闪亮的方式向用户显示警告。用户的输入是正确的,但输出不适合显示。目的是提醒用户由于数据太多而仅显示子集数据。warning()
仅显示在控制台中。谢谢。
由于原文很长,这里有一个假代码来解释问题。renderTable 中有一个警告。它的目的是检查数据,如果数据很大,只会显示前几项。
ui.R
shinyUI(fluidPage(
titlePanel("Validation App"),
sidebarLayout(
sidebarPanel(
selectInput("data", label = "Data set",
choices = c("", "mtcars", "faithful", "iris"))
),
# Show a plot of the generated distribution
mainPanel(
tableOutput("table"),
plotOutput("plot")
)
)
))
Run Code Online (Sandbox Code Playgroud)
服务器R
shinyServer(function(input, output) {
data <- reactive({
validate(
need(input$data != "", "Please select a data set")
)
get(input$data, 'package:datasets')
})
output$plot <- renderPlot({
hist(data()[, 1], col = 'forestgreen', border = 'white')
})
output$table <- renderTable({
warning("Warning message.")
head(data())
})
})
Run Code Online (Sandbox Code Playgroud)
我为此投入了更多的工作,并使警告面板成为有条件的。
然而,只有当我在每一页上都包含时,它才有效textOutput("warnstat")
。我认为是因为除非我这样做,否则它不会设置 javascript 变量output.warnstat
。
您可以在用户界面中构建一个警告面板,并进行相应的设置。这是一个简单的示例,但它可能比逐字打印语句更复杂。
shinyUI(fluidPage(
titlePanel("Validation App"),
sidebarLayout(
sidebarPanel(
selectInput("data", label = "Data set",
choices = c("", "mtcars", "faithful", "iris"))
),
# Show a plot of the generated distribution
mainPanel(
conditionalPanel(condition = "output.warnstat == 'Error'",
verbatimTextOutput("warnmsg")),
tableOutput("table"),
plotOutput("plot")
)
)
))
Run Code Online (Sandbox Code Playgroud)
shinyServer(function(input, output) {
errstat <- reactive({
ifelse (input$data=="mtcars",T,F)
})
data <- reactive({
validate(
need(input$data != "", "Please select a data set")
)
get(input$data, 'package:datasets')
})
output$plot <- renderPlot({
hist(data()[, 1], col = 'forestgreen', border = 'white')
})
output$table <- renderTable({
warning("Warning message.")
head(data())
})
output$warnmsg <- renderPrint({
if (errstat()){
print("Warning message - blah blah blah")
print(input$data)
head(data())
} else {
print("No error")
}
})
output$warnstat <- renderText({ifelse(errstat(),"Error","No error") })
outputOptions(output, "warnstat", suspendWhenHidden=FALSE)
})
Run Code Online (Sandbox Code Playgroud)