简体   繁体   English

图形未显示为闪亮[R]

[英]Graph is not displayed in shiny [R]

I am using shiny package in R to take input from user and plot the X and Y variable against each other as line plot.There is no error displayed.Everything is displayed except for the graph.Please can someone help why the graph is not displayed .Here is the ui.r file 我在R中使用闪亮的包从用户那里获取输入,并将X和Y变量相互绘制为线图。没有显示错误。除了图形外,所有内容均显示。请有人帮忙为什么不显示图形。这是ui.r文件

library(shiny) # load the shiny package
setwd("C:/indiahacks2")
dat<-read.csv("final.csv")

# Define UI for application
shinyUI(fluidPage(

  # Header or title Panel 
  titlePanel(h4('Impulse Response on VAR MODEL', align = "center")),

  # Sidebar panel
  sidebarPanel(



    selectInput("Impulse", label = "1. Select the Impulse Variable", 
                choices = names(dat)), 
    selectInput("Response", label = "1. Select the Response Variable", 
                choices = names(dat)),


    sliderInput("Lag", "2. Select the number of histogram BINs by using the slider below", min=0, max=25, value=10),

    radioButtons("colour", label = "3. Select the color of histogram",
                 choices = c("Green", "Red",
                             "Yellow"), selected = "Green")
  ),

  # Main Panel
  mainPanel(
    textOutput("text1"),
    textOutput("text2"),
    textOutput("text3"),
    textOutput("text3"),
    plotOutput("myhist")

  )

)
)

Server.r 服务器

library(shiny) # Load shiny package

dat<-read.csv("final.csv")

shinyServer(


  function(input, output) {

    output$text1 <- renderText({ 
      colm = as.numeric(input$Impulse)
      paste("Impulse Variable is", names(dat[colm]))

    })

    output$text2 <- renderText({ 
      paste("Color of plot is", input$radio)
    })

    output$text3 <- renderText({ 
      paste("Number of Lags is", input$Lag)
    })
    output$text4 <- renderText({ 
      colm = as.numeric(input$Response)
      paste("Response Variable is", names(dat[colm]))

    })

    output$myhist <- renderPlot(

      {
        colm = as.numeric(input$Impulse)
        colm1 = as.numeric(input$Response)
        plot(dat[,colm],dat[,colm1])})    
})

So there a couple of things wrong with your script, upon further inspection: 因此,在进一步检查后,您的脚本有几处错误:

1) colm cannot be referenced by output$text4 . 1) colm不能被output$text4引用。 This is because of scoping... 这是因为作用域...

2) When you comment-out the output$text4 code I now receive an undefined column error in the plot call. 2)当您注释掉output$text4代码时,在plot调用中我现在收到未定义的列错误。 This is because forcing your column choices to numeric returns NA . 这是因为将列选择强制为数字会返回NA

Below should do what you are looking for. 下面应该做您想要的。

Here is the server.R code: 这是server.R代码:

library(shiny) # Load shiny package
dat<-read.csv("final.csv")

shinyServer(

function(input, output) {

    output$text1 <- renderText({ 
        colm = as.numeric(input$Impulse)
        paste("Impulse Variable is", columns()[2])

    })
    output$text2 <- renderText({ 
        paste("Color of plot is", input$radio)
    })

    output$text3 <- renderText({ 
        paste("Number of Lags is", input$Lag)
    })
    output$text4 <- renderText({ 
        colm = as.numeric(input$Response)
        paste("Response Variable is", columns()[2])

    })

    columns<-reactive({
        colm = as.character(input$Impulse)
        colm1 = as.character(input$Response)
        return(c(colm, colm1) )
    })

    output$myhist <- renderPlot(

        {
            plot(dat[,columns()[1]],dat[,columns()[2]],type="b")})
})

*Ui.R * Ui.R

# Define UI for application
library(shiny)
shinyUI(fluidPage(

# Header or title Panel 
titlePanel(h4('Impulse Response on VAR MODEL', align = "center")),

# Sidebar panel
sidebarPanel(



    selectInput("Impulse", label = "1. Select the Impulse Variable", 
                choices = names(dat)), 
    selectInput("Response", label = "1. Select the Response Variable", 
                choices = names(dat)),


    sliderInput("Lag", "2. Select the number of histogram BINs by using the slider below", min=0, max=25, value=10),

    radioButtons("colour", label = "3. Select the color of histogram",
                 choices = c("Green", "Red",
                             "Yellow"), selected = "Green")
),

# Main Panel
mainPanel(
    textOutput("text1"),
    textOutput("text2"),
    textOutput("text3"),
    textOutput("text4"),
    plotOutput("myhist")

)

)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM