簡體   English   中英

在 shinydashboard 中加載分散 plot 的數據時出錯

[英]Error in loading data for scatter plot in shinydashboard

我正在嘗試在 shiny 儀表板中創建分散的 plot 輸出。 我有幾年類似的數據集,我想根據選擇的變量和年份來 plot。 數據集文件名為Y96Total8.rda, Y97Total8.rda ... 數據集名為Total (data.table)

不幸的是,我無法以真正的方式將數據集加載到 plot 結果,並且在 plot 選項卡中出現錯誤“數學函數的非數字參數”

如果有人對如何使用 shiny 儀表板生產此 plot 有任何建議,我們將不勝感激。

我附上了代碼。

library(shiny)
library(shinydashboard)
library(data.table)
library(ggplot2)
library(plotly)
library(DT)

header <- dashboardPage(
  skin = "green",
  dashboardHeader(title = "TEST"),
  dashboardSidebar(sidebarMenu(
    dir = "ltr",
    align = "right",
    menuItem("Correlation", tabName = "Correlation", icon = icon("users"))
  )),
  dashboardBody(load(file = "data/Test.rda"),
                dir = "ltr",
                tabItems(
                  tabItem(tabName = "Correlation",
                          fluidRow(tabsetPanel(
                            tabPanel(
                              "Inputs",
                              box(
                                status = "danger",
                                solidHeader = TRUE,
                                width = 6,
                                title = "Food Expenditures Per",
                                sliderInput(
                                  inputId = "Food_Expenditures_Per2",
                                  label = "Food Expenditures",
                                  min = 0,
                                  max = 30000000,
                                  value = c(1000000, 10000000)
                                )
                              ),
                              box(
                                status = "danger",
                                solidHeader = TRUE,
                                title = "Total Expenditures Per",
                                width = 6,
                                sliderInput(
                                  inputId = "Total_Exp_Month_Per2",
                                  label = "Total Expenditures Per",
                                  min = 0,
                                  max = 100000000,
                                  value = c(1000000, 30000000)
                                )
                              ),
                              box(
                                status = "info",
                                solidHeader = TRUE,
                                title = "First Variable",
                                width = 6,
                                selectInput(
                                  "Var1",
                                  "First Variable",
                                  list("FoodExpenditure_Per", "Total_Exp_Month_Per"),
                                  selected =
                                    "FoodExpenditure_Per"
                                )
                              ),
                              box(
                                status = "info",
                                solidHeader = TRUE,
                                title = "Second Variable",
                                width = 6,
                                selectInput(
                                  "Var2",
                                  "Second Variable",
                                  list("FoodExpenditure_Per", "Total_Exp_Month_Per"),
                                  selected =
                                    "Total_Exp_Month_Per"
                                )
                              ),
                              box(
                                status = "info",
                                solidHeader = TRUE,
                                title = "Year",
                                width = 6,
                                selectInput(
                                  inputId = "slcT2Year3",
                                  label = "Year",
                                  choices =
                                    list(1390, 1391, 1392, 1393,
                                         1394, 1395, 1396, 1397),
                                  selected =
                                    1396
                                )
                              ),
                              box(
                                status = "info",
                                solidHeader = TRUE,
                                title = "Add line of best fit",
                                width = 6,
                                checkboxInput("fit", "Add line of best fit")
                              ),

                            ),
                            tabPanel(
                              "Plot"
                              ,
                              box(
                                status = "info",
                                solidHeader = TRUE,
                                width = 700,
                                height = 450,
                                plotOutput("scatterplot", width =
                                             600, height = 400)
                                ,
                                downloadButton("downloadPlot3", "Download")
                              )
                            )
                          )))
                ))
)


app_server <- function(input, output, session) {
  ##################### Scatter Plot #########################
  output$scatterplot <- renderPlot({
    y <- input$slcT2Year3
    fn3 <- paste0("data/Y", substr(y, 3, 4), "Total8.rda")
    load(fn3)

    Total <- subset(
      Total,
      FoodExpenditure_Per >= input$Food_Expenditures_Per2[1] &
        FoodExpenditure_Per <= input$Food_Expenditures_Per2[2] &
        Total_Exp_Month_Per >= input$Total_Exp_Month_Per2[1] &
        Total_Exp_Month_Per <= input$Total_Exp_Month_Per2[2]
    )


    p <- ggplot(Total, aes(input$Var1, input$Var2)) +
      geom_point() +
      scale_x_log10()

    if (input$fit == TRUE) {
      p <- p + geom_smooth(method = "lm")
    }
    p
  })


  session$onSessionEnded(function() {
    stopApp()
    #    q("no")
  })

}

shinyApp(header, app_server)

錯誤圖片:

在此處輸入圖像描述

您的ggplot調用應更改為

p <- ggplot(Total, aes(Total[[input$Var1]], Total[[input$Var2]]))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM