【发布时间】: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。变量名称将根据列名称的不同而改变。我不能在模型中使用硬编码名称。
【问题讨论】: