我是R的新手并且发亮并期待制作一个交互式闪亮应用程序,允许用户使用具有D3可视化的rCharts切换他们为数据输入绘制图表的图表。
我可以让我的所有代码都在R中运行,但是当我尝试在闪亮的应用程序中运行它时,代码会编译,页面会打开,但不会显示任何情节。如何才能显示情节?我尝试在UI和Server函数中使用showOutput(),并尝试更改库路径,并确保库引用全部为小写。
闪亮代码:
library(shiny)
library(markdown)
library(corrplot)
library(ggplot2)
library(htmltools)
require(rCharts)
library(plyr)
library(dplyr)
library(rjson)
library(devtools)
options(RCHART_LIB = 'dimple')
options(RCHART_LIB = 'nvd3')
options(RCHART_LIB = 'polychart')
options(RCHART_LIB = 'morris')
ui3 <- shinyUI(
fluidPage(
# Application title
titlePanel("Survey Data Analysis"),
# Sidebar
sidebarPanel(
selectInput("dataset", "Dataset",
c("esoph", "upload my own")),
conditionalPanel("input.dataset === 'upload my own'",
fileInput("datafile", ""),
textInput("datafile_sep", "Field Seperator", value = ",")),
selectInput("plotType","Plot Type:",
c( Histogram = "Histogram", Bar = "Bar", Boxplot = "Boxplot", Scatter = "Scatter")),
uiOutput("x"),
uiOutput("y"),
checkboxInput("groups", "Grouping Variable", F),
uiOutput("group")
),
# Show a plot of the generated distribution
mainPanel(
tabsetPanel(
tabPanel("Data Plots",
column(9, showOutput("pd3", lib = "dimple")
#renderUI("plot1")
)),
tabPanel("Data",
dataTableOutput("dataTable"))
)
)
))
server3 <- shinyServer(function(input, output,session) {
require(rCharts)
options(RCHART_LIB = 'dimple')
options(RCHART_LIB = 'nvd3')
options(RCHART_LIB = 'polychart')
options(RCHART_LIB = 'morris')
#allows user to input data set or try on test set of data
dataset <- reactive({
datasource <- input$dataset
if(datasource == "upload my own") {
inFile <- input$datafile
if(is.null(inFile)) {
NULL
} else {
read.delim(inFile$datapath, sep = gsub("\\t", "\t", input$datafile_sep, fixed = TRUE))
}
} else {
eval(parse(text = datasource))
}
})
#updates y, dependent variable for 2D plots
output$y <- renderUI({
obj<-dataset()
var.opts<-c(colnames(obj))
selectInput("y","Dependent Variable:", var.opts) # uddate UI
})
#updates x, independent variable for plots
output$x <- renderUI({
obj<-dataset()
var.opts<-c(colnames(obj))
selectInput("x","Independent Variable:", var.opts) # uddate UI
})
#updates grouping variable for comparing data based on categorical variables such as gender
output$group <- renderUI({
obj<-dataset()
var.opts<-c(colnames(obj))
selectInput("group","Grouping Variable: ", var.opts) # uddate UI
})
#creates a plot object from the data so can create proper d3 plots
plot.obj <- reactive({
plot.list<-list()
plot.list$data<- dataset()
plot.list$x<-as.character(with(plot.list$data, get(input$x)))
plot.list$freqx <- as.data.frame(table(as.numeric(plot.list$x)))
colnames(plot.list$freqx) <- (c('x', 'Freq'))
# if (input$plotType == 'Scatter'){
plot.list$y<-as.character(with(plot.list$data,get(input$y)))
plot.list$freqy <- as.data.frame(table(as.numeric(plot.list$y)))
colnames(plot.list$freqy) <- (c('y', 'Freq'))
plot.list$variables <- data.frame(as.numeric(plot.list$x), as.numeric(plot.list$y))
colnames(plot.list$variables) <- (c('x','y'))
#}
#if (input$groups == TRUE){
plot.list$group <- as.character(with(plot.list$data, get(input$group)))
plot.list$groups = data.frame(as.numeric(plot.list$x), as.numeric(plot.list$group))
colnames(plot.list$groups) <- (c('x', 'g'))
#}
return(plot.list)
})
#makes sure correct rcharts library is referenced for each type of plot.
rchart_lib <- reactive({
if (input$plotType == "Histogram"){
pkg = "dimple"
}
if(input$plotType == "Bar" && input$groups == TRUE){
pkg = "nvd3"
}
else{
pkg = "morris"
}
if(input$plotType == "Scatter"){
pkg = "nvd3"
}
if(input$plotType == "Boxplot" && input$groups == TRUE){
pkg = "polychart"
}
#pkg = paste("/Library/Frameworks/R.framework/Versions/3.3/Resources/library/rCharts/libraries/", pkg, sep = "")
pkg = paste("/Users/laurastevens/Library/R/3.1/library/devtools/libs/rCharts/libraries/", pkg, sep = "")
return(pkg)
print(pkg)
})
pd3 <- reactive({
plot.df <- plot.obj()
if (input$plotType == "Histogram"){
p = dPlot(Freq~x, data = plot.df$freqx, type = 'bar', xAxis = list(orderRule = "x"), barGap = 0)
}
if (input$plotType == "Bar" && input$groups == TRUE){
p = nPlot(Freq ~ x, group = 'g', data = data.frame(table(plot.df$groups)), type = 'multiBarChart')
}
else{
p = mPlot(x = 'x', y = list('Freq'), data = plot.df$freqx, type = 'Bar', labels = list("Count"))
}
if(input$plotType == "Scatter" && input$groups == TRUE){
p = nPlot(Freq~x, group = 'g', data = data.frame(table(plot.df$groups)), type = 'scatterChart')
}
else{
p = nPlot(x ~ y, data = plot.df$variables, type = 'scatterChart')
}
if(input$plotType == "Boxplot" && input$groups == TRUE){
# compute boxplot statistics and cast it as a dataframe with no headers
dat = data.frame(boxplot(x ~ g, data = plot.df$groups, plot = F)$stats)
# compute boxplot statistics and cast it as a dataframe with no headers
bwstats = setNames(
as.data.frame(dat[complete.cases(dat),]),
nm = NULL
)
#initialize
p <- Highcharts$new()
# pass data as a list of lists
p$set(series = list(list(name = input$group, data = bwstats)))
# set xaxis/yaxis titles and labels
p$xAxis(
categories = levels(plot.df$groups$group),
title = list(text = input$group)
)
p$yAxis(
title = list(text = input$x)
)
p$chart(type = 'boxplot')
}
else{
p = boxplot(as.numeric(plot.df$x), xlab = input$x)
}
return(p)
})
#creates chart based on p returned from pd3
output$d3chart1 <- renderChart({
p1 <- pd3()
p1$addparams(height = 600, dom = "pd3")
return(p1)
})
#connects shinyUI and shinyServer functions for plot
output$plot1 <- renderUI({
if(input$plotType == "Boxplot" && input$groups == FALSE){
plotOutput("d3Chart1")
}
else{
rc_lib <- rchart_lib()
showOutput("d3Chart1", rc_lib)
}
})
})
shinyApp(ui = ui3, server = server3)
我正在使用R 3.3.1: R版本3.3.1(2016-06-21) 平台:x86_64-apple-darwin13.4.0(64位) 运行于:OS X 10.10.5(Yosemite)
区域设置: [1] en_US.UTF-8 / en_US.UTF-8 / en_US.UTF-8 / C / en_US.UTF-8 / en_US.UTF-8
附加基础包: [1] stats graphics grDevices utils数据集方法库
其他附件包:
[1] corrplot_0.77 stringr_1.1.0 sqldf_0.4-10 RSQLite_1.0.0 DBI_0.5-1
[6] gsubfn_0.6-6 proto_1.0.0 XLConnect_0.2-12 XLConnectJars_0.2-12 devtools_1.12.0
[11] rjson_0.2.15 dplyr_0.5.0 plyr_1.8.4 rCharts_0.4.5 htmltools_0.3.5
[16] markdown_0.7.7 shiny_0.14.2 ROCR_1.0-7 gplots_3.0.1 ggplot2_2.1.0
通过命名空间加载(而不是附加):
[1] Rcpp_0.12.7 bitops_1.0-6 tools_3.3.1 digest_0.6.10 jsonlite_1.1 memoise_1.0.0
[7] tibble_1.2 gtable_0.2.0 lattice_0.20-33 yaml_2.1.13 gridExtra_2.2.1 rJava_0.9-8
[13] withr_1.0.2 gtools_3.5.0 caTools_1.17.1 grid_3.3.1 data.table_1.9.6 R6_2.2.0
[19] RJSONIO_1.3-0 gdata_2.17.0 whisker_0.3-2 magrittr_1.5 scales_0.4.0 assertthat_0.1
[25] mime_0.5 xtable_1.8-2 colorspace_1.2-7 httpuv_1.3.3 KernSmooth_2.23-15 stringi_1.1.1