1

我对此很陌生,所以请原谅我 - 我正在 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)
}
4

1 回答 1

2

我发现当您运行一个本应server.Rui.R.

因此,您必须记住,这renderUI将创建一个成熟的客户端 UI。这意味着您在动态 UI 中返回的任何代码(即使它恰好存储在server.R其中也必须是可能存在的有效客户端代码ui.R。这也意味着测试可以更容易,因为您可以移动您尝试动态创建的代码(例如其中一个outtabsetPanels)以ui.R查看它是否有效。这可能会使将来的调试更容易。

在这种情况下,我认为如果你这样做了,你会看到的问题是在renderPlot通话中。由于您只是在此处创建动态 UI,因此您希望使用与 UI 兼容的函数,plotOutput("plotName")在本例中为 。然后在服务器端,您将为该输出分配一个绘图。

output$plotName <- renderPlot({
 ...
})

当绘图可见时,它将执行您想要的操作,并且在未显示绘图时将没有可见的效果。

于 2014-04-07T14:45:08.733 回答