我正在创建一个简单的折线图,可以在Shiny中正确呈现.
我现在添加了一个selectInput,其名称为2个不同的度量,写在我的数据集中.我希望我的y变量能够相应地改变.
p <- plot_ly(data = LineChartData(), x= Calendar.Month, y = input$Measure, type = "line", group = Calendar.Year, col = Calendar.Year)
Run Code Online (Sandbox Code Playgroud)
不幸的是,图表只有一点呈现.它不是在输入$ Measure并在我的数据集中找到该字段.
我知道在使用ggplot时,我会将我的aes切换为aes_string.在情节上是否有类似的解决方案?
编辑:这是一些可重现的代码
这是ui.R文件
#ui.R
shinyUI(
fluidPage(
titlePanel("Inbound Intermediary Performance"),
sidebarLayout(
sidebarPanel(
h4("Parameters"),
br(),
selectInput("Measure", "Measure", c("Var1","Var2"))
),
mainPanel(
plotlyOutput("lineChart")
)
)
)
)
Run Code Online (Sandbox Code Playgroud)
server.R
#server.R
library(plotly)
library(shiny)
library(ggplot2)
#Create data
data <- data.frame(Month = c(1,2,3,4,5,6,7,8,9,10,11,12), Var1 = c(36,33,30,27,24,21,18,15,12,9,6,3), Var2 = c(4,8,12,16,20,24,28,32,36,40,44,48))
shinyServer(function(input, output) {
#Create plot
output$lineChart <- renderPlotly({
#using ggplot
p <- ggplot(data=data, aes_string(x='Month', y = …Run Code Online (Sandbox Code Playgroud) 我很难理解这一点。
下面让我以“整洁”的方式过滤我的 data.frame,并使用plotly 绘制一个图。在本例中,我使用plotly基于公式的API来说明要使用数据框的哪些列:
library(plotly)
tidy_filter = function(data, x) {
x = enquo(x)
filter(data, !!x > 5)
}
mtcars %>%
tidy_filter(wt) %>%
plot_ly(x = ~wt, y = ~wt)
Run Code Online (Sandbox Code Playgroud)
我可以将其包装在一个函数中以获得相同的结果:
tidy_ply = function(data, x) {
x = enquo(x)
data = filter(data, !!x > 5)
plot_ly(data, x = x, y = x)
}
tidy_ply(mtcars, wt)
Run Code Online (Sandbox Code Playgroud)
现在:
我认为enquo(x)在这种情况下至少部分相当于~wt因为这就是它的工作原理。但它们是两个不同的东西(定量VS公式)。它们之间是什么关系,为什么上面的方法有效?
plotly 的公式 API 的优点是,如果我想操纵输入值,我可以做类似~wt/2. 但在上面的操作中,plot_ly(data, x = x, y = x/2)会产生错误。有办法让这项工作发挥作用吗?
我想普遍的问题是如何最好地将整洁的评估方法与情节的公式方法结合起来?