Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 渲染引用同一数据集的2个输出_R_Shiny - Fatal编程技术网

R 渲染引用同一数据集的2个输出

R 渲染引用同一数据集的2个输出,r,shiny,R,Shiny,我在下面构建了一个闪亮的应用程序,根据用户的输入更新折线图。在我尝试生成第二个输出之前,它工作正常。如何显示在renderPlot()函数中计算的值total.weight?在我看来,我的数据集df和变量total.weight应该在renderPlot()函数的“外部”创建,但我还没有弄清楚如何创建 ui.r library(shiny) shinyUI(fluidPage( # Application title titlePanel("Reproducible Example"

我在下面构建了一个闪亮的应用程序,根据用户的输入更新折线图。在我尝试生成第二个输出之前,它工作正常。如何显示在
renderPlot()
函数中计算的值
total.weight
?在我看来,我的数据集
df
和变量
total.weight
应该在
renderPlot()
函数的“外部”创建,但我还没有弄清楚如何创建

ui.r

library(shiny)

shinyUI(fluidPage(

  # Application title
  titlePanel("Reproducible Example"),

  # Sidebar with a slider input for the number of bins
  sidebarLayout(
    sidebarPanel(
      selectInput("ID", "group", c("A", "B"))
    ),

    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("plot1"),
      verbatimTextOutput("text1")
    )
  )
))
library(shiny)
library(ggplot2)

shinyServer(function(input, output) {

  output$plot1 <- renderPlot({

    years <- c(1:10)
    weight <- c(5,7,9,11,12,17,19,20,21,22)
    group <- c(rep("A",5), rep("B",5))
    df <- data.frame(years,weight,group)

    df <- subset(df, group == input$ID)
    total.weight <- sum(df$weight)

    #Plot
    ggplot(data=df, aes(x=df$years, y=df$weight)) +
      geom_line() +
      geom_point() 



  })

  output$text1 <- renderText({total.weight})

})
server.r

library(shiny)

shinyUI(fluidPage(

  # Application title
  titlePanel("Reproducible Example"),

  # Sidebar with a slider input for the number of bins
  sidebarLayout(
    sidebarPanel(
      selectInput("ID", "group", c("A", "B"))
    ),

    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("plot1"),
      verbatimTextOutput("text1")
    )
  )
))
library(shiny)
library(ggplot2)

shinyServer(function(input, output) {

  output$plot1 <- renderPlot({

    years <- c(1:10)
    weight <- c(5,7,9,11,12,17,19,20,21,22)
    group <- c(rep("A",5), rep("B",5))
    df <- data.frame(years,weight,group)

    df <- subset(df, group == input$ID)
    total.weight <- sum(df$weight)

    #Plot
    ggplot(data=df, aes(x=df$years, y=df$weight)) +
      geom_line() +
      geom_point() 



  })

  output$text1 <- renderText({total.weight})

})
库(闪亮)
图书馆(GG2)
shinyServer(功能(输入、输出){

输出$plot1快速解决方法是将总重量放入全局变量中:

total.weight <<- sum(df$weight)

快速解决方法是将总重量放入全局变量中:

total.weight <<- sum(df$weight)

您还可以创建反应:

服务器.R

library(shiny)
    library(ggplot2)

    shinyServer(function(input, output) {

            df <- reactive({
                    years <- c(1:10)
                    weight <- c(5,7,9,11,12,17,19,20,21,22)
                    group <- c(rep("A",5), rep("B",5))
                    df <- data.frame(years,weight,group)

                    df <- subset(df, group == input$ID)

            })

            total.weight <- reactive({
                    sum(df()$weight)
            })

            output$plot1 <- renderPlot({




                    #Plot
                    ggplot(data=df(), aes(x=years, y=weight)) +
                            geom_line() +
                            geom_point() 



            })

            output$text1 <- renderText({total.weight()})

    })
库(闪亮)
图书馆(GG2)
shinyServer(功能(输入、输出){

df您还可以创建反应:

服务器.R

library(shiny)
    library(ggplot2)

    shinyServer(function(input, output) {

            df <- reactive({
                    years <- c(1:10)
                    weight <- c(5,7,9,11,12,17,19,20,21,22)
                    group <- c(rep("A",5), rep("B",5))
                    df <- data.frame(years,weight,group)

                    df <- subset(df, group == input$ID)

            })

            total.weight <- reactive({
                    sum(df()$weight)
            })

            output$plot1 <- renderPlot({




                    #Plot
                    ggplot(data=df(), aes(x=years, y=weight)) +
                            geom_line() +
                            geom_point() 



            })

            output$text1 <- renderText({total.weight()})

    })
库(闪亮)
图书馆(GG2)
shinyServer(功能(输入、输出){

df使用
reactive()
函数将什么分配给
df
列为年、重量和组。使用
reactive()将什么分配给
df
函数是否准确?是年份、权重、组还是df?在您的例子中,它是df,通常是最后一行代码中的变量。为确保这一点,我通常只将要指定的变量名称作为最后一行代码。这意味着在df()中,列是年份、权重和组。