shinydashboard中散点图加载数据时出错

shinydashboard中散点图加载数据时出错,r,ggplot2,shiny,shinydashboard,shiny-server,R,Ggplot2,Shiny,Shinydashboard,Shiny Server,我试图在闪亮的仪表板中创建散点图输出。我有几年类似的数据集,我想根据选择的变量和年份进行绘图。数据集文件名为Y96Total8.rda、Y97Total8.rda。。。数据集名称为总计(data.table) 不幸的是,我无法以真实的方式加载数据集来绘制结果,并且在“绘图”选项卡中出现了错误“数学函数的非数值参数” 如果有人对如何使用闪亮的仪表盘绘制此图有任何建议,将不胜感激 我已附上代码 library(shiny) library(shinydashboard) library(data.t

我试图在闪亮的仪表板中创建散点图输出。我有几年类似的数据集,我想根据选择的变量和年份进行绘图。数据集文件名为Y96Total8.rda、Y97Total8.rda。。。数据集名称为总计(data.table)

不幸的是,我无法以真实的方式加载数据集来绘制结果,并且在“绘图”选项卡中出现了错误“数学函数的非数值参数”

如果有人对如何使用闪亮的仪表盘绘制此图有任何建议,将不胜感激

我已附上代码

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)
库(闪亮)
图书馆(shinydashboard)
库(数据表)
图书馆(GG2)
图书馆(绘本)
图书馆(DT)

header您的
ggplot
调用应更改为

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

p您的
ggplot
调用应更改为

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

p欢迎使用SO,请尝试生成reprex,尝试将y封装在反应函数中。所有传递的输入都应该是反应式的。欢迎使用SO!您的代码并不是一个真正的最小可还原示例。如果您的问题在“绘图”选项卡中,则删除与该特定选项卡不相关的所有内容会有所帮助。也就是说,在不运行代码9的情况下,我建议将
req(输入$slcT2Year3)
放在散点图的
renderPlot
的开头。应用程序启动时会触发渲染器(以及任何反应器)。如果一个输入在这一点上没有值(而且通常没有),问题就会接踵而至。以及渲染器中引用到
req
调用的任何其他
输入。由于您没有提供正在加载的示例数据(Test.rda),因此很难知道您拥有什么样的数据。但是,正如其他人所建议的,您需要使用一些反应式函数,
req
调用,并在输入变量在该点没有值时检查并提供“return(NULL)”。欢迎这样做,请尝试生成一个reprex,尝试将y封装在反应式函数中。所有传递的输入都应该是反应式的。欢迎使用SO!您的代码并不是一个真正的最小可还原示例。如果您的问题在“绘图”选项卡中,则删除与该特定选项卡不相关的所有内容会有所帮助。也就是说,在不运行代码9的情况下,我建议将
req(输入$slcT2Year3)
放在散点图的
renderPlot
的开头。应用程序启动时会触发渲染器(以及任何反应器)。如果一个输入在这一点上没有值(而且通常没有),问题就会接踵而至。以及渲染器中引用到
req
调用的任何其他
输入。由于您没有提供正在加载的示例数据(Test.rda),因此很难知道您拥有什么样的数据。但是,正如其他人所建议的,您需要使用一些反应式函数,
req
调用,并在输入变量在该点没有值时检查并提供“return(NULL)”。