在看到关于 GGVIS 的演讲后,我一直在尝试创建我的第一个 Shiny/ggvis 应用程序,但没有成功。我的情节在 R 中有效,但是当我尝试将其迁移到 Shiny 应用程序以在网络上显示时,我什么也没得到。显示单选按钮,据我所知,它似乎正在工作(我使用 rStudio/Shiny/Reactivity 教程中的表格进行了测试,但它似乎与我的 ggvis 图不太合适)。我一直在关注 Rstudio 上的教程并使用 ggvis 源代码中演示文件夹中的片段。我正在尝试创建一个简单的内核密度图,该图根据用户输入对数据进行子集化并显示分布。附件是我迄今为止所拥有的一个可重复的例子。对于我在这里缺少的内容,我将不胜感激。
用户界面
# clear memory & load packages
rm(list=ls())
library(shiny)
library(ggvis)
# Define UI for distribution application
shinyUI(fluidPage(
# Application title
titlePanel("Diamond Carats by Color/Cut"),
# Sidebar with controls to select subset
sidebarLayout(
sidebarPanel(
radioButtons("cut", "Diamond Cut:",
c("Ideal" = "IDEAL",
"Premium" = "IDEAL",
"Good" = "GOOD",
"Very Good" = "VGOOD"))
),
# Display your plot created by GGvis
mainPanel(ggvis_output("my_plot"))
)
))
服务器.R
# clear memory & load packages
rm(list=ls())
library(shiny)
library(ggvis)
# Define server logic for distribution application
shinyServer(function(input, output, session) {
# load your data
dataset <- diamonds
# Reactive expression to generate the subset.
datasetInput <- reactive({
selection <-switch(input$cut,
IDEAL = "Ideal",
PREM = "Premium",
GOOD = "Good",
VGOOD = "Very Good")
subset(dataset, cut == selection)
})
# Generate your plot using GGvis and your reactive inputs
gv <- reactive({
ggvis(datasetInput(), by_group(color),
props(x = ~carat,
stroke = ~color,
fill = ~color,
fillOpacity := 0.2,
fillOpacity.hover := 0.7)) +
layer_density()
})
# necessary additions for ggvis integration to shiny
output$controls <- renderControls(gv)
observe_ggvis(gv, "my_plot", session)
})
编辑:为了跟进这个问题,我注意到如果我删除以下内容:
by_group(color)
和
stroke = ~color,
fill = ~color,
从我在 server.R 中创建图形的调用中,图形运行(尽管没有多色分组,这是我首先追求的)并且单选按钮成功选择了我的数据子集。如前所述,当我只是在 R 中运行图形时,不使用反应子集功能,图形能够运行并通过菱形颜色改变笔触/填充。目前 Shiny 到 ggvis 交互中目前不支持此功能吗?还是我只是以某种方式误解了这个功能?这是我更新的代码,所以你可以看到我的结果:
用户界面
# clear memory & load packages
rm(list=ls())
library(shiny)
library(ggvis)
# Define UI for distribution application
shinyUI(fluidPage(
# Application title
titlePanel("Diamond Carats by Color/Cut"),
# Sidebar with controls to select subset
sidebarLayout(
sidebarPanel(
radioButtons("cut", "Diamond Cut:",
c("Ideal" = "IDEAL",
"Premium" = "IDEAL",
"Good" = "GOOD",
"Very Good" = "VGOOD"))
),
# Display your plot created by GGvis
mainPanel(ggvis_output("my_plot"))
)
))
服务器.R
# clear memory & load packages
rm(list=ls())
library(shiny)
library(ggvis)
# Define server logic for distribution application
shinyServer(function(input, output, session) {
# load your data
dataset <- diamonds
# Reactive expression to generate the subset.
datasetInput <- reactive({
selection <-switch(input$cut,
IDEAL = "Ideal",
PREM = "Premium",
GOOD = "Good",
VGOOD = "Very Good")
subset(dataset, cut == selection)
})
# Generate your plot using GGvis and your reactive inputs
gv <- reactive({
ggvis(datasetInput(),
props(x = ~carat,
fillOpacity := 0.2,
fillOpacity.hover := 0.7)) +
layer_density()
})
# necessary additions for ggvis integration to shiny
output$controls <- renderControls(gv)
observe_ggvis(gv, "my_plot", session)
})