2017-07-30 30 views
0

我试图从文本文件中为闪亮的仪表板中的用户显示帮助。但是我无法控制显示新行(“\ n”)。他们在文本文件和文本中,但闪亮不想显示它们。在仪表板中显示文件的内容Shiny

感谢您的帮助

回答

0

闪亮的转换所有元素为HTML,它不会使新行(\n)字符。为了创建换行符,可以使用p()函数将每行包装在一组HTML段落标签中。

这意味着,而是采用renderText()textOutput您需要在您的应用程序使用renderUIuiOutput

下面给出了如何将换行符转换为段落标签的完整示例。

require(stringi) 
require(shiny) 

# write text file with standard newline characters 
str <- 'These are words\nwith newline characters\n\nthat do not render.' 
write(x = str, file = 'Data.txt') 

ui <- fluidPage(
    h4('Reading raw text from file:'), 
    textOutput('textWithNewlines'), # text with newline characters output 

    h4('Converting text to list of paragraph tags'), 
    uiOutput('textWithHTML') # ui output as a list of HTML p() tags 
) 

server <- function(input,output){ 

    output$textWithNewlines <- renderText({ 
    rawText <- readLines('Data.txt') 
    return(rawText) 
    }) 

    ### SOLUTION ### 
    output$textWithHTML <- renderUI({ 
    rawText <- readLines('Data.txt') # get raw text 

    # split the text into a list of character vectors 
    # Each element in the list contains one line 
    splitText <- stringi::stri_split(str = rawText, regex = '\\n') 

    # wrap a paragraph tag around each element in the list 
    replacedText <- lapply(splitText, p) 

    return(replacedText) 
    }) 

} 
shinyApp(ui=ui, server=server) 
相关问题