闪亮的用户界面:从长长的csv文件列表中选择输入的名称 - 如何显示?

时间:2017-09-27 07:59:19

标签: r shiny shiny-server shinydashboard

我需要显示select输入选项,这些选项是从可能更改的csv文件列表中呈现的。

在第一步,用户通过单选按钮决定他想要检查的详细视图。在这里,我只是通过添加所选选项的路径来指向正确的文件夹。 在第二步中,用户应该能够选择"水果"详细视图。因此,我使用gsub过滤了一个巨大的csv文件列表,这很好用,但名称没有显示在选择输入字段中,而且我只是收到一条错误消息。

为了使它更简单,代码:

ui.R

      tabItem(tabName = "mainTab",

    # control boxes        

    fluidRow(
      box(
        title = "Please select a fruit representation", 
        width = 12, 
        status = "info",
        color = "maroon",
        collapsible = FALSE,
        radioButtons(
          inputId = "fruit_view", 
          label = "", 
          choices = c("Top Fruit View", 
            "Current Fruit Split",
            "Top Fruit & Value Class"), 
          selected = "Top Fruit View",
          inline = TRUE)
      )
      ),
    fluidRow(

      box(
        title = "Selected Fruit:", 
        width = 4,
        collapsible = FALSE,
        selectInput(
        inputId = "fruit_selection", 
        label = "", 
        choices = "")

server.R

get_fruitView <- reactive({
  switch(input$fruit_view,
    "Top Fruit View" = dir(
      path = "data/fruits/", full.names = TRUE),
    "Current Fruit Split" = dir(
      path = "data/splits/", full.names = TRUE),
    "Top Fruit & Value Class" = dir
    (path = "data/classes/", full.names = TRUE))
})


# loading the modeled data by fruit

      visible_fruits <- reactive({

      fruit_choiced <- get(input$fruit_view)

      fruit_choice <- view_choiced()[c(gsub(view_choiced(),
        pattern = "[^_]+_[^_]+_([^_]+)_[^_]+", replacement = "\\1"))]

      basename(unique(fruit_choice))
    })

    observe({
      updateSelectInput(session, "",
        choices = visible_fruits())
    })

正则表达式工作正常并隔离了我想要显示的名称,但它们在用户输入中不可见。

提前致谢。

顺便说一下,我收到此错误消息:

Warning: Error in serverFuncSource: server.R returned an object of unexpected type: environment
Stack trace (innermost first):
    1: shiny::runApp
Error in serverFuncSource() : 
  server.R returned an object of unexpected type: environment
Warning: Error in get: object 'input' not found
Stack trace (innermost first):
    58: get
    57: <reactive:visible_channels> [C:\Users\m.nierhoff\Desktop\AnalyseR\RAnalysen\channel-forecasting/server.R#60]
    46: visible_channels
    45: updateSelectInput
    44: observerFunc [C:\Users\m.nierhoff\Desktop\AnalyseR\RAnalysen\channel-forecasting/server.R#69]
     1: shiny::runApp

1 个答案:

答案 0 :(得分:0)

以下是动态更改输入选项的另一种方法。在这里,我使用uiOutputrenderUI函数对在我的server.R中创建一个UI元素,其优点是server.R可以执行任意R代码:

library(shiny)

ui <- fluidPage(

   # Application title
   titlePanel("Select CSV demo"),

   # Sidebar with a slider input for number of bins 
   sidebarLayout(
      sidebarPanel(
        radioButtons(
          inputId = "fruit_view", 
          label = "Select a fruit", 
          choices = c("Top Fruit View"="1", 
                      "Current Fruit Split"="2",
                      "Top Fruit & Value Class"="3"), 
          selected = "1",
          inline = TRUE)
      ),

      # Show a plot of the generated distribution
      mainPanel(

         uiOutput("ChoicesUI")
      )
   )
)


server <- function(input, output) {

   output$ChoicesUI <- renderUI({
     a <- read.table(paste0(input$fruit_view,".csv"))
     selectInput("Choices2",
                 label="select from file",
                 choices = a$V1)
   })
}

# Run the application 
shinyApp(ui = ui, server = server)

我手动创建了3个名为1.csv2.csv3.csv的csv,每个csv在每一行都有三个不同的元素,以便完成这项工作。

enter image description here

如果您需要进一步解释,或者这不符合您的要求,请告诉我