R Shinyapp - 如何从 csv 打开和绘图.获取错误:尝试在 get1index 中选择少于一个元素 [英] R shinyapp - How to open and plot from csv. Getting Error: attempt to select less than one element in get1index
问题描述
我是 Shinyapps 的新手,正在尝试构建一个简单的应用程序,该应用程序可以打开包含四列的 .csv.打开后,用户可以选择三列用于绘制 a) 点和 b) 相应点上方和下方的范围 - 请参见下面的第一张图片.但是,此时我不知道如何在图中引用我的点数据.我收到 错误:尝试在 get1index 中选择少于一个元素.
- 请参阅下面的第二张图片.n我在文本底部包含了一个 5 行的 .csv 文件.期望输出:
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:
新错误没有,解决了.
更新:下面的代码现在可以使用了 - 查看评论
# 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
推荐答案
收集我所有的意见来回答
gather all my commets to answer
1) 您需要了解 input
和 output
之间的区别这里你需要在 plot 中使用 input$ids
来获取选定的数据.
1) You need to understand differences between input
and output
Here you need to use input$ids
in plot for get selected data.
2) 最好为每个元素设置不同的 iduiOutput("col1")
和 selectInput("col1",..)
具有相同的 id
2) Better to set different id for each element
uiOutput("col1")
and selectInput("col1",..)
have same id
3) input$selectcol1
返回 character
向量,所以如果你想得到 name == input$selectcol1
的列,你需要 filedata()[[input$selectcol1]]
3) input$selectcol1
return character
vector, so if you want to get column with name == input$selectcol1
you need filedata()[[input$selectcol1]]
这篇关于R Shinyapp - 如何从 csv 打开和绘图.获取错误:尝试在 get1index 中选择少于一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!