0

我正在尝试使用自动滚动条来渲染文本输出,当文本变得太宽或太长时会激活该滚动条。目前,我x-axis在 UI 的 Textoutput 中实现了 with container=pre 上的滚动条作为参数。

我想要的是文本输出中的输出将自身限制为 4 或 5 行,然后有一个滚动条以查看剩余的行。

我查看了所有可以找到的关于该主题的帖子(这就是我实现 container=pre 的原因),但我找不到解决 y 轴滚动条的方法。我知道这与标签设置中的溢出 y: "auto" 有关,但我无法解决,也许我放错了。谢谢你。这是一个例子:

# Shiny example
library(shinydashboard)
library(shiny)
library(stringi)
library(shinyWidgets)

# Data 
  # Some random letters
names<- stringi::stri_rand_strings(100,20)
  # Some random numbers
numbers<- runif(100,0,100000)
  # a df 
df<- as.data.frame(cbind(names, numbers))

shinyApp(
ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    absolutePanel(id="panel", fixed = FALSE,
                  draggable = F, top = 80, left = "auto", right = 60, bottom = "auto",
                  width = 290, height = 370,
                  box( title = "Box example",
                       status = "warning", width = 230, solidHeader = T,
                       pickerInput(
                         inputId = "select_nb_names", 
                         choices = names,
                         multiple = TRUE, 
                         selected = NULL, 
                         width = 190,inline = FALSE),
                       # the textoutput that only has an x-axis scrollbar
textOutput("TextThatIWantToHaveAScroll",container = pre ))))),


server <-  function(input, output, session) {
  output$TextThatIWantToHaveAScroll<- renderText(
    paste0( input$select_nb_names," : ",df$numbers[df$names%in%input$select_nb_names],"\n"))


}

# Run the application 
)


4

1 回答 1

0

您可以使用 CSS 添加滚动条。在 Shiny 中,使用tags$style标签来定义 css 属性并包装在tags$head标签中。您可以使用输出元素的ID(即,#TextThatIWantToHaveAScroll)、文本输出的闪亮类(即,shiny-text-output)或标签名称(即,pre)来定位元素。如果您有多个元素应该接受相同的处理,那么使用.shiny-text-output是更好的选择。

要为所需元素创建滚动条(如示例中所示;使用 ID),请先设置 height 和 width 属性,然后使用overflow: scroll. 例如:

#TextThatIWantToHaveAScroll {
   width: 100%;
   height: 60px;
   overflow: scroll;
}

根据需要调整高度和宽度。还有其他可用的滚动选项。请参阅 Mozilla 关于溢出属性的 CSS 指南。这是完整的示例:

# Shiny example 
library(shinydashboard)
library(shiny)
library(stringi)
library(shinyWidgets)

# Data 
# Some random letters
names<- stringi::stri_rand_strings(100,20)
# Some random numbers
numbers<- runif(100,0,100000)
# a df 
df<- as.data.frame(cbind(names, numbers))

shinyApp(
    ui <- dashboardPage(
        dashboardHeader(),
        dashboardSidebar(),
        dashboardBody(
            tags$head(
                tags$style(
                    "#TextThatIWantToHaveAScroll {
                        width: 100%;
                        height: 60px;
                        overflow: scroll;
                    }"
                ),
            ),
            absolutePanel(id="panel", fixed = FALSE,
                          draggable = F, top = 80, left = "auto", right = 60, bottom = "auto",
                          width = 290, height = 370,
                          box( title = "Box example",
                               status = "warning", width = 230, solidHeader = T,
                               pickerInput(
                                   inputId = "select_nb_names", 
                                   choices = names,
                                   multiple = TRUE, 
                                   selected = NULL, 
                                   width = 190,inline = FALSE),
                               # the textoutput that only has an x-axis scrollbar
                               textOutput("TextThatIWantToHaveAScroll",container = pre))))),


    server <-  function(input, output, session) {
        output$TextThatIWantToHaveAScroll<- renderText(
            paste0( input$select_nb_names," : ",df$numbers[df$names%in%input$select_nb_names],"\n"))


    }

    # Run the application 
)
于 2020-04-03T09:14:17.360 回答