gpt4 book ai didi

r - Shiny/ggvis 对子集图数据的 react

转载 作者:行者123 更新时间:2023-12-02 04:27:21 33 4
gpt4 key购买 nike

在看过 GGVIS 上的演讲后,我一直在尝试创建我的第一个 Shiny/ggvis 应用程序,但没有成功。我的情节在 R 中有效,但是当我尝试将其迁移到 Shiny 应用程序以在网络上显示时,我什么也没得到。单选按钮已显示,据我所知似乎正在工作(我使用 rStudio/Shiny/Reactivity 教程中的表格进行了测试,但它似乎与我的 ggvis 图不太匹配)。我一直在关注 Rstudio 上的教程并使用 ggvis 源代码中 demo 文件夹中的片段。我正在尝试创建一个简单的核密度图,该图根据用户输入对数据进行子集化并显示分布。附件是我迄今为止所拥有的可重现的示例。如果您能了解我在这里缺少的内容,我将不胜感激。

ui.R

# 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 中运行图形时,在不使用 react 子集函数的情况下,图形能够运行并通过菱形颜色改变描边/填充。目前Shiny与ggvis交互中是否不支持此功能?或者我只是以某种方式误解了这个功能?这是我更新的代码,因此您可以看到我的结果:

ui.R

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

最佳答案

进一步搜索表明这是 ggvis 的一个已知错误:https://github.com/rstudio/ggvis/issues/71

关于r - Shiny/ggvis 对子集图数据的 react ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22312767/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com