你如何从闪亮的csv文件创建一个模型

时间:2016-04-05 15:46:15

标签: r shiny

我正在尝试创建一个闪亮的应用程序,它接受csv文件并创建usl模型,创建绘图并显示模型输出:

的代码:

library(shiny)
library(shinydashboard)
library(leaflet)
library(data.table)
library(ggplot2)
library(usl)

ui <- pageWithSidebar(
  headerPanel("CSV Viewer"),
  sidebarPanel(
    fileInput('file1', 'Choose CSV File',
              accept=c('text/csv','text/comma-separated-values,text/plain','.csv')),
    tags$hr(),
    checkboxInput('header', 'Header', TRUE),
    fluidRow(
      column(6,radioButtons("xaxisGrp","X-Axis:", c("1"="1","2"="2"))),
      column(6,checkboxGroupInput("yaxisGrp","Y-axis:", c("1"="1","2"="2")))
    ),
    radioButtons('sep', 'Separator',
                 c(Comma=',', Semicolon=';',Tab='\t'), ','),
    radioButtons('quote', 'Quote',
                 c(None='','Double Quote'='"','Single Quote'="'"),'"'),
    uiOutput("choose_columns")
  ),
  mainPanel(
    tabsetPanel(
      tabPanel("Plot",plotOutput("plot")),
      tabPanel("Data", tableOutput('contents'))
    )
  )
)


####server

server <- function(input, output,session) {
  dsnames <- c()

  data_set <- reactive({
    inFile <- input$file1
    data(specsdm91)
    if (is.null(inFile))
      return(specsdm91)

    data_set<-read.csv(inFile$datapath, header=input$header, 
                       sep=input$sep, quote=input$quote)
  })

  output$contents <- renderTable({data_set()})

  observe({
    dsnames <- names(data_set())
    cb_options <- list()
    cb_options[ dsnames] <- dsnames
    updateRadioButtons(session, "xaxisGrp",
                       label = "X-Axis",
                       choices = cb_options,
                       selected = "")
    updateCheckboxGroupInput(session, "yaxisGrp",
                             label = "Y-Axis",
                             choices = cb_options,
                             selected = "")
  })
  output$choose_dataset <- renderUI({
    selectInput("dataset", "Data set", as.list(data_sets))
  })
  output$plot = renderPlot(
    {
      df <- data_set()
      gp <- NULL
      if (!is.null(df)){
         ##I need to show the plot here from the model           
          plot(throughput ~ load, data=df)
          plot(usl.model, add=true)


        }
      }
      return(gp)
    }
  )
  output$choose_columns <- renderUI({

    if(is.null(input$dataset))
      return()
    colnames <- names(contents)
    checkboxGroupInput("columns", "Choose columns", 
                       choices  = colnames,
                       selected = colnames)
  }) 
}

shinyApp(ui,server)

我的csv文件如下所示:

load    throughput
1   64.9
18  995.9
36  1652.4
72  1853.2
108 1828.9
144 1775
216 1702.2

=== 当我在xv和yv上打印时,我得到变量名称:

1] "load"
[1] "throughput"

我也可以打印df:

  load throughput
1    1       64.9
2   18      995.9
3   36     1652.4
4   72     1853.2
5  108     1828.9
6  144     1775.0
7  216     1702.2

当我打印时:

df$xv

我得到了

NULL

当我运行应用程序时,我收到此错误:

Warning: Error in <Anonymous>: invalid type (NULL) for variable 'df$xv'
Stack trace (innermost first):
    82: <Anonymous>
    81: eval
    80: eval
    79: plot.formula
    78: plot
    77: plot
    76: renderPlot [C:\shiny\file/ui.R#82]
    68: output$plot
     1: shiny::runAp

当我使用融合函数转换df时,它可以工作:

 mdf <- melt(df,id.vars=xv,measure.vars=yv)
          usl.model<-usl(value~load,data=mdf)
          plot(usl.model)

问题是我需要能够使用从csv文件而不是(xv和yv)获取的变量来创建模型,而不是像对值和加载那样硬编码。如何在创建模型和绘图时使用变量xv和yv。变量名称将根据列名称的不同而改变。我不能在模型中使用硬编码名称。

1 个答案:

答案 0 :(得分:1)

你的代码不是一个'最小'的例子(那里有很多额外的东西)并且没有提供的工作(在78中的错误),并且只有一个情节,没有关于模型。

那说,问题是您需要通过所选输入来引用列; X为input$xaxisGrp,Y为input$yaxisGrp

renderPlot更改为最低限度可达到您想要的效果

  output$plot = renderPlot({

df <- data_set()
df2 <- df[,c(input$xaxisGrp, input$yaxisGrp)]
if (!is.null(df)){
  ##I need to show the plot here from the model           
  #plot(throughput ~ load, data=df)
  plot(df2[,1], df2[,2])
  }
}

如果您阅读Shiny文档,还有更多可扩展的方法。