下面是一个上传rda或csv文件的闪亮应用。我只需要在selectinput中选择csv作为文件类型时才显示checkboxinput和radiobutton小部件。选择rda时,不要显示这些小部件。
library(shiny)
library(DT)
##---------------------------------------------------------------
## ui
##---------------------------------------------------------------
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
# select a file type
selectInput('filetype', label = h5(strong('Please select a file type')),
choices = c('rda', 'csv'),
selected = 'rda'),
# Input: Select a file ----
fileInput("file1", "Choose a file",
multiple = TRUE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv")),
tags$hr(),
# Input: Checkbox if file has header ----
checkboxInput("header", "Header", TRUE),
# Input: Select separator ----
radioButtons("sep", "Separator",
choices = c(Comma = ",",
Semicolon = ";",
Tab = "\t"),
selected = ","),
# Input: Select quotes ----
radioButtons("quote", "Quote",
choices = c(None = "",
"Double Quote" = '"',
"Single Quote" = "'"),
selected = '"')
),
#------------------------------Main Panel--------------------
mainPanel(
DT::dataTableOutput("data.table")
)
)
)
##---------------------------------------------------------------------
# server
##---------------------------------------------------------------------
options(shiny.maxRequestSize=30*1024^2)
server <- function(input, output, session) {
dt <- reactive ({
if (input$filetype %in% 'rda') {
load (input$file1$datapath)
df
} else {
read.csv(input$file1$datapath,
header = input$header,
sep = input$sep,
quote = input$quote)
}
})
output$data.table <- DT::renderDataTable({
req(input$file1)
DT::datatable(dt(),
options = list(orderClasses = TRUE,
lengthMenu = c(5, 10, 20), pageLength = 5))
})
}
runApp(shinyApp(ui=ui, server=server))
真的很感激,如果有人可以帮助我。我不知道如何实现这一目标。
答案 0 :(得分:2)
以下是如何构建动态用户界面的示例,如果所选的文件类型为“csv&#39;”,则只显示单选按钮。有关详情,请参阅https://shiny.rstudio.com/articles/dynamic-ui.html。
library(shiny)
ui <- fluidPage(
selectInput(
"select",
label = "File Type",
choices = list("csv", "rda"),
selected = c("csv")
),
conditionalPanel(
condition = "input.select == 'csv'",
radioButtons(
"radio",
label = "Separator",
choices = list("commas", "tabs", "spaces")
)
)
)
server <- function(input, output, session) {
}
shinyApp(ui, server)