0
ユーザーのためにヘルプをテキストファイルから光沢のあるダッシュボードに表示しようとしています。しかし、私は新しい行( "\ n")の表示を制御することはできません。彼らはテキストファイルとテキストにありますが、光沢はそれらを表示したくありません。ヘルプダッシュボードにファイルの内容を表示するShiny
ユーザーのためにヘルプをテキストファイルから光沢のあるダッシュボードに表示しようとしています。しかし、私は新しい行( "\ n")の表示を制御することはできません。彼らはテキストファイルとテキストにありますが、光沢はそれらを表示したくありません。ヘルプダッシュボードにファイルの内容を表示するShiny
シャイニーため
おかげで改行(\n
)文字をレンダリングしないHTMLにすべての要素を変換します。改行を作成するには、p()
関数を使用してHTML段落タグのセットに各行を折り返します。
つまり、renderText()
とtextOutput
の代わりに、renderUI
とuiOutput
をアプリで使用する必要があります。
改行文字を段落タグに変換する方法の完全な例を以下に示します。
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)