[R] Building a reactive plot with ggplot2 and shiny.
Bert Gunter
bgunter.4567 at gmail.com
Mon Mar 14 16:42:56 CET 2016
1. I don't use Shiny, so I may be wrong about this, but--
2. Shiny is an R-Studio product. I believe they have a user support
forum on their website, and you might therefore get a faster and
better answer by posting there (e.g. at Rstudio.com ).
Cheers,
Bert
Bert Gunter
"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
On Mon, Mar 14, 2016 at 7:35 AM, Santiago Paz <santiago.l.paz at gmail.com> wrote:
> I'm fairly new with R and shiny and I am trying to make an application
> where users can select various variables from a data set and plot the
> ratios of the variables in one graph.
>
> Basically: - The user selects the number of ratios they want - The
> user then selects which variable will be the numerator and which will
> be the denominator. - The a plot should then appear with curves for
> each ratio.
>
> My main problem right now is that the plot isn't displayed until all
> the numerators and denominators have been selected. I want it to look
> such that when you finish specifying one ratio it plots it, then when
> you specify another ratio it adds it to the graph. Basically I want
> the plot to be reactive so that it updates as you select the ratios.
> Here's a basic idea of the code
>
> SERVER
>
>
> output$sopPlot <- renderPlot({
>
> data <- read.csv("data")
>
> # Introduce function that binds columns, useful when generating
> the data frame that includes all ratios
> cbind.all <- function (...)
> {
> nm <- list(...)
> nm <- lapply(nm, as.matrix)
> n <- max(sapply(nm, nrow))
> do.call(cbind, lapply(nm, function(x) rbind(x, matrix(, n -
>
> nrow(x), ncol(x)))))
> }
>
> # Some variables that are useful in the loop that creates the data
> frame of the ratios
>
> ratio <- as.character(input$nRatios)
> rationum <- as.integer(substr(ratio,2,2))
> Ninput <- c(input$Num1, input$Num2)
> Dinput <- c(input$Den1, input$Den2)
>
> subData <- data.frame()
>
> # Loop that will analyze each ratio and create a data frame for
> the number of ratios selected
>
> for (i in 1:rationum){
>
> numData <- subset(data, Ninput[i])
> numData <- aggregate(Q~Y, data = numData, FUN=sum)
>
> denData <- subset(data, Dinput[i])
> denData <- aggregate(Q~Y, data = denData, FUN=sum)
>
> # Combining numerator and denominator data
> subD <- merge(numData, denData, by.x="Y", by.y="Y")
>
> # Dividing variables to generate ratio data
> rat<- as.vector(subD$Q.x/subD$Q.y)
>
> # Insert this data for the ratio into empty dataframe using cbind.all
> subData <- cbind.all(subData, rat)
>
> # naming each column
> num <- as.character(i)
> colnames(subData)[i] <- paste("Ratio",num, sep=" ")
>
> subData<- as.data.frame(subData)
> }
>
> #Reshape data
> subData.long <- melt(subData, id.vars = "Y")
>
> # Plot
>
> g <- ggplot(subData.long,aes(Y,value,color=variable))+geom_line()+geom_point()
> g + scale_y_continuous("Ratio") + scale_x_continuous("Year")
>
> })
>
>
>
>
>
> UI
>
>
>
> sidebarPanel(
> conditionalPanelselectInput("nRatios", "How Many Ratios:",
> choices = c('0'='n0','1'='n1','2'='n2'))
>
> br()
> ),
>
> conditionalPanel(condition = "input.analysisType == 'ratio' &
> (input.nRatios == 'n1' | input.nRatios == 'n2' | input.nRatios == 'n3'
> | input.nRatios == 'n4' | input.nRatios == 'n5')",
> p(strong("Ratio 1")),
> selectizeInput("Num1",
> "Numerator 1:",
> choices = categories,
> multiple = TRUE),
> selectizeInput("Den1",
> "Denominator 1:",
> choices = categories,
> multiple = TRUE),
> br()
>
> ),
>
> # 2
> conditionalPanel(condition = "input.analysisType == 'ratio'
> & (input.nRatios == 'n2' | input.nRatios == 'n3' | input.nRatios ==
> 'n4' | input.nRatios == 'n5')",
> p(strong("Ratio 2")),
> selectizeInput("Num2",
> "Numerator 2:",
> choices = categories,
> multiple = TRUE),
> selectizeInput("Den2",
> "Denominator 2:",
> choices = categories,
> multiple = TRUE),
> br()
>
> ),
>
> mainPanel(
> plotOutput("sopPlot"),
> textOutput("debug")
> )
> ))
>
>
>
> I apologize if this is too much text.
>
> I think the biggest problem is that the ratios are subsetted and
> stored in subData in the for loop. And the plot is made once subData
> is acquired.
>
> Anyway, any help is appreciated. Thanks in advance.
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list