【问题标题】:Dynamic input for HTML shiny ui (index.html)HTML 闪亮 ui (index.html) 的动态输入
【发布时间】:2015-08-10 15:40:22
【问题描述】:

我想用 html/css 从头开始​​定制我闪亮的应用程序。因此,我使用 index.html 而不是 ui.r,我遇到了以下问题:

我在我的 ui.r 中使用动态输入,例如:

selectInput("var","Date",choices = format(Sys.time(),'%Y-%m-%d'))

翻译为:

<div class="form-group shiny-input-container">
  <label class="control-label" for="var">Date</label>
  <div>
    <select id="var"><option value="2015-08-10" selected>2015-08-10</option></select>
    <script type="application/json" data-for="var" data-nonempty="">{}</script>
  </div>
</div> 

由于我必须使用 Sys.time() 而不是 html 代码中的实际日期,我很想知道是否可以在仅使用 .html 作为 ui 时调用 R/shiny?

【问题讨论】:

  • 您可以尝试在 html 文件中添加 uiOutput div,而不是使用 selectInput,并从应用程序的 server.R 部分添加 selectInputSys.time()。请参阅动态创建控件...部分here
  • 非常感谢您的建议。所以我可以在 server.r 中创建 html 输出,例如:output$date &lt;- renderUI({ selectInput("var","Date",choices = format(Sys.time(),'%Y-%m-%d')) }) 并将其与纯 html 包含到 index.html 中:&lt;pre id="date" class="shiny-html-output"&gt;&lt;/pre&gt; 使用 selectInput 这完全是多余的,但现在我可以为 html 部分创建自己的函数。

标签: html r shiny


【解决方案1】:

Shiny 对此有一个特殊的功能:updateSelectInput

在您的 ShinyUI 中留下 choices = NULLchoices = "Please Select Something." 之类的东西(通过 ui.R 或 index.html 生成无关紧要)。

然后在 server.R 中,调用 updateSelectInput 将您的 choices 属性更改为您在反应式上下文中想要的任何内容。

这里是一个 1 文件应用程序中的一些简单代码示例:

app <- shinyApp(

  ui = shinyUI(fluidPage(
    selectInput("inputID", label = "Reactive Select Input." , choices = "Please Select, Monsieur."), 
    actionButton("buttonID", label = "Refresh Select Options.")
  )),

  server = function(session, input, output) {

    observeEvent(input$buttonID, {
      updateSelectInput(session, "inputID", choices = as.character(Sys.time()))
    })

  }
)

享受吧!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-25
    • 2014-04-21
    • 2016-07-05
    • 1970-01-01
    • 2014-10-25
    • 2016-11-21
    • 2016-12-25
    相关资源
    最近更新 更多