首页 > 解决方案 > 选择文件小部件:“FUN 中的错误:找不到对象‘类型’”

问题描述

我正在构建一个闪亮的应用程序,它允许我使用小部件“选择文件”和“选择文件”选择数据文件,以及使用库 ggplot2 的 geom_bar 对象绘制条形图。该图由一个条形图组成,该条形图表示每种操作类型(“类型”)的收入(“收入”),并且每种类型都有不同颜色的条形图。当我运行该应用程序时,我收到以下错误:FUN 中的错误:找不到对象“类型”。

我已经通过 aes_string 更改了 aes 但它没有改变任何东西。我还尝试在 geom_bar 对象中添加 inherit.aes = FALSE。我确保我使用的数据保存为数据框。

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

#user interface
ui <- fluidPage(
  headerPanel(title = "Shiny File Upload"),
  sidebarLayout(
    sidebarPanel(
      fileInput(inputId = "file",
                label = "Upload the file",
                multiple = TRUE),
      checkboxInput(inputId = "header", label = "Header"),
      radioButtons("sep","Seperator", choices = c(Comma=",", Period = ".", Semicolon = ";")),


      # Select variable for y-axis
      selectInput(inputId = "y", 
                  label = "Revenue:",
                  choices = "Revenue",
                  selected = ""),

      # Select variable for x-axis
      selectInput(inputId = "x", 
                  label = "X-axis:",
                  choices = "Type",
                  selected = ""),

      # Select variable for color
      selectInput(inputId = "z", 
                  label = "Color by:",
                  choices = "Type",
                  selected = "")
    ), 
    # Outputs
    mainPanel(
      uiOutput("input_file"),
      plotOutput(outputId = "Barplot")
    )
  )
)

# Define server function required to create the scatterplot
server <- function(input, output) {


  #Dispays the content of the input$file dataframe
  output$filedf <- renderTable({
  if(is.null(input$file)){return()}
   input$file
  })
  output$filedf2 <- renderTable({
    if(is.null(input$file)){return()}
    input$file$datapath
  })
  #Side bar select input widget coming through render UI()
  output$selectfile <- renderUI({
    if(is.null(input$file)){return()}
    list(hr(),
         helpText("Select the files for which you need to see data and summary stats"),
         selectInput("Select", "Select", choices=input$file$name)
         )
  })


      # Create the scatterplot object the plotOutput function is expecting

  output$Barplot <- renderPlot({
    ggplot(data = input$file, aes_string(x = input$x , y = input$y, fill = input$x)) + geom_bar( stat ="identity") + coord_flip()

  })  
}

shinyApp(ui = ui, server = server)

我希望有一个条形图,其中包含 14 种操作类型的收入条,条形颜色根据观察结果而有所不同。我希望能够选择我想要的数据并获得这个数据集的条形图。

标签: rshinywidgetreactive

解决方案


推荐阅读