【问题标题】:ggplot2 mutate error when select variable from uploaded dataset in R shinydashbard从 R shinydashbard 中上传的数据集中选择变量时,ggplot2 变异错误
【发布时间】:2022-01-06 02:04:23
【问题描述】:

我正在尝试在 R 中使用 ggplot 进行绘图。我想上传数据,任何变量都可以用于绘图。我正在尝试动态保留aes()。我尝试了几个例子example 1,但对我有用。这是我的代码:

library(shiny)
library(shinydashboard)
library(readxl)
library(DT)
library(dplyr)
library(ggplot2)
# Define UI for application that draws a histogram
ui <- fluidPage(
  titlePanel("Uploading Files"),
  sidebarLayout(
    sidebarPanel(
      fileInput('file1', 'Upload data File',
                accept=c('text/csv','.xlsx', 
                         'text/comma-separated-values,text/plain', 
                         '.csv'))),
      mainPanel(
        DT::dataTableOutput('contents')
      )
    ),
  tabPanel("First Type",
           pageWithSidebar(
             headerPanel('Visualization of Dengue Cases'),
             sidebarPanel(
               
              
               selectInput('xcol', 'X Variable', ""),
               selectInput('ycol', 'Y Variable', "", selected = "")
               
             ),
             
             mainPanel(
               plotOutput('MyPlot')
             )
           )
  )
  )
  
       
       
# Define server logic required to draw a histogram
server <- function(input, output,session) {

  data <- reactive({ 
    req(input$file1) 
    
    inFile <- input$file1 
    
    
    df <- read_excel(paste(inFile$datapath,  sep=""), 1)
    
    
    
    
    updateSelectInput(session, inputId = 'xcol', label = 'X Variable',
                      choices = names(df), selected = names(df))
    updateSelectInput(session, inputId = 'ycol', label = 'Y Variable',
                      choices = names(df), selected = names(df)[2])
    
    return(df)
  })
  
  output$contents <- DT::renderDataTable({
    data()
  },options = list(pageLength = 10, width="100%", scrollX = TRUE))
  
  
  output$MyPlot <- renderPlot({
    select_quo <- quo(input$MyPlot_select)
    
    data %>%
      mutate(user_input = !!select_quo) %>%
      ggplot(aes(fill=user_input,  y=user_input, x= user_input)) + 
      geom_bar( stat="identity")
    
    })
}
# Run the application 
shinyApp(ui = ui, server = server)

可以使用任何数据集,例如Diamond dataset。 还请帮助允许所有类型的数据格式(.csv, .txt,.xls)。目前只接受.xls

【问题讨论】:

    标签: r ggplot2 shinydashboard


    【解决方案1】:

    您的代码有几个问题。

    1. 您在renderPlot 中使用data 而不是data()
    2. 没有输入input$MyPlot_select
    3. 使用quo!! 不会得到想要的结果。相反,如果您的列名是字符串,您可以简单地使用 .data 代词。
    4. renderPlot的开头添加req

    这表示您的renderPlot 应该是这样的:

    output$MyPlot <- renderPlot({
        req(input$xcol, input$ycol)
    
        x <- input$xcol
        y <- input$ycol
        fill <- input$xcol
        
        ggplot(data(), aes(x = .data[[x]], y = .data[[y]], fill=.data[[fill]])) + 
          geom_col()
      })
    

    关于你问题的第二部分。要使您的应用程序适用于不同类型的输入文件,您可以使用例如获取文件扩展名tools::file_ext 并在switch 语句中使用结果。

    完全可重现的代码:

    library(shiny)
    library(shinydashboard)
    library(readxl)
    library(DT)
    library(dplyr)
    library(ggplot2)
    
    ui <- fluidPage(
      titlePanel("Uploading Files"),
      sidebarLayout(
        sidebarPanel(
          fileInput("file1", "Upload data File",
            accept = c(
              "text/csv", ".xlsx",
              "text/comma-separated-values,text/plain",
              ".csv"
            )
          )
        ),
        mainPanel(
          DT::dataTableOutput("contents")
        )
      ),
      tabPanel(
        "First Type",
        pageWithSidebar(
          headerPanel("Visualization of Dengue Cases"),
          sidebarPanel(
            selectInput("xcol", "X Variable", ""),
            selectInput("ycol", "Y Variable", "", selected = "")
          ),
          mainPanel(
            plotOutput("MyPlot")
          )
        )
      )
    )
    
    
    
    # Define server logic required to draw a histogram
    server <- function(input, output, session) {
      data <- reactive({
        req(input$file1)
    
        inFile <- input$file1
    
        type <- tools::file_ext(inFile$name)
        
        filename <- inFile$datapath
        
        df <- switch(type,
                     "xlsx" = read_excel(filename),
                     "csv" = read_csv(filename),
                     "tsv" = read_tsv(filename))
        
        updateSelectInput(session,
          inputId = "xcol", label = "X Variable",
          choices = names(df), selected = names(df)
        )
        updateSelectInput(session,
          inputId = "ycol", label = "Y Variable",
          choices = names(df), selected = names(df)[2]
        )
    
        return(df)
      })
    
      output$contents <- DT::renderDataTable({
          data()
        }, options = list(pageLength = 10, width = "100%", scrollX = TRUE))
    
      output$MyPlot <- renderPlot({
        req(input$xcol, input$ycol)
    
        x <- input$xcol
        y <- input$ycol
        fill <- input$xcol
        
        ggplot(data(), aes(x = .data[[x]], y = .data[[y]], fill=.data[[fill]])) + 
          geom_col()
      })
    }
    # Run the application
    shinyApp(ui = ui, server = server)
    

    【讨论】:

      猜你喜欢
      • 2018-12-28
      • 1970-01-01
      • 2019-08-30
      • 1970-01-01
      • 1970-01-01
      • 2019-01-19
      • 2020-02-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多