我希望创建一个Shiny应用程序,能够显示交互式图表标题(根据所选的x轴数值而定)。
以下是一个非常简单的示例:
library(shiny)
library(DT)
library(ggplot2)
x <- as.numeric(1:1000000)
y <- as.numeric(1:1000000)
z <- as.numeric(1:1000000)
data <- data.frame(x,y, z)
shinyApp(
ui = fluidPage(selectInput(inputId = "yaxis",
label = "Y-axis",
choices = list("x","y","z"),
selected = c("x")),
dataTableOutput('tableId'),
plotOutput('plot1')),
server = function(input, output) {
output$tableId = renderDataTable({
datatable(data, options = list(pageLength = 10, lengthMenu=c(10,20,30)))
})
output$plot1 = renderPlot({
filtered_data <- data[input$tableId_rows_all, ]
ggplot(data=filtered_data, aes_string(x="x",y=input$yaxis)) + geom_line()
})
}
)
我尝试了这段代码:
ggtitle("Line plot of x vs",input$yaxis)
它不起作用,图表没有显示,给了我一个错误:
Warning: Error in ggtitle: unused argument (input$yaxis)
[重要]
使用ggtitle(input$yaxis)
可以为我提供一个交互式标题,但我需要构建一个句子(例如:x与input$yaxis
的折线图),其中动态参数(input$yaxis
)是其一部分!
感谢您的任何帮助!
干杯
ggtitle(paste("Line plot of x vs", input$yaxis))
来设置图表标题,其中"Line plot of x vs"是固定的部分,而input$yaxis
是根据用户输入的y轴标签动态生成的。 - Axeman