以下尝试将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)
)
)
以上结果如下:
如屏幕截图所示,它使用两个单独的div
。为什么在filetextInput()
函数的情况下它不起作用?
答案 0 :(得分:2)
函数返回最后一个评估值,因此在您的情况下,第一部分将丢失。例如。 :
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)
)
)
}