R shinyapp - 如何从csv打开和绘图。获取错误:尝试在get1index中选择少于一个元素

时间:2016-06-01 07:45:13

标签: r plot reactive-programming shiny

我是shinyapps的新手并尝试构建一个可以打开带有四列的.csv的简单应用程序。 打开后,用户可以选择三列用于绘制a)点和b)各个点上方和下方的范围 - 请参见下面的第一张图。 但是,在这一点上,我不知道如何在图中引用我的点数据。 我得到Error: attempt to select less than one element in get1index. - 请参阅下面的第二张图片。我在文本的底部包含了一个5行.csv文件。 期望的输出:

enter image description here

新错误 没有,解决了。

更新:现在的工作代码 - 见评论

   # Set wd
#setwd("C:/Data/SCRIPTS/R/Uncertainty/MCSims/simsShinyApp")

# Set libraries
library(shiny)
library(rriskDistributions)
library(ggplot2)
library(dplyr)

ui <-
        shinyUI(fluidPage(tabsetPanel(
                tabPanel("Group Data", " ",
                         fluidRow(
                                 titlePanel(h2("Group Guesses"), br()),

                                 column(width = 2,
                                        " ",
                                         fileInput('datafile', 'Choose CSV file',
                                                   accept=c('text/csv', 'text/comma-separated-values,text/plain')),
                                         uiOutput("selectcol1"),
                                         uiOutput("selectcol2"),
                                         uiOutput("selectcol3"),
                                         uiOutput("selectcol4")
                                 ),

                                 column(width = 3,
                                        " ",
                                        dataTableOutput('filedata')),
                                 column(width = 7,
                                        " ",
                                        plotOutput("plot6", height = "600px"))
                         ))
        )))

server <- function(input, output) {
        filedata <- reactive({
                infile <- input$datafile
                if (is.null(infile)) {
                        # User has not uploaded a file yet
                        return(NULL)
                }
                read.csv(infile$datapath)
        })

        # filedata() <- filedata()[order(filedata()[[input$selectcol1]]]


        x <- reactive({
                1:dim(filedata())[1]
        })

        output$selectcol1 <- renderUI({
                df <-filedata()
                if (is.null(df)) return(NULL)

                items=names(df)
                names(items)=items
                selectInput("selectcol1", "Best Estimate",items)

        })

        output$selectcol2 <- renderUI({
                df <-filedata()
                if (is.null(df)) return(NULL)

                items=names(df)
                names(items)=items
                selectInput("selectcol2", "Lower Bound",items)
        })

        output$selectcol3 <- renderUI({
                df <-filedata()
                if (is.null(df)) return(NULL)

                items=colnames(df)
                names(items)=items
                selectInput("selectcol3", "Upper Bound:",items)

        })

        output$selectcol4 <- renderUI({
          df <-filedata()
          if (is.null(df)) return(NULL)

          items=colnames(df)
          names(items)=items
          selectInput("selectcol4", "Source:",items)

        })

        output$filedata = renderDataTable({
                filedata()

        })
        output$plot6 <- renderPlot({
                plot(
                        x(),
                        filedata()[[input$selectcol1]],
                        log = "y",
                        ylim = range(c(min(
                          filedata()[[input$selectcol2]]
                        ), max(
                          filedata()[[input$selectcol3]]
                        ))),
                        pch = 18,
                        xlab = "Guess",
                        ylab = "Value",
                        main = "Scatter plot with 90% confidence intervals",
                        col = filedata()[[input$selectcol4]]
                )
                # hack: we draw arrows but with very special "arrowheads"
                arrows(
                        x(),
                        filedata()[[input$selectcol2]],
                        x(),
                        filedata()[[input$selectcol3]],
                        length = 0.05,
                        angle = 90,
                        code = 3
                )
                abline(h = 951, col = "green")
                abline(h = ave(filedata()[[input$selectcol1]]), col = "red")
        })
}

shinyApp(ui = ui, server = server)

FILE.CSV

Best,Lb,Up,Source
5,3,10,Bill
6,2,8,Tom
6,3,11,Bill
4,1,12,Tom

1 个答案:

答案 0 :(得分:1)

收集我所有的回答

1)您需要了解inputoutput之间的差异 在这里,您需要在绘图中使用input$ids来获取所选数据。

2)最好为每个元素设置不同的id uiOutput("col1")selectInput("col1",..)具有相同的ID

3)input$selectcol1返回character向量,因此如果您想获取名称== input$selectcol1的列,则需要filedata()[[input$selectcol1]]