我对此很陌生,所以请原谅我 - 我正在 Shiny 中构建一个应用程序,它采用固定格式的上传数据文件,并根据用户选择呈现各种图。该应用程序的一个重要部分是一组选项卡,每个选项卡都包含一个绘图。
我正在使用 renderUI 来渲染选项卡,但在渲染绘图时遇到了很多困难。我已经尝试了 renderPlot、outputPlot 并且无法显示绘图。我收到各种错误,但下面的代码在我们渲染图(engPlot)的地方生成“as.character(x)中的错误:无法将类型'closure'强制转换为'character'类型的向量”。
用户界面
# Define UI
shinyUI(fluidPage(
# Application title
titlePanel("Chart Creation Tool"),
# Sidebar
sidebarLayout(
sidebarPanel(
fileInput("fileBlob", "Upload File", multiple = FALSE, accept = NULL),
selectInput("selectAnalysis", label=h3("Select Input"), choices=c("Month x Year", "Strategies", "Programs", "Segments")),
uiOutput("strategyList")
),
# Show a plot of the generated distribution
mainPanel(
uiOutput("mainPanel")
)
)
))
server.R 的 mainPanel 部分
output$mainPanel <- renderUI ({
if (length(RawImport())==0L) {
out <- NULL
}else{
if (input$selectAnalysis=="Month x Year") {
dfAggMonth <- aggregate(cbind(Sent,Delivered,UniqueOpens,Responders,Bounced,Unsubscribes,TotalSpamComplaints,HardBounces,SoftBounces) ~ SentMonth + SentYear + SentMonthName, RawImport(), FUN = sum)
dfAggMonth <- addRatios(dfAggMonth)
dfAggMonth <- dfAggMonth[with(dfAggMonth, order(Date)), ]
engPlot <- runplot(paste(dfAggMonth$SentMonthName, dfAggMonth$SentYear,sep="-"), dfAggMonth$Date, dfAggMonth$Delivered, dfAggMonth$UniqueOpenRate, dfAggMonth$ResponderRate, "engagement", , "Temp Title")
out <- tabsetPanel(
tabPanel("Engagement", "Engagement", renderPlot(engPlot)),
tabPanel("Summary", "summary", "summary"),
tabPanel("Deliverability",runplot(paste(dfAggMonth$SentMonthName, dfAggMonth$SentYear,sep="-"), dfAggMonth$Date, dfAggMonth$Delivered, dfAggMonth$BounceRate, , "deliverability", , "Temp Title"))
)
}
else {
out <- tabsetPanel(
tabPanel("Tab 1", input$selectAnalysis),
tabPanel("Tab 2", input$selectAnalysis)
)
}
}
out
})
运行图函数
runplot <- function(xSeriesLabels, xSeriesValues, leftseries, rightseries1, rightseries2, chartType, columnSeries, xTitle){
if (missing(xTitle)==FALSE) {
strTitle <- xTitle
} else {
strTitle <- "no title supplied"
}
p <- barplot(leftseries, width=1, col=barCol, axisnames = leftseries, names.arg=xSeriesLabels, axis.lty=1, xlab=strTitle)
return(p)
}