闪亮的条件面板

时间:2016-12-14 13:50:04

标签: r shiny

在我的应用程序中,我希望用户选择一个文件夹,然后从该文件夹中选择一个文件。

我想使用conditionalPanel(),因此用户只会看到第一个按钮,直到他选择该文件夹。我写了这段代码,但是我收到了这条错误消息,' object' input'找不到',这样做的正确方法是什么?将条件面板放在绝对面板中是一个问题吗?

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)

1 个答案:

答案 0 :(得分:2)

探测来自于尝试在应用的ui部分内动态创建文件选择的选项。您应该这样做的方法是使用uiserver

uiOutput部分中创建renderUI(您的文件选择)的动态部分

以下代码似乎可以按照您的描述进行操作:

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)