我希望通过使用上传的文件名更新自己,使我的Shiny程序的标题对文件输入有反应。这就是我所拥有的:
ui.R:
shinyUI(pageWithSidebar(
headerPanel("File"),
sidebarPanel(
fileInput("file1", "Upload a file:",
accept = c('.csv','text/csv','text/comma-separated-values,text/plain'),
multiple = F) ) ))
server.R:
shinyServer(function(input, output, session) {
in_data <- reactive({
inFile <- input$file1
if (is.null(inFile)) return(NULL)
read.csv(inFile$datapath, as.is=T)
}) })
基本上,我想更新headerPanel,以便在用户上传文件后显示“File [name]”。我在服务器中访问ui.R卡住了。我试着在server.R中调用in_data(),但是找不到它。有什么想法吗?
答案 0 :(得分:2)
诀窍是使用uiOutput
headerPanel
,例如,
ui <- pageWithSidebar(
uiOutput("header_panel"),
sidebarPanel(
fileInput("file1", "Upload a file:",
accept = c('.csv','text/csv','text/comma-separated-values,text/plain'),
multiple = F
)
),
mainPanel()
)
server <- function(input, output, session) {
in_data <- reactive({
inFile <- input$file1
if (is.null(inFile)) return(NULL)
read.csv(inFile$datapath, as.is=T)
})
output$header_panel <- renderUI({
inFile <- input$file1
if (is.null(inFile)) headerPanel('File')
else headerPanel(paste('File', inFile$name))
})
}
shinyApp(ui=ui, server=server)