我需要将闪亮的输入对象转换为纯 HTML。
library(shiny)
test <- selectInput("testInput", label = "",
choices = c("test1", "test2", "test3"), selected = "test1")
Run Code Online (Sandbox Code Playgroud)
在这个例子中,test是一个 Shiny.tag 对象。
class(test)
#[1] "shiny.tag"
Run Code Online (Sandbox Code Playgroud)
我希望它是一个 html 对象,就好像我是从文本中解析出来的一样:
test2 <- HTML('<div class="form-group shiny-input-container">
<label class="control-label" for="testInput"></label>
<div>
<select id="testInput"><option value="test1" selected>test1</option>
<option value="test2">test2</option>
<option value="test3">test3</option></select>
<script type="application/json" data-for="testInput" data-nonempty="">{}</script>
</div>
</div>')
class(test2)
#[1] "html" "character"
Run Code Online (Sandbox Code Playgroud)
我试着像这样直接这样做:
HTML(test)
#Error in FUN(X[[i]], ...) : argument is not a character vector
Run Code Online (Sandbox Code Playgroud)
但是它需要一个字符向量,并且似乎没有将shiny.tag 对象转换为HTML 的方法。
我认为将闪亮的输入对象转换为 html 应该很容易,但不确定如何实现。
先谢谢了!
您应该使用doRenderTags或renderTags来自htmltools:
library(shiny)
test <- selectInput(
inputId = "testInput", label = "",
choices = c("test1", "test2", "test3"), selected = "test1"
)
(html <- htmltools::doRenderTags(test))
# <div class="form-group shiny-input-container">
# <label class="control-label" for="testInput"></label>
# <div>
# <select id="testInput"><option value="test1" selected>test1</option>
# <option value="test2">test2</option>
# <option value="test3">test3</option></select>
# <script type="application/json" data-for="testInput" data-nonempty="">{}</script>
# </div>
# </div>
class(html)
# [1] "html" "character"
Run Code Online (Sandbox Code Playgroud)
但renderTags更好,因为它解决了依赖关系(在你的情况下selectize.js):
html2 <- htmltools::renderTags(test)
class(html2)
# [1] "list"
str(html2, max.level = 2)
# List of 4
# $ head :Classes 'html', 'character' atomic [1:1]
# .. ..- attr(*, "html")= logi TRUE
# $ singletons : chr(0)
# $ dependencies:List of 1
# ..$ :List of 10
# .. ..- attr(*, "class")= chr "html_dependency"
# $ html :Classes 'html', 'character' atomic [1:1] <div class="form-group shiny-input-container">
# <label class="control-label" for="testInput"></label>
# <div>
# | __truncated__
# .. ..- attr(*, "html")= logi TRUE
Run Code Online (Sandbox Code Playgroud)
您可以使用html列表中的插槽访问原始 HTML 。