【问题标题】:Graph is not displayed in shiny [R]图表不以闪亮的 [R] 显示
【发布时间】:2016-01-05 10:44:15
【问题描述】:

我在 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])})    
})

【问题讨论】:

  • 您需要使用plot 命令,而不是行。 lines 用于添加到现有的绘图设备,而不是创建一个。 plot(dat[,colm],dat[,colm1],type="b")
  • 我将行命令更改为 plot(dat[,colm],dat[,colm1]) 但仍然没有变化
  • 你想要直方图还是条形图?
  • 用户选择的两个变量之间的 LINE 散点图

标签: r shiny shinydashboard


【解决方案1】:

因此,在进一步检查后,您的脚本存在一些问题:

1) colm 不能被 output$text4 引用。这是因为范围...

2) 当您注释掉 output$text4 代码时,我现在在 plot 调用中收到未定义的列错误。这是因为强制您的列选择为数字返回NA

下面应该做你正在寻找的。​​p>

这里是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

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

)

)

【讨论】:

    猜你喜欢
    • 2014-05-13
    • 2018-12-21
    • 2021-09-02
    • 2018-03-23
    • 2016-12-14
    • 2019-03-28
    • 1970-01-01
    • 2014-06-16
    • 2014-09-04
    相关资源
    最近更新 更多