r - 为闪亮仪表板中的散点图加载数据时出错
问题描述
我正在尝试在闪亮的仪表板中创建散点图输出。几年来我有类似的数据集,我想根据选择的变量和年份进行绘图。数据集文件名为Y96Total8.rda, Y97Total8.rda ... 数据集名为Total (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)
错误图片:
解决方案
您的ggplot
电话应更改为
p <- ggplot(Total, aes(Total[[input$Var1]], Total[[input$Var2]]))
推荐阅读
- python - 查找二维数组中前“n”个元素的二维坐标(行,列),然后使用这些坐标使用 python 对其他数组进行操作?
- reactjs - 在 React 上的多个组件中导入样式
- sql - 按列分组并选择其他列作为rails中的列表
- amazon-dynamodb - 使用 Appsync 和 DynamoDB 按 id 获取项目的有效方法
- arrays - 如何将与字符串分离的元素添加到另一个数组?
- image - Cordova 插件 cordova-plugin-wkwebview-engine 无法显示图像
- python - 使用 add_url_rule 方法的烧瓶路由
- javascript - 导出默认值有效,但在 React 中无法导出任何其他内容
- node.js - 如何执行并获得 NodeJS 承诺的结果?
- node.js - 如何将图像上传到 Firebase?可能吗?