Shiny side by side divs for fileInput and textInput

The following attempts to put Shiny fileInput() and textInput() side by side.

Simple server.R file:

 shinyServer(function(input, output) {} ) 

And the following 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) ) ) 

The above results:

Trying to put fileInput and textInput side by side

As shown in the screenshot, it works using two separate div s. Why does this not work with the filetextInput() function?

+1
function r shiny
Jan 05 '16 at 10:54 on
source share
1 answer

Functions return the last evaluated value, so in your case the first part will be lost. For example.

 function(){ "a" "b" } 

returns "b"

so you don’t want to. Use div or 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) ) ) } 
+2
Jan 05 '16 at 11:15
source share



All Articles