使用从文件夹中的闪亮到借出文件的下拉底部

时间:2018-03-29 12:39:19

标签: r shiny

我使用闪亮来从特定文件夹上传不同的数据文件,并根据某个列绘制直方图。每个文件的名称类似于"30092017ARB.csv"(日期+ ARB.csv)。 代码循环遍历数据文件夹中的所有文件名,并在下拉列表中打印文件名。选择文件名后,应上传并绘制mw-column的直方图(列名为“mw”)。我的GUI如下所示:

library("shiny")
  dataset <- list.files("C:/R_myfirstT/data", pattern=".*.csv$")
# Define UI for dataset viewer app ----
ui <- fluidPage(


  # App title ----
  titlePanel("Data plot"),

  # Sidebar layout with input and output definitions ----
  sidebarLayout(

    # Sidebar panel for inputs ----
    sidebarPanel(

      # Input: Selector for choosing dataset ----
      selectInput(inputId = "date",
                  label = "Choose a date:",
                  choices = dataset)

    ),

    # Main panel for displaying outputs ----
    mainPanel(
  plotOutput("plot")
     )
  )
)

和服务器

# Define server  ----
dataset <- list.files("C:/R_myfirstT/data", pattern=".*.csv$")
dat.name<-paste("C:/R_myfirstT/data/",dataset,sep = "")

server <- function(input, output) {

  datasetInput <- reactive({
    switch(input$dataset,
           for (i in 1:length(dataset)){
             toString(dataset[i])=read.csv(file=dat.name[i], header=TRUE, sep=";")
           }
          )
    output$plot <- renderPlot({
      hist(dataset.mw, breaks = 40)
    })
  })

}

我的问题是:我没有得到任何直方图!我只得到了drop down bottom这很好,但不完全是我的目标! 任何想法可能是什么原因?

1 个答案:

答案 0 :(得分:1)

这样的工作:

ui.R

library("shiny")

# Define UI for dataset viewer app ----
ui <- fluidPage(

  # App title ----
  titlePanel("Data plot"),

  # Sidebar layout with input and output definitions ----
  sidebarLayout(

    # Sidebar panel for inputs ----
    sidebarPanel(

      # Input: Selector for choosing dataset ----

      selectInput(inputId = 'date',
                           label = 'Choose a date:',
                           choices = list.files(path = "./data",
                                                full.names = FALSE,
                                                recursive = FALSE))
    ),

    # Main panel for displaying outputs ----
    mainPanel(
      plotOutput("plot")
    )
  )
)

server.R

# Define server  ----
server <- function(input, output) {

    dataset <- reactive({
      infile <- input$date
      if (is.null(infile)){
        return(NULL)
      }
      read.csv(paste0('./data/',infile))
    })

    output$plot <- renderPlot({
      x <- dataset()[,1]
      hist(x, breaks = 40)
    })

}

enter image description here