问题描述
在看到关于 GGVIS 的演讲后,我一直在尝试创建我的第一个 Shiny/ggvis 应用程序,但没有成功.我的情节在 R 中工作,但是当我尝试将其迁移到 Shiny 应用程序中以在网络上显示时,我什么也得不到.单选按钮被显示出来,我可以告诉它似乎正在工作(我使用 rStudio/Shiny/Reactivity 教程中的表格进行了测试,但它似乎与我的 ggvis 情节不太匹配).我一直在关注 Rstudio 上的教程,并使用 ggvis 源代码中演示文件夹中的部分.我正在尝试创建一个简单的内核密度图,该图根据用户输入对数据进行子集化并显示分布.附件是我迄今为止所拥有的可重复示例.如果您能了解我在这里缺少什么,我将不胜感激.
After seeing a talk on GGVIS, I have been trying my hand unsuccessfully at creating my first Shiny/ggvis app. My plot works in R, but when I try to migrate it into a Shiny app for display on the web I get nothing. The radio buttons are displayed and from what I can tell seem to be working (I tested using the table from rStudio/Shiny/Reactivity tutorial, but it doesn't seem to place nice with my ggvis plot). I have been following the tutorials on Rstudio and using pieces from the demo folder in the ggvis source code. I am trying to create a simple kernel density plot that subsets data based on user input and displays the distribution. Attached is a reproducible example of what I have thusfar. I would appreciate any insight as to what I am missing here.
# 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"))
)
))
server.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)
})
为了跟进这个问题,我注意到如果我删除以下内容:
To follow up on this question, I have noticed that if I remove the following:
by_group(color)
和
stroke = ~color,
fill = ~color,
从我在 server.R 中创建图形的调用开始,图形运行(尽管没有多色分组,这正是我所追求的)并且单选按钮成功地选择了我的数据子集.如上所述,当我只是在 R 中运行图形时,不使用反应性子集功能,图形能够运行并通过钻石颜色改变笔触/填充.目前 Shiny 与 ggvis 交互中目前不支持此功能吗?或者我只是以某种方式误解了这个功能?这是我更新的代码,所以你可以看到我的结果:
from my call to create the graph in server.R, the graph runs (albeit without the multicolored groupings, which is what I was after in the first place) and the radio buttons successfully select my data subset. As mentioned, when I am just running the graph in R, without the use of the reactive subsetting function, the graph is able to run and vary the stroke/fill by diamond color. Is this feature currently not supported in Shiny to ggvis interaction at this time? Or am I just misinterpreting this function somehow? Here is my updated code, so you can see my result:
# 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"))
)
))
server.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)
})
推荐答案
进一步搜索表明这是 ggvis 的已知错误:https://github.com/rstudio/ggvis/issues/71
Further search would indicate that this is a known bug for ggvis: https://github.com/rstudio/ggvis/issues/71
这篇关于Shiny/ggvis 对子集绘图数据的反应性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!