我正在尝试在R Shiny中创建动态图,但该图未显示在仪表板中。
我有以下测试数据,这些数据将上传到闪亮的应用程序中
:Region.1=c( 375.00,375.00,370.00,350.00,350.00,305.00,300.00,250.00,245.00,240.00,235.00,225.00,215.00,200.00,100.00,100.00,100.00,100.00,100.00,100.00)
Region.2 =c(0.00,0.00,0.00,0.00,0.00,200.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,20.00,500.00,235.00,0.00)
Region.3 =c(100000.00,76000.00,60000.00,50000.00,50000.00,30000.00,30000.00,26000.00,19000.00,11000.00,10000.00,8000.00,7000.00,5000.00,4070.00,4000.00,3660.00,2585.00,2550.00,250.00)
Type=c("B","F","F","B","B","A","A","F","F","B","A","B","F","B","F","F","A","A","F","B")
data <- data.frame("Region.1"=Region.1, "Region.2"=Region.2, "Region.3"=Region.3, "Type"=Type)
“我的闪亮脚本”看起来像这样(简化代码)
function(input, output,session) {
myData <- reactive({
req(input$file)
inFile <- input$file
if(is.null(inFile)){return(NULL)}
read.csv(inFile$datapath, header = TRUE, sep = input$sep,
quote = '""')
})
df<-reactive({
df<-myData()
dfType <- df[ , grepl( "Type" , names( df ) ) ]
dfRegion<-df[ , grepl( "Region" , names( df ) ) ]
df4<-cbind(dfType, dfRegion)
colnames(df4)[which(names(df4) == "dfType")] <- "Type"
colnames(df4)[which(names(df4) == "dfRegion")] <- "Region"
df4[df4==""] <- NA
df4 <- na.omit(df4)
return(df4)
})
df.datasets <- reactive({
df<-df()
df<- melt(df,id.vars=c("Type"),
measure.vars=c("Region.1", "Region.2", "Region.3"))
test<-aggregate(df$value, by=list(Region=df$variable, Type=df$Type), FUN=sum)
test<-test %>% group_by(Region) %>% transmute(Type, percent = (x/sum(x))*100)
test$percent<-round(test$percent, digits = 0)
test[test==0] <- NA
test<-test[complete.cases(test),]
})
output$plot1<-renderPlot({
blank_theme <- theme_minimal()+
theme(
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.border = element_blank(),
panel.grid=element_blank(),
axis.ticks = element_blank(),
plot.title=element_text(size=14, face="bold")
)
test<-df.datasets()
levels(test$"Region")[levels(test$"Region") %in% c("Region.1")] <- "Region 1"
levels(test$"Region")[levels(test$"Region") %in% c("Region.2")] <- "Region 2"
levels(test$"Region")[levels(test$"Region") %in% c("Region.3")] <- "Region 3"
levels(test$"Region")[levels(test$"Region") %in% c("Region.4")] <- "Region 4"
p<-ggplot(test,
aes(x =" ", y = percent, fill = Type))+
geom_bar(width = 1, stat = "identity",position="fill", color="grey35")+
coord_polar("y")+
facet_grid(.~Region)+
geom_text(aes( label= paste(percent, "%")) ,
position=position_fill(0.5), size=4.5)+
theme(axis.text.x=element_blank(),
strip.text.x = element_text(size=15, color="black", face="bold"),
strip.text.y = element_text(size=15, color="black", face="bold"),
legend.text = element_text(size=15),
legend.title = element_text(size=15, face="bold"),
plot.title = element_text(hjust = 0.5, size = 25),
axis.text=element_text(size=10))
})
}
myData <- NULL
sidebar <- dashboardSidebar(
sidebarMenu(
fileInput('file', 'Upload File (.csv | .txt)',
accept=c('text/csv',
'text/comma-separated-values,text/plain',
'.csv')),
radioButtons('sep', 'File Type',
c(CSV=';',
Text='\t'))
#actionButton("goButton", " Legenda", icon = icon("play-circle"))
)
)
dashboardPage(
dashboardHeader(title = "Overview"),
sidebar,
dashboardBody(tags$style("image {max-width: 100%; width: 100%; height: auto}"),
fluidRow(
tabBox(
title = " ",
# The id lets us use input$tabset1 on the server to find the current tab
id = "tabset1",
height = "1200px", width = "1000px"
,
tabPanel( "Plots",
column(12,
plotOutput("plot1"))
)
)
)))
我试图精简代码以检测错误,但找不到。当我在Shiny之外运行代码时,该图确实显示为: 我也没有收到任何错误消息。因此,我认为该情节正在运行,但不知何故。我尝试修改列的大小,但这没用。
有人知道吗?我真的很感激,谢谢:)
答案 0 :(得分:0)
似乎您已将图分配给p,然后在renderPlot块中从未调用过p,如果仅在块末添加p会发生什么情况?