简体   繁体   English

Shiny R:无法显示情节

[英]Shiny R: Can't display plot

I try to display interactive plots by using R shiny.我尝试使用 R 闪亮显示交互式绘图。 I can successfully make the GUI and published, but the plots in tabPanel shows nothing, just like the picture shows below.我可以成功制作GUI并发布,但是tabPanel中的图表没有显示,就像下图所示。 There is the data I used (have been downloaded into my laptop).我使用数据(已下载到我的笔记本电脑中)。

I think problem may caused by the way how I preprocessing my data in server.R, but whatever I tried, it still display nothing.我认为问题可能是由我在 server.R 中预处理数据的方式引起的,但是无论我尝试过什么,它仍然没有显示任何内容。 No Error shows when I run the app.运行应用程序时没有错误显示。

enter image description here在此处输入图片说明

My code in ui.R:我在 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
         )
       )
     )
   )
  ))

My code in server.R:我在 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)
    }
      
  })
  
})

The mistake lies in the tabPanel setup.错误在于tabPanel设置。 value is not the correct argument for the plot. value不是该图的正确参数。 value is "the value that should be sent when tabsetPanel reports that this tab is selected" (taken from the manual). value“当tabsetPanel报告选择此选项卡时应发送的值” (取自手册)。 That means, value has the role of an id (like id argument of tabsetPanel or outputId of plotOutput ).这意味着, value具有 id 的作用(如tabsetPanel id 参数或outputIdplotOutput )。

Remove value = to make it work (the code snippet below gave me an output on my system).删除value =使其工作(下面的代码片段在我的系统上给了我一个输出)。

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
   )
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM