[英]Shiny conditional panel
在我的應用中,我希望用戶選擇一個文件夾,然后從中選擇一個文件。
我以為可以使用conditionalPanel()
以便用戶在選擇文件夾之前只能看到第一個按鈕。 我編寫了這段代碼,但收到了此錯誤消息,“未找到對象'輸入'”,這樣做的正確方法是什么? 將條件面板置於絕對面板中是否有問題?
library(shiny)
library(ggplot2)
ui <- shinyUI(fluidPage(
titlePanel(""),
fluidRow(
# select input for selecting a folder
column(2, absolutePanel(fixed = TRUE, width = '180px',
selectInput("pick_folder", label = '', selected='choose_a_folder',
choices = setNames(as.list(c('choose_a_folder', basename(setdiff(list.dirs(recursive = FALSE),'.')))),
c('choose_a_folder', basename(setdiff(list.dirs(recursive = FALSE),'.'))))))),
# select input for selecting a file absolutePanel then conditionalPanel
column(2, absolutePanel(fixed = TRUE, width = '180px',
conditionalPanel(condition="input.pick_folder==choose_a_folder",
selectInput('pick_file', label = '', selected = 'choose_a_file',
choices = setNames(as.list(c('choose_a_file', basename(setdiff(list.files(path=input$pick_folder ,recursive = FALSE),'.')))),
c('choose a file', basename(setdiff(list.files(path=input$pick_folder ,recursive = FALSE),'.')))))))),
),
fluidRow(
#plot
plotOutput('my_plot')
)))
# server
server <- shinyServer(function(input, output) {
output$my_plot <- renderPlot({
dat <- read.table(file=paste(input$pick_folder, input$pick_file, sep='/'))
# some plots over dat
})
})
shinyApp(ui, server)
探查來自嘗試在應用程序的ui
部分內部動態創建文件選擇的選擇。 您應該執行的方法是使用uiOutput
和renderUI
在server
部分中創建ui
的動態部分(您的文件選擇)
以下代碼似乎可以滿足您的描述:
library(shiny)
library(ggplot2)
ui <- shinyUI(fluidPage(
titlePanel(""),
fluidRow(
# select input for selecting a folder
column(2, absolutePanel(fixed = TRUE, width = '180px',
selectInput("pick_folder", label = '', selected='choose_a_folder',
choices = setNames(as.list(c('choose_a_folder', basename(setdiff(list.dirs(recursive = FALSE),'.')))),
c('choose_a_folder', basename(setdiff(list.dirs(recursive = FALSE),'.'))))))),
# select input for selecting a file absolutePanel then conditionalPanel
column(2, absolutePanel(fixed = TRUE, width = '180px',
conditionalPanel(condition="input.pick_folder==choose_a_folder",
# Insert a dynamic bit of UI
uiOutput("fileselection")
)
)
)
),
fluidRow(
#plot
plotOutput('my_plot')
)))
# server
server <- shinyServer(function(input, output) {
output$my_plot <- renderPlot({
dat <- read.table(file=paste(input$pick_folder, input$pick_file, sep='/'))
# some plots over dat
})
output$fileselection <- renderUI({ #Define the dynamic UI
selectInput('pick_file', label = '', selected = 'choose_a_file',
choices = setNames(as.list(c('choose_a_file', basename(setdiff(list.files(path=input$pick_folder ,recursive = FALSE),'.')))),
c('choose a file', basename(setdiff(list.files(path=input$pick_folder ,recursive = FALSE),'.'))
)
)
)
})
})
shinyApp(ui, server)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.