【问题标题】:Shiny R: Can't display plotShiny R:无法显示情节
【发布时间】:2021-12-03 16:57:06
【问题描述】:

我尝试使用 R shiny 来显示交互式绘图。我可以成功制作 GUI 并发布,但 tabPanel 中的图没有显示,如下图所示。有data I used(已下载到我的笔记本电脑)。

我认为问题可能是由我在 server.R 中预处理数据的方式引起的,但无论我尝试了什么,它仍然没有显示任何内容。运行应用程序时没有显示错误。

enter image description here

我在 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",
          value = plotOutput(outputId = "hist")  # Add an figure in tab1
        ),
        tabPanel(
          # Tab2
          title = "Scatterplot",
          value = plotOutput(outputId = "scatter")  # Add an figure in tab2
         )
       )
     )
   )
  ))

我在 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)
    }else{
      ggplot(landdata, aes_string(x = input$var_x)) +
        geom_histogram(color = input$color) +
        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 = cbind(landdata, Outlier)
    landdata$Outlier = ifelse(landdata$Outlier == 0, yes = "Y", no = "N") %>% factor(level = c("Y", "N"))
    
    # Plotting
    if (input$log == FALSE){
      ggplot(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(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)
    }
      
  })
  
})

【问题讨论】:

    标签: r ggplot2 shiny


    【解决方案1】:

    错误在于tabPanel 设置。 value 不是情节的正确论据。 value“当tabsetPanel 报告此选项卡被选中时应发送的值”(摘自手册)。这意味着,value 具有 id 的作用(如 tabsetPanelplotOutputoutputId 的 id 参数)。

    删除 value = 以使其工作(下面的代码 sn-p 在我的系统上给了我一个输出)。

    tabsetPanel(  # Establish tabset panel
       tabPanel(
          # Tab1
          title = "Histogram",
          plotOutput(outputId = "hist")  # Add an figure in tab1
       ),
       tabPanel(
          # Tab2
          title = "Scatterplot",
          plotOutput(outputId = "scatter")  # Add an figure in tab2
       )
    )
    
    

    【讨论】:

    • 非常感谢您解释我的错误和代码!
    猜你喜欢
    • 2021-08-12
    • 1970-01-01
    • 2019-11-22
    • 2015-10-18
    • 2020-07-13
    • 1970-01-01
    • 2016-07-01
    • 2013-06-28
    • 1970-01-01
    相关资源
    最近更新 更多