我想基于htmltemplate创建一个Shiny应用程序。当我创建静态SELECT / OPTION html时,服务器脚本中可以使用OPTION中的选定值。在UI中显示它,但是当我使用R代码生成SELECT / OPTION html时,我无法在UI中呈现所选的值。
有没有办法生成动态SELECT / OPTION列表并使用R代码中的选定值?
html模板:
<!doctype html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script type="application/shiny-singletons"></script>
<script type="application/html-dependencies">json2[2014.02.04];jquery[1.12.4];shiny[1.0.5];htmlwidgets[1.0];plotly-binding[4.7.1];bootstrap[3.3.7]</script>
<script src="shared/json2-min.js"></script>
<script src="shared/jquery.min.js"></script>
<link href="shared/shiny.css" rel="stylesheet" />
<script src="shared/shiny.min.js"></script>
<script src="htmlwidgets-1.0/htmlwidgets.js"></script>
<script src="plotly-binding-4.7.1/plotly.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="shared/bootstrap/css/bootstrap.min.css" rel="stylesheet" />
<script src="shared/bootstrap/js/bootstrap.min.js"></script>
<script src="shared/bootstrap/shim/html5shiv.min.js"></script>
<script src="shared/bootstrap/shim/respond.min.js"></script>
</head>
<body>
<!-- FIRST SELECT -->
<select name="car1">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
<!-- SELECTED VALUE WILL BE DISPLAYED HERE - WORKS FINE -->
<div class="shiny-html-output" id="w1"></div>
<!-- SECOND SELECT -->
<select class="shiny-html-output" name="car2" id="i"></select>
<!-- SELECTED VALUE WILL BE DISPLAYED HERE - DOES NOT WORK -->
<div class="shiny-html-output" id="w2"></div>
</body>
</html>
R代码:
library(shiny)
library(plotly)
library(htmlwidgets)
shinyApp(
ui <- htmlTemplate("template.html"),
server <- function(input, output) {
# INSERT SELECTED VALUE BELOW FIRST SELECT
output$w1 <- renderText(input$car1)
# GENERATE OPTIONS
html <- ""
for(r in 1:nrow(mtcars)){
html <- paste(html, "<option value='",mtcars[r, "mpg"],"'>",mtcars[r,"mpg"],"</option>", sep="")
}
# INJECT INTO TEMPLATE
output$i <- renderText(html)
# INSERT SELECTED VALUE BELOW SECOND SELECT (DOES NOT WORK)
output$w2 <- renderText(input$car2)
}
)
答案 0 :(得分:1)
事实证明,一个html标签无法输入和输出Shiny。当应用程序启动时,Shiny服务器会添加两个css类:
似乎造成了一些冲突。
解决方案是使用附加容器,因此Shiny输入和输出不在相同的html标签中。