简体   繁体   English

闪亮的条件面板

[英]Shiny conditional panel

In my app I want the user to choose a folder, and the to choose a file from that folder. 在我的应用中,我希望用户选择一个文件夹,然后从中选择一个文件。

I thought to use conditionalPanel() so the user will see only the first button until he pick's the folder. 我以为可以使用conditionalPanel()以便用户在选择文件夹之前只能看到第一个按钮。 I wrote this code but I get this error message, 'object 'input' is not found', what would be the right way to do this? 我编写了这段代码,但收到了此错误消息,“未找到对象'输入'”,这样做的正确方法是什么? And is it a problem to put a conditional panel in an absolute panel? 将条件面板置于绝对面板中是否有问题?

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)

The probem arises from trying to dynamically create the choices for the file selection inside the ui part of your app. 探查来自尝试在应用程序的ui部分内部动态创建文件选择的选择。 The way you should do this is to create the dynamic part of the ui (Your file selection) in your server part using uiOutput and renderUI 您应该执行的方法是使用uiOutputrenderUIserver部分中创建ui的动态部分(您的文件选择)

The following code seems to do what you describe you want: 以下代码似乎可以满足您的描述:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM