输入输出如何连接?

问题描述 投票:0回答:1

我不知道如何连接滑块中的输入(n)和输出(绘图)。当我移动闪亮应用程序中滑块的底部时,绘图没有改变。我想知道输出和输入是否没有正确链接在一起。

library(ggplot2)
library(shiny)

plot<-function(x,y,xlim=c(-3,3)){
  x <- seq(-4, 4, by=0.01)
  norm_dens <- dnorm(x)
  t_dens <- dt(x, df = n-1)
  df = data.frame(x = x, z = norm_dens, t = t_dens)
  ggplot(data = df, aes(x)) +
    geom_line(aes(y = z, colour = "z"))+
    geom_line(aes(y = t,color = "t"))+
    labs(x="x", y = "")+
    scale_color_manual(name = "l", values = c("z" = "blue", "t" = "red"))+
    coord_cartesian(xlim = xlim)
}
plot(x,y)
## UI function
ui <- fluidPage(
  mainPanel(
    plotOutput(outputId="plot")),
  fluidRow(
    column(2,
           "Sample Size",
           sliderInput("n", label = "n", value = 5, min = 2, max = 100),step=1)
  )
)
# Server logic
server <- function(input, output) {
  reactive({
    df %>%
      filter(n %in% input$n)
  })
  output$plot<-renderPlot({
    
    plot(x,y)
  })
}

## Run shiny app
shinyApp(ui, server)
r shiny
1个回答
0
投票

这里不需要反应式。试试这个:

library(ggplot2)
library(shiny)

plot<-function(x,y,xlim=c(-3,3),n){
    x <- seq(-4, 4, by=0.01)
    norm_dens <- dnorm(x)
    t_dens <- dt(x, df = n-1)
    df = data.frame(x = x, z = norm_dens, t = t_dens)
    ggplot(data = df, aes(x)) +
        geom_line(aes(y = z, colour = "z"))+
        geom_line(aes(y = t,color = "t"))+
        labs(x="x", y = "")+
        scale_color_manual(name = "l", values = c("z" = "blue", "t" = "red"))+
        coord_cartesian(xlim = xlim)
}

## UI function
ui <- fluidPage(
    mainPanel(
        plotOutput(outputId="plot")),
    fluidRow(
        column(2,
               "Sample Size",
               sliderInput("n", label = "n", value = 5, min = 2, max = 100),step=1)
    )
)
# Server logic
server <- function(input, output) {

    output$plot<-renderPlot({
        req(input$n)
        plot(x,y,n=input$n)
    })
}

## Run shiny app
shinyApp(ui, server)`enter code here`
© www.soinside.com 2019 - 2024. All rights reserved.