【问题标题】:how do you create a model from a csv file in shiny你如何在闪亮的 csv 文件中创建模型
【发布时间】:2016-07-25 16:15:14
【问题描述】:

我正在尝试创建一个接收 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(用户界面,服务器)

我的 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

当我使用 melt 函数转换 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。变量名称将根据列名称的不同而改变。我不能在模型中使用硬编码名称。

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    您的代码不是一个“最小”示例(其中有很多额外的东西),并且不能按提供的方式工作(ln 78 中的错误),而且只有一个情节,与模型无关。

    也就是说,问题在于您需要通过所选输入来引用列; input$xaxisGrp 代表 X,input$yaxisGrp 代表 Y。

    将您的 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 文档,还有更多可扩展的方式。

    【讨论】:

    • 我对 Shiny 很陌生,很抱歉这篇文章。您能否看一下这个新帖子:stackoverflow.com/questions/36454567/… 当我单击绘图选项卡时,我得到:需要有限的“xlim”值错误。看起来 x 和 y 值没有被填充。
    • 在我转到另一个问题之前,您能否确认您是否尝试过这个问题的答案?
    猜你喜欢
    • 1970-01-01
    • 2019-07-09
    • 1970-01-01
    • 2012-07-19
    • 2021-09-26
    • 2016-11-28
    • 2019-06-11
    • 2017-04-24
    • 2017-12-09
    相关资源
    最近更新 更多