R Shiny应用tabPanel无报错但无法显示plot绘图问题求助
问题根源
代码的核心问题出在ui.R中tabPanel的参数使用错误:tabPanel的value参数是可选的、用于给标签页赋值以便程序识别选中状态,不是用来放置标签页内容的,你将plotOutput赋值给了value,导致绘图组件没有被加载到页面中,自然不会显示任何内容。
修复方案
1. 修正ui.R代码
删除tabPanel中包裹plotOutput的value =前缀,直接将绘图输出作为标签页的内容参数传入,修正后的完整ui.R代码如下:
library(shiny) shinyUI(fluidPage( titlePanel("Data Viz Lab"), sidebarLayout( sidebarPanel( ## Add X-Variable select element selectInput(inputId = "var_x", label = h5("X-Variable"), choices = c("Structure.Cost", "Land.Value", "Home.Value", "Home.Price.index"), selected = "Land.Value"), ## Add Fill Color select element selectInput(inputId = "color", label = h5("Fill Color"), choices = c("brown", "yellow", "green", "blue", "red"), selected = "brown"), ## Add log-scale check box checkboxInput(inputId = "log", label = "log-sclae for X-variable in Scatterplot?", value = FALSE), ## Add Y-Variable select element selectInput(inputId = "var_y", label = h5("Y-Variable"), choices = c("Structure.Cost", "Land.Value", "Home.Value", "Home.Price.index"), selected = "Structure.Cost"), ## Add Circle-Size side bar sliderInput(inputId = "size", label = h5("Circle-Size"), min = 1, max = 10, value = 3), ## Add Outlier color select element selectInput(inputId = "color_out", label = h5("Outlier Color"), choices = c("white", "yellow", "green", "blue", "red"), selected = "white") ), mainPanel( tabsetPanel( # Establish tabset panel tabPanel( # Tab1 title = "Histogram", plotOutput(outputId = "hist") # 移除了value=前缀 ), tabPanel( # Tab2 title = "Scatterplot", plotOutput(outputId = "scatter") # 移除了value=前缀 ) ) ) ) ))
2. 优化server.R冗余逻辑
避免每次渲染散点图时重复修改全局数据集导致的列名冲突,同时给直方图增加bins参数避免默认警告,修正后的完整server.R代码如下:
library(shiny) library(ggplot2) library(sp) library(dplyr) # setwd() landdata = read.csv("landdata.csv") options(scipen = 999) shinyServer(function(input, output) { ## Plotting Histogram output$hist = renderPlot({ # Plotting if (input$log == FALSE){ ggplot(landdata, aes_string(x = input$var_x)) + geom_histogram(color = input$color, bins = 30) # 新增bins参数避免警告 }else{ ggplot(landdata, aes_string(x = input$var_x)) + geom_histogram(color = input$color, bins = 30) + scale_x_log10(input$var_x) } }) ## Plotting Scatter plot output$scatter = renderPlot({ # Data pre-processing p = ggplot(data = landdata, aes_string(x = input$var_x, y = input$var_y)) + geom_point() + stat_ellipse(type = "norm", level = 0.95, color = "black") build = ggplot_build(p)$data pts = build[[1]] elli = build[[2]] Outlier = point.in.polygon(pts$x, pts$y, elli$x, elli$y) # 改用临时变量存储,不修改全局landdata避免列重复 tmp_landdata = cbind(landdata, Outlier) tmp_landdata$Outlier = ifelse(tmp_landdata$Outlier == 0, yes = "Y", no = "N") %>% factor(level = c("Y", "N")) # Plotting if (input$log == FALSE){ ggplot(tmp_landdata, aes_string(x = input$var_x, y = input$var_y)) + geom_point(aes(color = Outlier), size = input$size) + scale_color_manual(values = c(input$color, input$color_out)) }else{ ggplot(tmp_landdata, aes_string(x = input$var_x, y = input$var_y)) + geom_point(aes(color = Outlier), size = input$size) + scale_color_manual(values = c(input$color, input$color_out)) + scale_x_log10(input$var_x) } }) })
修改后重新运行应用即可正常展示两个标签页的绘图内容。
内容的提问来源于stack exchange,提问作者HsunHung Chu
相关产品推荐
相关产品推荐

