我正在创建一个应用程序,允许用户上传任何csv文件,应用程序将对其进行图形化。我想让用户在图形样式,gVis和ggplot之间切换。这些图表可以自己实现,但我似乎无法弄清楚如何使用checkboxInput(输入$ switchLine)来启用用户切换它们。我将仅发布与手头问题相关的示例代码,如果您需要更多信息,请告诉我。
我尝试过像服务器那样的事情:
if (input$switchLine) {
output$gvisLine
} else {
output$plotLine
}
但问题是在ui.R中,ggplot行使用plotOutput而gVis使用html输出。
ui.R(我已经注释了gVis系列,因为我现在只知道如何一次绘制一个)
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)
})
})
答案 0 :(得分:4)
使用
conditionalPanel(
condition = "input.switchLine == false",
plotOutput('plotLine', height="auto")
),
conditionalPanel(
condition = "input.switchLine == true",
htmlOutput("gvisLine")
)
或ui.R