How to enable user to switch between ggplot2 and gVis graphs in R Shiny?
I'm making an app that allows the user to upload any csv file and the app will graph it. I'd like to allow the user to switch between graph styles, gVis and ggplot. The graphs work as implemented by themselves, but I can't seem to figure out how to enable the user to switch them with a checkboxInput (input$switchLine). I'll post only the sample code relative to the problem at hand, let me know if you need more info.
I've tried things like in server:
if (input$switchLine) {
output$gvisLine
} else {
output$plotLine
}
But the problem is that in the ui.R, ggplot line uses plotOutput while gVis uses html Output.
ui.R (I've commented out the gVis line as I only know how to plot one at a time right now)
library(shiny)
dataset <- list('Upload a file'=c(1))
shinyUI(pageWithSidebar(
headerPanel(''),
sidebarPanel(
wellPanel(
selectInput('xLine', 'X', names(dataset)),
selectInput('yLine', 'Y', names(dataset), multiple=T)
),
wellPanel(
checkboxInput('switchLine', 'Switch to gVis')
)
),
mainPanel(
tabPanel("Line Graph", plotOutput('plotLine', height="auto"), value="line"),
#Below is the gvis Line
#tabPanel("Line Graph", htmlOutput("gvisLine"), value="line")
)
))
server.R
library(reshape2)
library(googleVis)
library(ggplot2)
library(plyr)
library(scales)
require(xlsx)
require(xlsxjars)
require(rJava)
require(shiny)
options(shiny.maxRequestSize=-1)
shinyServer(function(input, output, session) {
if (is.null(input$file))
return(NULL)
else if (identical(input$format, 'CSV'))
return(read.csv(input$file$datapath))
else if (identical(input$format, 'XLSX'))
return(read.xlsx2(input$file$datapath, input$sheet))
else
return(read.delim(input$file$datapath))
})
observe({
df <- data()
str(names(df))
updateSelectInput(session, 'xLine', choices = names(df))
updateSelectInput(session, 'yLine', choices = names(df))
}
})
output$gvisLine<- renderGvis( {
tempX <- input$xLine
tempY <- input$yLine
if (is.null(data()))
return(NULL)
if (is.null(tempY))
return(NULL)
gvisLineChart(data(),xvar=tempX,yvar=tempY,
options=list(
title=paste("",tempX," VS ",tempY,""),
titlePosition='out',
hAxis="{slantedText:'true',slantedTextAngle:45}",
titleTextStyle="{color:'black',fontName:'Courier'}",
legend="{color:'black',fontName:'Courier'}",
fontSize="10",
chartArea="{left:40,top:30,width:'90%',height:'85%'}",
height=700, width=1100))
})
output$plotLine <- renderPlot(height=650, units="px", {
tempX <- input$xLine
tempY <- input$yLine
if (is.null(data()))
return(NULL)
if (is.null(tempY))
return(NULL)
widedata <- subset(data(), select = c(tempX, tempY))
melted <- melt(widedata, id = tempX)
p <- ggplot(melted, aes_string(x=names(melted)[1], y="value", group="variable", color="variable")) + geom_line() + geom_point()
p <- p + opts(axis.text.x=theme_text(angle=45, hjust=1, vjust=1))
p <- p + labs(title=paste("",tempX," VS ",tempY,""))
print(p)
})
})
Use
conditionalPanel(
condition = "input.switchLine == false",
plotOutput('plotLine', height="auto")
),
conditionalPanel(
condition = "input.switchLine == true",
htmlOutput("gvisLine")
)
or something similar in ui.R
上一篇: R闪亮的mainPanel显示样式和字体