【问题标题】:R shinyapp - How to open and plot from csv. Getting Error: attempt to select less than one element in get1indexR shinyapp - 如何从 csv 打开和绘图。出现错误:尝试在 get1index 中选择少于一个元素
【发布时间】:2016-06-01 07:45:12
【问题描述】:

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

新错误 没有,解决了。

更新:下面的代码现在可以使用 - 请参阅 cmets

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

文件.csv

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

【问题讨论】:

  • 让一些对象具有一个 ID(我的想法)是一种不好的做法——uiOutput("col1") 和 selectInput("col1",...)。尝试更改 id 并使用 input$selectcol1 instaed of output$col1 其中 selectcol1 - 它是 selectInput("col1",..) 的新 id(每个输入都如此)
  • @Batanichek:谢谢。你能给我举个例子吗?我已经使用你的 cmets 更新了代码,但我仍然很难。新的错误是 x 和 y 长度不同。
  • 没有数据很难做例子。但现在问题在这里: input$selectcol1- 它的字符不是你的数据列。试试plot( x(), filedata()[[input$selectcol1]],...(你所有的输入$selectcol1)
  • @Batanichek:谢谢。我在底部添加了一个简单的 .csv 文件示例。您的 cmets 有助于进一步理解,但仍然出现错误:“尝试在 get1index 中选择少于一个元素”。有什么想法可以帮助我解决这个问题吗?我将继续尝试/研究以找出错误的原因...
  • @Batanichek:谢谢;我解决了。

标签: r plot reactive-programming shiny


【解决方案1】:

收集我所有的评论来回答

1) 您需要了解input 和output 之间的区别 这里你需要在 plot 中使用input$ids 来获取选定的数据。

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

3) input$selectcol1 返回character 向量,所以如果你想得到名称== input$selectcol1 的列,你需要filedata()[[input$selectcol1]]

【讨论】:

    猜你喜欢
    • 2023-04-04
    • 1970-01-01
    • 2020-06-19
    • 1970-01-01
    • 2016-06-10
    • 2016-01-24
    • 2015-07-13
    • 2018-03-23
    • 2020-07-28
    相关资源
    最近更新 更多