R shinyapp - How to open and plot from csv. Gettin

2019-07-30 22:49发布

问题:

I'm new to shinyapps and trying to build a simple app that can open a .csv with four columns. After opening, the user is can select three columns used to plot a) points and b) ranges above and below the respective points - see first image below. However, at this point I don't know how to reference my point data in the plot. I get Error: attempt to select less than one element in get1index. - see second image below. nI have included a 5 line .csv file at the bottom of the text. Desired output:

New Error None, solved.

UPDATE: CODE BELOW NOW WORKS - see comments

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

gather all my commets to answer

1) You need to understand differences between input and output Here you need to use input$ids in plot for get selected data.

2) Better to set different id for each element uiOutput("col1") and selectInput("col1",..) have same id

3) input$selectcol1 return character vector, so if you want to get column with name == input$selectcol1 you need filedata()[[input$selectcol1]]