R Shiny - 错误子集数据

时间:2018-02-17 23:15:21

标签: r shiny

我是一个非常新的闪亮的人,我试图在用CSV读取文件后基于变量分层对数据进行子集化。我经常收到错误"对象类型'关闭'不是子集表格"。我将不胜感激任何帮助。谢谢。

代码是:

library(shiny)
library(ggplot2)
library(dplyr)
library(tidyverse)


## Only run examples in interactive R sessions
if (interactive()) {

ui <- fluidPage(

# App title ----
titlePanel("Uploading Files"),

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

  # Sidebar panel for inputs ----
  sidebarPanel(

    # Input: Select a file ----
    fileInput("file1", "Choose CSV File",
              multiple = TRUE,
              accept = c("text/csv",
                         "text/comma-separated-values,text/plain",
                         ".csv")),

    # Horizontal line ----
    tags$hr(),

    # Input: Checkbox if file has header ----
    checkboxInput("header", "Header", TRUE),

    # Input: Select separator ----
    radioButtons("sep", "Separator",
                 choices = c(Comma = ",",
                             Semicolon = ";",
                             Tab = "\t"),
                 selected = ","),

    # Input: Select quotes ----
    radioButtons("quote", "Quote",
                 choices = c(None = "",
                             "Double Quote" = '"',
                             "Single Quote" = "'"),
                 selected = '"'),

    # Horizontal line ----
    tags$hr(),

    # Input: Select number of rows to display ----
    radioButtons("disp", "Display",
                 choices = c(Head = "head",
                             All = "all"),
                 selected = "head"),



# Include a Slider for Strata
  sliderInput("strata",
              "strata:",
              min = 1,
              max = 20,
              value = c(1,20),
              step=1)

  ),  

########################## 

  # Main panel for displaying outputs ----
  mainPanel(

    # Output: Data file ----
    tableOutput("contents")

  )  
 )
)

###


server <- function(input, output, session) {

output$contents <- renderTable({



  req(input$file1)

  df <- read.csv(input$file1$datapath,
                 header = input$header,
                 sep = input$sep,
                 quote = input$quote, stringsAsFactors = FALSE)

df<-data.frame(df)

  filtered<-reactive({
                  df()%>% 
         filter(df$strata>=input$strata[1] &          df$strata<=input$strata[2])})



  if(input$disp == "head") {
    return(head(filtered))
  }
  else {
    return(filtered)
  }



    })

  }
  # Run the app ----
  shinyApp(ui, server)

}

1 个答案:

答案 0 :(得分:1)

请勿在{{1​​}}来电中定义reactive,但要将其作为单独的元素。另外,正如Martin在评论中指出的那样,不要在那里使用renderTable。当你从被动函数调用一个值时,你需要这样做,这不是你在那里做的。

下面给出一个工作实例。希望这有帮助!

df()