我想在Shiny的侧边栏面板之外显示静态文本。我能够在侧边栏面板中显示文本。但是,如果我尝试在侧边栏面板之外显示文本,则会出现以下错误:“ match.arg中的错误:'arg'必须为NULL或字符向量”。
下面的示例代码在侧边栏面板中显示句子“ This is a static text”。我想在侧边栏面板“正下方”显示文本,但不在面板窗口内部显示。
下面的代码给了我这个输出:
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30),
h5("This is a static text")
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$distPlot <- renderPlot({
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
}
# Run the application
shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)
该sidebarPanel函数会将所有内容放入formwith class中well。一种巧妙的解决方案(也许有更好的解决方案)是创建一个自定义函数siderbarPanel来将元素放在form. 下面是您的代码,其函数sidebarPanel2只是原始函数的自定义,用于将元素放置在“正下方”。您可以输入任何内容,而不仅仅是文本。
library(shiny)
sidebarPanel2 <- function (..., out = NULL, width = 4)
{
div(class = paste0("col-sm-", width),
tags$form(class = "well", ...),
out
)
}
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel2(fluid = FALSE,
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30),
out = h5("This is a static text")
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$distPlot <- renderPlot({
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
}
# Run the application
shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)