[英]R Shiny - Display static text outside sidebar panel
我想在Shiny的侧边栏面板外面显示静态文本。 我可以在侧边栏面板中显示文本。 但是,如果我尝试在侧边栏面板之外显示文本,那么我会收到此错误:“match.arg中的错误:'arg'必须为NULL或字符向量”。
下面是一个示例代码,它在侧边栏面板中显示句子“This is an 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)
sidebarPanel
函数会将所有内容放在一个类well
的form
。 一个hacky解决方案(可能有一个更好的解决方案)是创建一个自定义函数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)
一种替代方法:根本不能使用sidebarLayout()
,只需定义自己的shiny-input-container
样式(有关调整滑块组件样式的更多示例,请参阅此答案 )。 然后,您可以使用fluidRow()
和column()
嵌套( 示例 )对输入窗口小部件,文本和绘图进行更复杂的排列。
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
#CSS Styles
tags$style(HTML(
paste0(".shiny-input-container {background-color: #f5f5f5; border: 1px solid #e3e3e3;",
"padding-left: 10px; padding-right: 10px; border-radius: 3px;}")
)),
# Application title
titlePanel("Old Faithful Geyser Data"),
#use column to mimic sidebarPanel
column(
width = 4,
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30),
hr(),
h5("This is a static text")
),
#use column to mimic main panel
column(
width = 8,
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)
由reprex包创建于2018-09-28(v0.2.1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.