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