【问题标题】:Argument improper in shiny ggplot闪亮的ggplot中的参数不正确
【发布时间】:2018-12-12 17:58:33
【问题描述】:

我想创建一个闪亮的应用程序,在表格中绘制各种数据的图表。我创建了闪亮的应用程序

  1. 告诉用户选择要绘制的文件

  2. 将数据文件加载到数据数据库中

  3. 询问用户要绘制哪一列

  4. 将各种数据插入到各自的数据帧中

  5. 然后绘制相应的选定列

代码如下

    library(shiny)
library(ggplot2)
ui <- fluidPage(
   titlePanel("Creating a database"),
   sidebarLayout(
      sidebarPanel(
        textInput("name", "Company Name"),
        numericInput("income", "Income", value = 1),
        numericInput("expenditure", "Expenditure", value = 1),
        dateInput("date", h3("Date input"),value = Sys.Date() ,min = "0000-01-01",
                  max = Sys.Date(), format = "dd/mm/yy"),
        actionButton("Action", "Submit"),#Submit Button
        actionButton("new", "New")),
        mainPanel(
          tabsetPanel(type = "tabs",
                      tabPanel("Table", tableOutput("table")),
                      tabPanel("Download",
                               textInput("filename", "Enter Filename for download"),   #filename
                               helpText(strong("Warning: Append if want to update existing data.")),
                               downloadButton('downloadData', 'Download'), #Button to save the file
                               downloadButton('Appenddata', 'Append')),#Button to update a file )
                      tabPanel("Plot", 
                               actionButton("filechoose", "Choose File"),
                               br(),

                               selectInput("toplot", "To Plot", choices = c("Income" = "inc",
                                                                  "Expenditure" = "exp",
                                                                  "Gross Profit" = "gprofit",
                                                                  "Net Profit" = "nprofit"
                                                                  )),
                              actionButton("plotit", "PLOT"),
                              plotOutput("Plot"))
          )

      )
)
)
# Define server logic required to draw a histogram
server <- function(input, output){
  #Global variable to save the data
  Data <- data.frame()

  Results <- reactive(data.frame(input$name, input$income, input$expenditure,
                                 as.character(input$date),
                                 as.character(Sys.Date())))

  #To append the row and display in the table when the submit button is clicked
  observeEvent(input$Action,{
    Data <<- rbind(Data,Results()) #Append the row in the dataframe
    output$table <- renderTable(Data) #Display the output in the table
  })

  observeEvent(input$new, {
    Data <<- NULL
    output$table <- renderTable(Data)
  })

  observeEvent(input$filechoose, {
    Data <<- read.csv(file.choose()) #Choose file to plot
    inc <- as.numeric(Data[ ,2]) 
    exp <- as.numeric(Data[ ,3]) 
    date <- Data[,4]
    gprofit <- exp - inc
    nprofit <- (exp - inc) * 0.06
    output$table <- renderTable(Data) #Display the choosen file details
  })

  output$downloadData <- downloadHandler(
    filename = function() {
      paste(input$filename , ".csv", sep="")}, # Create the download file name
    content = function(file) {
      write.csv(Data, file,row.names = FALSE) # download data
    })

  output$Appenddata <- downloadHandler(
    filename = function() {
      paste(input$filename, ".csv", sep="")}, 
    content = function(file) {
      write.table( Data, file=file.choose(),append = T, sep=',',
                   row.names = FALSE, col.names = FALSE) # Append data in existing
    })

  observeEvent(input$plotit, {
    bab <- input$toplot
               output$Plot <- renderPlot("Plot",
                                         ggplot()+ geom_bar(data = Data, aes(x= input$toplot,
                                                            y= date)))})
}

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

但是当我按下“绘图”按钮时,它给出了错误

