使戈维斯的情节与光泽反应

时间:2022-08-26 23:35:24

I have the following plot and I wish to make it interactive. More specifically, I'd like a slider for maximum supply to range between 60 and 120. I've read through the shiny and ggvis documentation and I can't get it to work. I've included an attempt below.

我有以下的情节,我想让它具有互动性。更具体地说,我想要一个最大供应的滑块,在60到120之间。我已经阅读了闪亮的和ggvis的文档,我不能让它工作。我在下面列出了一个尝试。

library(ggvis)
library(dplyr)
library(tidyr)

maximum_supply = input$maximum_supply

supply.function = function(supply)
  60 - 60/maximum_supply * supply

data_frame(quantity = 0:maximum_supply) %>%
  mutate(price = supply.function(quantity)) %>%
  ggvis(~quantity, ~price) %>%
  layer_lines()

shiny.R

shiny.R

library(shiny)
library(ggvis)
library(dplyr)

shinyServer(function(input, output) {

  reactive({ 
    maximum_supply = 120

    supply.function = function(supply)
      60 - 60/maximum_supply * supply

    data_frame(quantity = 0:maximum_supply) %>%
      mutate(price = supply.function(quantity)) %>%
      ggvis(~quantity, ~price) %>%
      layer_lines() %>%
      bind_shiny("ggvis", "ggvis_ui")
  })
})

ui.R

ui.R

library(shiny)
library(ggvis)
library(dplyr)

shinyUI(fluidPage(

  titlePanel("Example"),

  sidebarLayout(
    sidebarPanel(
      sliderInput("maximum_supply",
                  "Maximum Supply",
                  min = 60,
                  max = 120,
                  value = 90),
    ),

    mainPanel(
      uiOutput("ggvis_ui"),
      ggvisOutput("ggvis")
    )
  )
))

1 个解决方案

#1


2  

Making the data that you pass to ggvis reactive is the key here, since you want to be able to subset it in response to user input. Also, the maximum_supply is part of input and can be accessed via input$maximum_supply in the supply function and the reactive data.

将传递给ggvis反应式的数据放在这里是关键,因为您希望能够根据用户输入对其进行子集化。而且,maximum_supply是输入的一部分,可以通过在supply函数和反应数据中输入$maximum_supply进行访问。

library(ggvis)
library(shiny)
library(dplyr)

shinyApp(
    shinyUI(fluidPage(
        titlePanel("Example"),
        sidebarLayout(
            sidebarPanel(
                sliderInput("maximum_supply",
                            "Maximum Supply",
                            min = 60,
                            max = 120,
                            value = 90),
                uiOutput("ggvis_ui")
            ),
            mainPanel(
                ggvisOutput("ggvis")
            )
        )
    )),
    shinyServer(function(input, output) {
        supply.function <- function(supply) {
            60 - 60/input$maximum_supply* supply
        }

        dat <- reactive({ data.frame( quantity = 0:input$maximum_supply ) })

        dat %>%
          mutate(price = supply.function(quantity)) %>%
          ggvis(~quantity, ~price) %>%
          layer_lines() %>%
          bind_shiny("ggvis", "ggvis_ui")
    })
)

#1


2  

Making the data that you pass to ggvis reactive is the key here, since you want to be able to subset it in response to user input. Also, the maximum_supply is part of input and can be accessed via input$maximum_supply in the supply function and the reactive data.

将传递给ggvis反应式的数据放在这里是关键,因为您希望能够根据用户输入对其进行子集化。而且,maximum_supply是输入的一部分,可以通过在supply函数和反应数据中输入$maximum_supply进行访问。

library(ggvis)
library(shiny)
library(dplyr)

shinyApp(
    shinyUI(fluidPage(
        titlePanel("Example"),
        sidebarLayout(
            sidebarPanel(
                sliderInput("maximum_supply",
                            "Maximum Supply",
                            min = 60,
                            max = 120,
                            value = 90),
                uiOutput("ggvis_ui")
            ),
            mainPanel(
                ggvisOutput("ggvis")
            )
        )
    )),
    shinyServer(function(input, output) {
        supply.function <- function(supply) {
            60 - 60/input$maximum_supply* supply
        }

        dat <- reactive({ data.frame( quantity = 0:input$maximum_supply ) })

        dat %>%
          mutate(price = supply.function(quantity)) %>%
          ggvis(~quantity, ~price) %>%
          layer_lines() %>%
          bind_shiny("ggvis", "ggvis_ui")
    })
)