单选按钮在闪亮-0.12.1 / mime-0.3中为我打破。以下代码适用于旧版本shiny-0.11.1 / mime-0.2但不适用于较新版本(输入$ dist和输入$ n返回空字符串)。下面的示例基于http://shiny.rstudio.com/articles/html-ui.html,但选择菜单被修改为单选按钮。
的index.html
<html>
<head>
<script src="shared/jquery.js" type="text/javascript"></script>
<script src="shared/shiny.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="shared/shiny.css"/>
</head>
<body>
<h1>HTML UI</h1>
<p>
<label>Distribution type:</label><br />
<input type="radio" name="dist" value="norm" checked="checked" />Normal<br>
<input type="radio" name="dist" value="unif" />Uniform<br>
<input type="radio" name="dist" value="lnor" />Log-normal<br>
<input type="radio" name="dist" value="exp" />Exponential<br>
</p>
<p>
<label>Number of observations:</label><br />
<input type="number" name="n" value="500" min="1" max="1000" />
</p>
<pre id="summary" class="shiny-text-output"></pre>
<div id="plot" class="shiny-plot-output"
style="width: 100%; height: 400px"></div>
<div id="table" class="shiny-html-output"></div>
</body>
</html>
server.R
library(shiny)
# Define server logic for random distribution application
shinyServer(function(input, output) {
# Reactive expression to generate the requested distribution. This is
# called whenever the inputs change. The output renderers defined
# below then all used the value computed from this expression
data <- reactive({
dist <- switch(input$dist,
norm = rnorm,
unif = runif,
lnorm = rlnorm,
exp = rexp,
rnorm)
dist(input$n)
})
# Generate a plot of the data. Also uses the inputs to build the
# plot label. Note that the dependencies on both the inputs and
# the data reactive expression are both tracked, and all expressions
# are called in the sequence implied by the dependency graph
output$plot <- renderPlot({
dist <- input$dist
n <- input$n
hist(data(),
main=paste('r', dist, '(', n, ')', sep=''))
})
# Generate a summary of the data
output$summary <- renderPrint({
summary(data())
})
# Generate an HTML table view of the data
output$table <- renderTable({
data.frame(x=data())
})
})
答案 0 :(得分:1)
只需添加一个div
<div id="dist" class="form-group shiny-input-radiogroup shiny-input-container">
<label>Distribution type:</label><br />
<input type="radio" name="dist" value="norm" checked="checked" />Normal<br>
<input type="radio" name="dist" value="unif" />Uniform<br>
<input type="radio" name="dist" value="lnor" />Log-normal<br>
<input type="radio" name="dist" value="exp" />Exponential<br>
</div>