【问题标题】:Shiny side by side divs for fileInput and textInputfileInput 和 textInput 的闪亮并排 div
【发布时间】:2016-04-09 04:14:00
【问题描述】:

以下尝试将 Shiny fileInput()textInput() 并排放置。

一个简单的server.R 文件:

shinyServer(function(input, output) {} )

还有以下ui.R

# Custom function(s) to get file- and text-Input side by side
# Based on: <http://stackoverflow.com/a/21132918/1172302>

# options(shiny.error=browser)

# Globals

display.inline.block <- "display:inline-block"
class.input.small = "input-small"
FileInputId <- "SampleFile"
FileInputLabel <- "Sample"
TextInputId <- "SampleLabel"
TextInputLabel <- "Label"
TextInputLabelDefault <- "Sample Label"

# helper functions

fileInput.custom <- function (inputId, label, ...)
{
    tagList(tags$label(label, `for` = inputId),
            tags$input(id = inputId, type = "file", ...)
    )
}

textInput.custom <- function (inputId, label, value = "",...) 
{
    tagList(tags$label(label, `for` = inputId),
            tags$input(id = inputId, type = "text", value = value,...)
    )
}

filetextInput <- function (fileId, fileLabel, textId, textLabel, textValue, divstyle, ...)
{

    # sample file
    div(style = divstyle,
        fileInput.custom(inputId = fileId,
                      label = fileLabel,
                      class = class.input.small))

    # label for sample, to be used in plot(s)
    div(style = divstyle,
        textInput.custom(inputId = textId,
                      label = textLabel,
                      value = textValue,
                      class = class.input.small))

}

# Shiny UI

shinyUI(
  fluidPage(

    # sample input
    div(style = display.inline.block,
      fileInput.custom(inputId = FileInputId,
                label = FileInputLabel)
    ),

    # label for sample 
    div(style = display.inline.block,
      textInput.custom(inputId = TextInputId,
                label = TextInputLabel,
                value = TextInputLabelDefault)
    ),

    hr(),

    filetextInput(
        fileId = FileInputId,
        fileLabel = FileInputLabel,
        textId = TextInputId,
        textLabel = TextInputLabel,
        textValue = TextInputLabelDefault,
        divstyle = display.inline.block)

  )
)

以上结果:

如屏幕截图所示,它使用两个独立的divs 工作。为什么在filetextInput()函数的情况下不起作用?

【问题讨论】:

  • 将 fileTextInput 的正文包裹在 tagList 或 div 中。
  • 某种相关 -- 受 Python 的严重影响,我在oreilly.com/ideas/… 中读到以下内容:我相当肯定,我大部分时间都花在调试神秘问题上是因为这个一个简单的错误:忘记从函数返回值。

标签: r function shiny


【解决方案1】:

函数返回最后评估的值,因此在您的情况下,第一部分丢失了。例如。 :

function(){
"a"
"b"
}

返回“b”

所以你不想要那个。使用 div 或 tagList。

filetextInput <- function (fileId, fileLabel, textId, textLabel, textValue, divstyle, ...)
{
  div(
  # sample file
  div(style = divstyle,
      fileInput.custom(inputId = fileId,
                       label = fileLabel,
                       class = class.input.small)
      ),

  # label for sample, to be used in plot(s)
  div(style = divstyle,
      textInput.custom(inputId = textId,
                       label = textLabel,
                       value = textValue,
                       class = class.input.small)
      )
  )

}

【讨论】:

  • 并认为生成的 html 将在浏览器中进行评估:在 ui.R 之外使用 css 文件和/或引导类,例如“.form-inline”,例如www/my.css
  • 啊,另一个div。谢谢。 --ps 我总是想知道为什么人们回答问题而不给他们投票:-p
  • 我真的不知道什么时候应该投票赞成一个问题,抱歉。但我认为,一个具有挑战性、清晰且非平凡的问题值得一票。在这里,这是一个关于函数如何工作的问题,而不是什么大问题。但是,正如您提供的完整示例,这可能值得点击 ;)
  • 这确实是一个笼统的问题——我的错误,我应该在 meta 或其他地方问这个问题。还是谢谢。
猜你喜欢
  • 2016-09-22
  • 1970-01-01
  • 2017-09-10
  • 1970-01-01
  • 2013-06-25
  • 2017-11-08
  • 2015-11-16
  • 2021-06-15
  • 2015-03-17
相关资源
最近更新 更多