闪亮R:无法显示剧情



我尝试使用R shiny来显示交互式绘图。我可以成功地制作GUI并发布,但是tabPanel中的plot没有显示任何内容,如下图所示。这是我用过的数据(已经下载到我的笔记本电脑里了)。

我认为问题可能是由我在服务器上预处理数据的方式引起的。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
)
)
)
)
))

我的代码在服务器。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)
}

})

})

错误在于tabPanel设置。value不是这个情节的正确论证。value"tabsetPanel报告选中该选项卡时应发送的值">(取自手册)。这意味着,value具有id的作用(就像tabsetPanel的id参数或plotOutputoutputId)。

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

最新更新