【问题标题】:plot.CA() renders in shiny app locally but not when app is deployedplot.CA() 在本地应用闪亮的应用程序中呈现,但在部署应用程序时不呈现
【发布时间】:2018-04-11 14:02:41
【问题描述】:

我创建了一个 Shiny 应用程序,它接受用户输入并创建 CA 图。当我在本地运行应用程序时它工作得很好,但由于某种原因,当我部署仪表板时,绘图的图像不会出现。我可以在日志中看到,数据上传并重新格式化为适当的数据框工作正常,但绘图本身无法呈现。

有人知道为什么会这样吗?我在下面发布了我的代码(您会在我的代码中看到一些用于调试的 print() 行)。任何帮助将不胜感激!

    #PERCEPTUAL MAPPING DASHBOARD

library(FactoMineR)
library(factoextra)
library(SensoMineR)
library(shinythemes)
library(ca)



ui <- fluidPage(theme = shinytheme("darkly"),
  # Application title
  titlePanel("Perceptual Map Dashboard"),
  sidebarLayout(
    # Sidebar with a slider and selection inputs
    sidebarPanel(

      #Excel doc row and column names
      numericInput(inputId="startcol",label="Input start column of CSV file:",value="", min=1,max=10000),
      numericInput(inputId="endcol",label="Input end column of CSV file:",value="", min=1,max=10000),
      #Inputing brands and emotions
      br(),
      numericInput(inputId = "rownums",label = "How many emotions/characteristics are you evaluating?",value = "", min = 1,max = 10000),
      br(),
      h6("Note: Please enter brands and emotions/characteristics in the order that they appear in the excel document exported from Survey Gizmo."),
      textInput ( 'brands', 'List the brands included in your perceptual map (separated by commas):', value=""),
      textInput ( 'emotions', 'List the emotions/characteristics included in your perceptual map (separated by commas):', value=""),
      #Removing brands and emotions

      #Select graph type
      textInput(inputId="plottitle",label="Title your graph:"),
      #Upload Excel Grid
      fileInput(inputId = 'data', 'Upload CSV File',
                accept=c('.csv')),
      actionButton("go","Create Map")
    ),

    # Visual Output
    mainPanel(
      wellPanel(h4('Visual'),
                h5("Once your visual appears, just right click it to save it as a .png file.")),
      plotOutput(outputId = "plot",  width = "100%", height=500)
      # downloadButton("downloadPlot", "Download Visual")
    )
  )
)



server <- function(input,output){



  K <- eventReactive(input$go,{


      x <- read.csv(input$data$datapath, header = F)

      x[!is.na(x)] <- 1
      x[is.na(x)] <- 0
      x<-x[,as.numeric(input$startcol):as.numeric(input$endcol)]
      column.sums<-colSums(x)
      print(column.sums)
      pmd.matrix<-matrix(column.sums, byrow = T, nrow=as.numeric(input$rownums))
      pmd.df2<-as.data.frame(pmd.matrix)
      colnames(pmd.df2) = unlist(strsplit(as.character(input$brands),","))
      print(pmd.df2)
      row.names(pmd.df2)= unlist(strsplit(as.character(input$emotions),","))
      print(pmd.df2)
      pmd.df2[-nrow(pmd.df2),] 
      print(pmd.df2)
      fit <- CA(pmd.df2, graph=F)
      return(fit)


    })

  p <- eventReactive(input$go,{

      input$plottitle

  })

  output$plot<- renderPlot({

      plot.CA(K(), col.row = "blue", col.col="black",  cex=1, new.plot=T,
                    title=p())


  })

}

shinyApp(ui = ui, server = server)

【问题讨论】:

  • 我没有必要的包来测试这个。不过我可以建议一件事:不要在observeEvent 中使用renderPlot。相反,创建一个返回 pmd.df2eventReactive
  • 您好 Gregor,感谢您抽出宝贵时间进行审核。我尝试进行更改,但它似乎仍然不起作用。有没有可能是我做错了?
  • 您的修改看起来不错。我真的不能告诉你那里出了什么问题。您可能会尝试的一件事是使用png() 保存文件并使用renderImage 将其加载到用户界面中。这基本上是 renderPlot 在后台所做的,但这样您可以检查图像文件是否实际创建。
  • 所以,我现在尝试过的事情:1)从observeEvent 更改为eventReactive(如上所示)并将renderPlot 移到外面。 2) 将此文件保存为图像并调用图像 3) 将我的函数保存到名为“o”的变量中并在其后调用 print(o)。 #3 是我得到的最接近的。它的作用是显示一个与我要求的绘图尺寸相同的白框,但它显示为空白。还有其他想法吗?
  • 所以图像创建正确但renderImage({list(src = imagepath)}) 不起作用?我是否正确假设您在 ubuntu 服务器上使用 shiny-server

标签: image shiny rendering


【解决方案1】:

我给你的建议是检查这个问题是否特定于你的情节,或者plot.CA 是否一般不适用于闪亮。尝试“部署”(显然,您不使用网络服务器?)以下应用程序

library(FactoMineR)
library(shiny)

data(children)
res.ca <- CA(children, col.sup = 6:8, row.sup = 15:18)

shinyApp(
  fluidPage(plotOutput("plot")),
  function(input, output, sesison) {
    output$plot <- renderPlot({
      plot.CA(res.ca)
    })
  }
)

如果这确实有效,则您的模型可能有问题,或者ca 包和FactorMineR 包之间可能存在名称串通。

如果这不起作用,请尝试以下方法

## use same data/libraries as above

myfile <- tempfile(fileext = ".png")

shinyApp(
  fluidPage(imageOutput("plot")),
  function(input, output, sesison) {
    output$plot <- renderImage({
      png(file = myfile)
      plot.CA(res.ca)
      dev.off()
      list(src = myfile)
    }, deleteFile = FALSE)
  }
)

看看

  1. 应用现在是否可以运行
  2. myfile 是否被创建并包含合理的内容。

【讨论】:

  • 所以您提供的第一段代码有效。我会回去检查我的模型。不过我很困惑,因为我不仅之前使用相同的 CSV 和我的输入值列表(列和行的名称)使这个模型工作,而且该应用程序也可以在本地工作并且可以毫无问题地生成图像。直到它发布它才停止工作,这让我相信这个问题可能与 plot.CA() 函数在部署后无法识别reactiveValues 更相关。想法?顺便说一句,感谢您对此的帮助!
猜你喜欢
  • 2018-09-24
  • 2020-02-22
  • 1970-01-01
  • 2015-09-04
  • 1970-01-01
  • 1970-01-01
  • 2021-08-13
  • 2017-10-30
  • 2023-03-15
相关资源
最近更新 更多