Error in *: non-numeric argument to binary operator`

我哪里错了?我还使用 as.numeric 将数据转换为数字以消除错误。打开也可以更改它的建议。请帮忙。谢谢。

数据如

【问题讨论】:

  • 在您的renderPlot 部分似乎您需要将其指定为geom_bar(aes(x = input$toplot, y = date))
  • 是的,我看到了那个错误并更正了它。还是不行。同样的错误
  • 我觉得 input$toplot 的类与 geom_bar() 中的 x= 不同。这是正确的吗?我该如何纠正它?
  • 你能提供一个你在 ggplot 中输入的数据的例子吗?
  • 在编辑中添加@Thor6

标签: r ggplot2 shiny


【解决方案1】:

使用来自switch() statement usage 的开关盒 或How to use the switch statement in R functions? 帮助您做出选择。

library(shiny)
library(ggplot2)
ui <- fluidPage(
  titlePanel("Creating a database"),
  sidebarLayout(
    sidebarPanel(
      textInput("name", "Company Name"),
      numericInput("income", "Income", value = 1),
      numericInput("expenditure", "Expenditure", value = 1),
      dateInput("date", h3("Date input"),value = Sys.Date() ,min = "0000-01-01",
                max = Sys.Date(), format = "dd/mm/yy"),
      actionButton("Action", "Submit"),#Submit Button
      actionButton("new", "New")),

    mainPanel(
      tabsetPanel(type = "tabs",
                  tabPanel("Table", tableOutput("table")),
                  tabPanel("Download",
                           textInput("filename", "Enter Filename for download"),   #filename
                           helpText(strong("Warning: Append if want to update existing data.")),
                           downloadButton('downloadData', 'Download'), #Button to save the file
                           downloadButton('Appenddata', 'Append')),#Button to update a file )
                  tabPanel("Plot", 
                           actionButton("filechoose", "Choose File"),
                           br(),

                           selectInput("toplot", "To Plot", choices = c("Income" = "inc",
                                                                        "Expenditure" = "exp",
                                                                        "Gross Profit" = "gprofit",
                                                                        "Net Profit" = "nprofit"
                           )),
                           actionButton("plotit", "PLOT"),
                           plotOutput("Plot")
                  )
      )

    )
  )
)
# Define server logic required to draw a histogram
server <- function(input, output){
  #Global variable to save the data
  Data <- data.frame()

  Results <- reactive(data.frame(input$name, input$income, input$expenditure,
                                 as.character(input$date),
                                 as.character(Sys.Date())))

  #To append the row and display in the table when the submit button is clicked
  observeEvent(input$Action,{
    Data <<- rbind(Data,Results()) #Append the row in the dataframe
    output$table <- renderTable(Data) #Display the output in the table
  })

  observeEvent(input$new, {
    Data <<- NULL
    output$table <- renderTable(Data)
  })

  observeEvent(input$filechoose, {
    Data <<- read.csv(file.choose()) #Choose file to plot
    output$table <- renderTable(Data) #Display the choosen file details
  })

  output$downloadData <- downloadHandler(
    filename = function() {
      paste(input$filename , ".csv", sep="")}, # Create the download file name
    content = function(file) {
      write.csv(Data, file,row.names = FALSE) # download data
    })

  output$Appenddata <- downloadHandler(
    filename = function() {
      paste(input$filename, ".csv", sep="")}, 
    content = function(file) {
      write.table( Data, file=file.choose(),append = T, sep=',',
                   row.names = FALSE, col.names = FALSE) # Append data in existing
    })

  observeEvent(input$plotit, {
    inc <- c(Data[ ,2]) 
    exp <- c(Data[ ,3]) 
    date <- c(Data[,4])
    gprofit <- c(Data[ ,3]- Data[ ,2])
    nprofit <- (exp - inc) * 0.06
    y = input$toplot

    switch(EXPR = y ,
           inc = output$Plot <- renderPlot(ggplot(data = Data, aes(x= date, y= inc))+
                                             geom_bar(stat = "identity",
                                                      fill = "blue")+xlab("Dates")+
                                             ylab("Income")),
           exp = output$Plot <- renderPlot(ggplot(data = Data, aes(x= date, y= exp))+
                                             geom_bar(stat = "identity",
                                                      fill = "blue")+xlab("Dates")+
                                             ylab("Expenditure")),
           gprofit = output$Plot <- renderPlot(ggplot(data = Data, aes(x= date, y= gprofit))+
                                                 geom_bar(stat = "identity",
                                                          fill = "blue")+xlab("Dates")+
                                                 ylab("Gross Profit")),
           nprofit =  output$Plot <- renderPlot(ggplot(data = Data, aes(x= date, y= nprofit))+
                                                  geom_bar(stat = "identity",
                                                           fill = "blue")+xlab("Dates")+
                                                  ylab("Net Profit")))})
}

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

【讨论】:

    猜你喜欢
    • 2021-02-13
    • 1970-01-01
    • 1970-01-01
    • 2013-11-01
    • 2019-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多