タイプ "ファイル"の入力はブラウザによって制御され、定義されているため、外観を変更するためにシャイニー側から行うことはできませんが、非表示にすることはできません。
ファイル入力はラベルにも反応するため、ラベルの外観を自由に変更できます。
最初の手順は、fileinput
機能の独自のバージョンを作成することです。この新しい関数は、CSSを使用してtags$input
を隠し、ラベルをカスタマイズし、オプションでプログレスバーを削除します。
以下は、2つのファイル入力がカスタムテキストとアイコンのボタンとして表示され、進行状況バーがある場合とない場合のコードです。完璧に動作
library(shiny)
# based on the Shiny fileInput function
fileInput2 <- function(inputId, label = NULL, labelIcon = NULL, multiple = FALSE,
accept = NULL, width = NULL, progress = TRUE, ...) {
# add class fileinput_2 defined in UI to hide the inputTag
inputTag <- tags$input(id = inputId, name = inputId, type = "file",
class = "fileinput_2")
if (multiple)
inputTag$attribs$multiple <- "multiple"
if (length(accept) > 0)
inputTag$attribs$accept <- paste(accept, collapse = ",")
div(..., style = if (!is.null(width)) paste0("width: ", validateCssUnit(width), ";"),
inputTag,
# label customized with an action button
tags$label(`for` = inputId, div(icon(labelIcon), label,
class = "btn btn-default action-button")),
# optionally display a progress bar
if(progress)
tags$div(id = paste(inputId, "_progress", sep = ""),
class = "progress shiny-file-input-progress",
tags$div(class = "progress-bar")
)
)
}
ui <- fluidPage(
# define class fileinput_2 to hide inputTag in fileInput2
tags$head(tags$style(HTML(
".fileinput_2 {
width: 0.1px;
height: 0.1px;
opacity: 0;
overflow: hidden;
position: absolute;
z-index: -1;
}"
))),
sidebarLayout(
sidebarPanel(
h3("Without progress bar"),
fileInput2("file1", "Load File 1", labelIcon = "folder-open-o",
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv"), progress = FALSE),
h3("With progress bar"),
fileInput2("file2", "Load File 2", labelIcon = "file-code-o",
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv"), progress = TRUE)
),
mainPanel(
verbatimTextOutput("content1"),
verbatimTextOutput("content2")
)
)
)
server <- function(input, output) {
output$content1 <- renderPrint({
inFile <- input$file1
if (is.null(inFile))
return(NULL)
paste("File name 1:", inFile$name)
})
output$content2 <- renderPrint({
inFile <- input$file2
if (is.null(inFile))
return(NULL)
paste("File name 2:", inFile$name)
})
}
shinyApp(ui, server)
おかげで、!もう1つの最後の質問は、もともと私はここにあなたのソリューションを使用して2つのアクションボタンを持っていた:http://stackoverflow.com/questions/40537917/how-to-place-buttons-in-shiny/40538274#40538274私はまだ訴えている私はこの質問のボタンを他の質問のボタンの隣に置いておきたいと思っています。それは簡単に実行可能ですか? – RustyStatistician
実際に私は周りの仕事を考え出したので、心配しないでください。 – RustyStatistician