如何使用户能够在R中的ggplot2和gVis图之间切换?
我正在制作一个允许用户上传任何csv文件的应用程序,该应用程序将对其进行图形化。我想允许用户在图形样式、GVI和ggplot之间切换。这些图是自己实现的,但我似乎不知道如何让用户使用checkboxInput(input$switchLine)切换它们。我只发布与手头问题相关的示例代码,如果您需要更多信息,请告诉我 我在服务器中尝试过以下操作:如何使用户能够在R中的ggplot2和gVis图之间切换?,r,ggplot2,shiny,R,Ggplot2,Shiny,我正在制作一个允许用户上传任何csv文件的应用程序,该应用程序将对其进行图形化。我想允许用户在图形样式、GVI和ggplot之间切换。这些图是自己实现的,但我似乎不知道如何让用户使用checkboxInput(input$switchLine)切换它们。我只发布与手头问题相关的示例代码,如果您需要更多信息,请告诉我 我在服务器中尝试过以下操作: if (input$switchLine) { output$gvisLine } else { output$plotLine }
if (input$switchLine) {
output$gvisLine
} else {
output$plotLine
}
但问题是在ui.R中,ggplot line使用plotOutput,而gVis使用html输出
R(我已经注释掉了gVis行,因为我现在只知道如何一次绘制一条)
库(闪亮)
数据集使用
或者类似于ui.R
使用
或者类似的ui.R
我以前尝试过这个,但由于需要使用tabPanels(我只放了一小部分代码),所以出现了一个错误。当我试图用tabPanels绘制上面的代码时,我得到一个参数的长度为零的错误。在tabPanels()中似乎对我有用。如果你有一个(最小的)可复制的例子,请张贴它!我以前尝试过这个,但由于需要使用tabPanels(我只放了一小部分代码),所以出现了一个错误。当我试图用tabPanels绘制上面的代码时,我得到一个参数的长度为零的错误。在tabPanels()中似乎对我有用。如果你有一个(最小的)可复制的例子,请张贴它!
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")
)
))
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)
})
})
conditionalPanel(
condition = "input.switchLine == false",
plotOutput('plotLine', height="auto")
),
conditionalPanel(
condition = "input.switchLine == true",
htmlOutput("gvisLine")
)