Shinny R:将输出与csv中的现有数据一起打印到散点图上
我想让用户看到他们收入的百分位数。我的应用程序接收输入,然后运行一些计算以显示新的图形。我想使用ggplot2将这两个输出绘制到散点图上 我的数据是csv,如下所示:Shinny R:将输出与csv中的现有数据一起打印到散点图上,r,ggplot2,shiny,R,Ggplot2,Shiny,我想让用户看到他们收入的百分位数。我的应用程序接收输入,然后运行一些计算以显示新的图形。我想使用ggplot2将这两个输出绘制到散点图上 我的数据是csv,如下所示: PP BTI ATI 1 9710 9660 2 10000 9900 3 10300 10100 4 10600 10400 etc server.R摘录: #income calculator incomeFT <- eventReactive(input$updateBut
PP BTI ATI
1 9710 9660
2 10000 9900
3 10300 10100
4 10600 10400
etc
server.R摘录:
#income calculator
incomeFT <- eventReactive(input$updateButton, {
if (input$incomeNum < input$taxFree) print(input$incomeNum)
else print ((input$incomeNum-input$taxFree)/100*input$flatIncomeTax)
})
output$incomeFT <- renderPrint({
incomeFT()
})
#Income Percentile Scatterplot
incomedata <- read.csv("~/Documents/Work/Shiny/Level 7/Shiny Flat Tax/Flat Tax App/data/incomedist.csv")
ranges <- reactiveValues(x = NULL, y = NULL)
output$plot1 <- renderPlot({
ggplot(incomedata, aes(x = BTI, y = PP)) +
geom_point() +
coord_cartesian(xlim = ranges$x, ylim = ranges$y)
})
#Brush and zoom on scatterplot
observeEvent(input$plot1_dblclick, {
brush <- input$plot1_brush
if (!is.null(brush)) {
ranges$x <- c(brush$xmin, brush$xmax)
ranges$y <- c(brush$ymin, brush$ymax)
}
else {
ranges$x <- NULL
ranges$y <- NULL
}
})
numericInput(inputId = "incomeNum",
label = "Your annual income:",
value = 0
),
plotOutput("plot1", height = 300,
dblclick = "plot1_dblclick",
brush = brushOpts(
id = "plot1_brush",
resetOnNew = TRUE
)
)
如果这是一个非常简单的问题,请道歉。我已经搜索了stackoverflow、ggplot2文档和闪亮的教程/文档,但运气不好
我是否需要找到将输出写入csv的方法?任何帮助都将不胜感激。谢谢
编辑:一个可视示例
编辑2:sessionInfo()
sessionInfo()
R版本3.2.4(2016-03-10)
平台:x86_64-apple-darwin13.4.0(64位)
运行于:OS X 10.11.4(El Capitan)
区域设置:
en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
附加基本包:
统计图形GRUTILS数据集方法库
其他随附包裹:
ggplot2_2.1.0 dplyr_0.4.3光泽_0.13.2
通过命名空间加载(未附加):
Rcpp_0.12.4摘要_0.6.9资产认为_0.1 mime_0.4 plyr_1.8.3网格_3.2.4 R6_2.1.2[8] jsonlite_0.9.19 xtable_1.8-2 gtable_0.2.0 DBI_0.4-1 magrittr_1.5标尺_0.4.0工具_3.2.4
[15] munsell_0.4.3 httpuv_1.3.3 parallel_3.2.4 rsconnect_0.4.3 colorspace_1.2-6 htmltools_0.3.5本质上,当特定输入的值更改时,您希望在数据集中触发更改,使数据集成为被动值。下面是你可以如何着手做这件事。请注意,我使用截至该点的整个数据(而不是原始数据)计算新数据点的百分比
library(dplyr)
library(shiny)
library(ggplot2)
# ui
ui_foo = fluidPage(
plotOutput(
"plot_foo"
),
numericInput(inputId = "income", label = "Income: ", value = NULL),
actionButton("button_click", "Go!")
)
# server
server_foo = shinyServer(function(input, output) {
react_vals = reactiveValues(
# simulate some data --> initialize the reactive dataset
df_foo = data_frame(
percentile = seq.int(99),
BTI = sort(rnorm(99))
)
)
# change the data when the button changes
observeEvent(input$button_click, {
ecdf_income = ecdf(react_vals$df_foo$BTI)
react_vals$df_foo = rbind(react_vals$df_foo,
c(percentile = ecdf_income(input$income)*100,
BTI = input$income))
})
# make the plot respond to changes in the dataset
output$plot_foo = renderPlot({
react_vals$df_foo %>%
ggplot(aes(x = percentile, y = BTI)) +
geom_point() +
geom_line() +
theme_bw()
})
})
# run the app
shinyApp(ui = ui_foo, server = server_foo)
更新:
在第一个答案中添加三个功能:
Done
按钮)在变量df\u foo\u updated
中返回这些点library(dplyr)
library(shiny)
library(ggplot2)
# ui
ui_foo = fluidPage(
plotOutput(
"plot_foo"
),
numericInput(inputId = "income", label = "Income: ", value = NULL),
actionButton("button_click", "Update"),
textOutput("entered_income_percentile"),
br(),
actionButton(inputId = "done", "Done")
)
# server
server_foo = shinyServer(function(input, output) {
react_vals = reactiveValues(
# simulate some data --> initialize the reactive dataset
df_foo = data_frame(
Percentile = as.numeric(seq.int(99)),
BTI = sort(rnorm(99)),
`Data Type` = "Original"
)
)
# change the data when the button changes
observeEvent(input$button_click, {
ecdf_income = ecdf(react_vals$df_foo$BTI)
react_vals$df_foo = rbind(react_vals$df_foo,
c(Percentile = ecdf_income(input$income)*100,
BTI = input$income, `Data Type` = "User Added"))
# the percentile of the income entered
output$entered_income_percentile = renderText({
req(input$income)
paste0("The entered income lies in the: ", round(ecdf_income(input$income)*100, 2), " percentile.")
})
})
# make the plot respond to changes in the dataset
output$plot_foo = renderPlot({
react_vals$df_foo %>%
ggplot(aes(x = as.numeric(Percentile), y = as.numeric(BTI), color = `Data Type`, group = `Data Type`)) +
geom_point(size = 1.6) +
theme_bw() +
xlab("Percentile") + ylab("Income")
})
# when app is stopped
observeEvent(input$done, {
stopApp(react_vals$df_foo)
})
})
# run the app
app_foo = shinyApp(ui = ui_foo, server = server_foo)
df_foo_updated = runApp(app_foo)
@gizzard你可以详细说明你想要什么,我会看看是否能在答案中包含它们。我要添加的三件事是将计算出的百分位推回到用户,以不同于我们开始使用的点的颜色标记用户输入的点,以及在应用程序停止后能够收集用户输入的点。这些都不是很难做到的。我需要的基本要素都在你的答案中,但我还没有到掌握它的阶段。看着r-cookbook的页面,我想到了一个理论:我可以将输入写入csv吗?(现在是长格式。)
PP收入类别1 9710 BTI。。。99 159000 BTI 1 9660 ATI。。。99 107000 ATI blank blank User
然后(以某种方式)通过引用其余数据计算输入的百分比?我需要绘图来显示整个数据。我已经在我的原始帖子中添加了一个图像,以显示我的意思。@gizzard我已经更新了答案,以包含您要求的功能。请运行第二段代码,看看它是否符合您的要求。如果希望ATI
变量也出现在绘图上,则需要组合第一个答案中的代码。我得到了以下警告:warning:Error in handlers$add:Key/已在使用堆栈跟踪(最内层的第一个):43:handlers$add 42:handlerManager$addHandler 41:startApp 40:runApp 1:runApp在handlers$add中出错(handler,key,tail):key/已在使用中
@gizzard重新启动R会话,并清除所有其他变量。如果问题仍然存在,请发布您的sessionInfo()
。这似乎是一个版本问题。还有,尽管有警告,这个应用程序还能工作吗?