将反应输入传递到 R Shiny 中的绘图轴
Posted
技术标签:
【中文标题】将反应输入传递到 R Shiny 中的绘图轴【英文标题】:Passing Reactive Input into axis of a plotyly chart in R Shiny 【发布时间】:2017-08-31 09:58:46 【问题描述】:我目前正在尝试一个闪亮的应用程序,它输出简单线性回归和图表的摘要。对于这两种情况,我希望用户从表的列中选择自变量和因变量,并使用这些相同的输入来运行回归和图形。现在我不知道如何将用户选择的输入传递给 plotly 进行显示。谁能帮忙,先谢谢了!
这里是示例数据:
AvgIR SumCount AvgLTV AvgGFEE AvgRTC Date
1: 0.04106781 180029 0.753180543134717 0.002424778 319.6837 2015-10-01
2: 0.04036154 160061 0.738038310394162 0.002722529 312.6314 2015-11-01
3: 0.04001407 145560 0.739287372904644 0.002425912 313.0351 2015-12-01
4: 0.04034078 147693 0.739693214979721 0.002600640 315.0238 2016-01-01
5: 0.04055688 142545 0.734515977410642 0.002449523 310.3950 2016-02-01
6: 0.04007467 176344 0.735780463185592 0.002459228 309.9615 2016-03-01
这是用户界面:
ui <- fluidPage(
headerPanel("Regression and Time Series Analysis"),
sidebarPanel(
p("Select a Dependent Variable"),
selectInput(inputId = "DepVar", label = "Dependent Variables", multiple = FALSE, choices = names(RegData2)),
p("Select input(s) for the Independent Variable(s)"),
selectInput(inputId = "IndVar", label = "Independent Variables", multiple = FALSE, choices = list( "SumCount", "AvgIR", "AvgLTV", "AvgGFEE", "AvgRTC", "Date"), selected = "AvgLTV"),
p("Summary of Regression"),
verbatimTextOutput(outputId = "RegSum")
),
mainPanel(
verbatimTextOutput(outputId = "IndPrint"),
verbatimTextOutput(outputId = "DepPrint"),
verbatimTextOutput(outputId = "test"),
verbatimTextOutput(outputId = "xaxis"),
verbatimTextOutput(outputId = "yaxis"),
tableOutput("table"),
plotlyOutput("graph")
)
)
这里是服务器:
server <- function(input, output)
lm1 <- reactive(lm(reformulate(input$IndVar, input$DepVar), data = RegData2))
Ind <- reactive(input$IndVar)
Dep <- reactive(input$DepVar)
plotdata <- reactive(as.data.frame(RegData2[, c(which(names(RegData2) == Ind()), which(names(RegData2) == Dep())), with = FALSE]))
xaxis <- reactive(names(RegData2)[which(names(RegData2) == Ind())])
yaxis <- reactive(names(RegData2)[which(names(RegData2) == Dep())])
# xaxisN <- reactive(names(xaxis()))
# yaxisN <- reactive(names(yaxis()))
output$table <- renderTable(
x<-plotdata()
#RegData2[, c(which(names(RegData2) == Ind()), which(names(RegData2) == Dep())), with = FALSE]
)
output$graph <- renderPlotly(
#xaxis <- paste(input$IndVar)
#yaxis <- paste(input$DepVar)
#THIS ONE WORKS, but isn't reactive
#plot<-plot_ly(plotdata(), x = ~AvgLTV, y = ~AvgIR, mode = "markers", type = "scatter")
#THIS ONE DOESN'T WORK, is reactive
plot<-plot_ly(plotdata(), x = ~input$IndVar, y = ~input$DepVar, mode = "markers", type = "scatter")
)
output$IndPrint <- renderPrint(str(Ind()))
output$test <- renderPrint(str(plotdata()))
output$xaxis <- renderPrint(xaxis())
output$yaxis <- renderPrint(yaxis())
output$DepPrint <- renderPrint(input$DepVar)
output$RegSum <- renderPrint(summary(lm1()))
shinyApp(ui = ui, server = server)
【问题讨论】:
不熟悉, with = FALSE
中的plotdata
反应式。应该做什么?无论如何,我得到了一个错误。
感谢您的以下回答,您的解决方案有效! data.tables 的“with = FALSE”仅将数据子集到我在字符值语句的“j”部分中输入的那两列。例如。 [我,j,由]。使用以下语句:RegData2[, c(which(names(RegData2) == "AvgLTV"), which(names(RegData2) == "AvgIR")), with = FALSE] 它返回:AvgLTV AvgIR 1: 0.753180543134717 0.04106781 2: 0.738038310394162 0.04036154 3: 0.739287372904644 0.04001407 没有“with = FALSE”它返回列标题的位置:[1] 3 1
好的。只是想指出你现在也可以投票:)
【参考方案1】:
我认为问题是你不能在 plotly 中使用变量选择器,就像 aes_string
函数在 ggplot2 中为你做的那样——至少你尝试过的方式。
可能有一种方法可以在情节中传递角色名称,但文档真的不是很好,我什么也找不到。
但是我确实完成了这项工作 - 这是可以接受的。
将绘图数据框放入局部变量 df。 使用要绘制的变量创建了两个新变量 xx 和 yy 使用layout
命令覆盖xaxis
和yaxis
标签。
这使output$graph
看起来像这样:
output$graph <- renderPlotly(
df <- plotdata()
df$xx <- df[[input$IndVar]]
df$yy <- df[[input$DepVar]]
plot<-plot_ly(df, x = ~xx, y = ~yy, mode = "markers", type = "scatter") %>%
layout( xaxis = list( title=input$IndVar),
yaxis = list( title=input$DepVar ) )
plot
)
产量:
注意:以下是我重新格式化和输入数据的方式,以防有人想要复制 - 大约需要 5 分钟:
AvgIR <- c(0.04106781,0.04036154,0.04001407,0.04034078,0.04055688,0.04007467 )
SumCount <-c(180029 ,160061 ,145560 ,147693 ,142545 ,176344 )
AvgLTV <-c(0.753180543134717 ,0.738038310394162 ,0.739287372904644 ,0.739693214979721 ,0.734515977410642 ,0.735780463185592 )
AvgGFEE<-c(0.002424778 ,0.002722529 ,0.002425912 ,0.002600640 ,0.002449523 ,0.002459228 )
AvgRTC <-c(319.6837,312.6314 ,313.0351 ,315.0238 ,310.3950 ,309.9615 )
Date <- c("2015-10-01","2015-11-01","2015-12-01","2016-01-01","2016-02-01","2016-03-01")
RegData2 <- data.frame(AvgIR=AvgIR,SumCount=SumCount,AvgLTV=AvgLTV,AvgGFEE=AvgGFEE,AvgRTC=AvgRTC,Date=Date)
RegData2$Date <- as.POSIXct(RegData2$Date)
【讨论】:
以上是关于将反应输入传递到 R Shiny 中的绘图轴的主要内容,如果未能解决你的问题,请参考以下文章
如何将 Shiny 应用程序中的表格和绘图作为参数传递给 R Markdown?
R Shiny:将多个连续的反应传递给 selectInput