How can i make sidebar panel dynamic in shiny? - r

**#Ui.code:**
library(shiny)
fluidPage(
titlePanel(title=h2(" Lucas Tvs",align="center")),
sidebarPanel(
conditionalPanel(condition="input.tabs1=='Profit Loss'",
selectInput("Operations","Select the desired Profit Loss statement",choices = profitloss1$Operations)),
br(),
conditionalPanel(condition="input.tabs1=='Profit Loss'",
selectInput("summary1","Select the desired Summary",choices = summary1$OPERATIONS)),
conditionalPanel(condition="input.tabs1=='Ratio'",
selectInput("Ratio","Select the desired Ratios",choices=ratios1$Ratios)),
br(),
conditionalPanel(condition="input.tabs1=='Ratio'",
selectInput("summary","Select the desired Summary",choices = summary$RATIO)),
conditionalPanel(condition="input.tabs1=='Balancesheet'",
selectInput("Particulars","Select the desired Balancesheet statement", choices = Balancesheet$Particulars)),
br(),
conditionalPanel(condition="input.tabs1=='Balancesheet'",
selectInput("summary2","Select the desired Summary",choices = summary2$PARTICULARS))
),
mainPanel(
tabsetPanel(id="tabs1",
tabPanel("Profit Loss",column(5,tableOutput("profitloss")), column(7,plotOutput("plot")),tableOutput("summary1")),
tabPanel("Ratio",column(5,tableOutput("Ratio")),column(7,plotOutput("plot2")),tableOutput("summary")),
tabPanel("Balancesheet",column(5,tableOutput("Balancesheet")),column(6,plotOutput("plot1")),tableOutput("summary2"))
)
)
)
In the UI part I have created 3 tabs in the main panel and 2 select inputs in the sidebar panel.All those are reacting dynamically but now I want my code to show me that both the select input in the side bar panel are mutually dependent on each other.
#server code:
library(shiny)
library(ggplot2)
shinyServer(function(input,output){
output$profitloss<-renderTable({
oporationfilter<-profitloss1[profitloss1$Operations==input$Operations,c("Years","Value")]
})
output$Ratio<-renderTable({
ratiofilter<-ratios1[ratios1$Ratios==input$Ratio,c("Years","Value")]
})
output$Balancesheet<-renderTable({
Balancesheetfilter<-Balancesheet[Balancesheet$Particulars==input$Particulars,c("Years","Value")]
})
output$summary<-renderTable({
summaryfilter<-summary[summary$RATIO==input$summary,c("Mean","Standard.Deviation","CAGR.1","CAGR.3","CAGR.5")]
})
output$summary1<-renderTable({
summary1filter<-summary1[summary1$OPERATIONS==input$summary1,c("Mean","Standard.Deviation","CAGR.1","CAGR.3","CAGR.5","CAGR.7")]
})
output$summary2<-renderTable({
summary2filter<-summary2[summary2$PARTICULARS==input$summary2,c("Mean","Standard.Deviation","CAGR.1","CAGR.3","CAGR.5")]
})
output$plot<-renderPlot({
options(scipen = 999)
p<-ggplot(data = profitloss1[profitloss1$Operations==input$Operations,]
,aes(x=Years,y=Value))
p+geom_line()+xlab("Years")+ylab("Value in Lakhs")+ggtitle("Profitloss Plot")
})
output$plot2<-renderPlot({
q<-ggplot(data = ratios1[ratios1$Ratios==input$Ratio,]
,aes(x=Years,y=Value))
q+geom_line()+xlab("Years")+ylab("value in lakhs")+ggtitle("Ratios Plot ")
})
output$plot1<-renderPlot({
q<-ggplot(data = Balancesheet[Balancesheet$Particulars==input$Particulars,]
,aes(x=Years,y=Value))
q+geom_line()+xlab("Years")+ylab("value in lakhs")+ggtitle("Balancesheet Plot")
})
})
This is the code for designing of the website now I want my both the select input should be mutually dependent.
for example: If select in XXX in drop down menu I should automatically get the same output in the 2nd select input.

Related

R Shiny: Right Align and Left Align in the Same Dropdown Menu

I am making an R Shiny app and would like to left align and right align in the same dropdown menu.
So in the example app:
library(shiny)
# Define UI
ui <- fluidPage(
# App title ----
titlePanel("Dropdown Problems"),
# Sidebar layout with input and output definitions
sidebarLayout(
# Sidebar panel for inputs
sidebarPanel(
# Define Dropdown Menu
selectizeInput("selection_dropdown", "Select Selection of Interest:",
choices=NULL,
options=list(
maxItems=1,
placeholder='Select Selection',
create=TRUE)
)
),
# Main panel for displaying outputs ----
mainPanel(
# Output:
plotOutput(outputId = "sample_plot")
)
)
)
server <- function(session,input, output) {
# Define New Data Frame
new_data_frame <- data.frame(column1=c("aaaaaaaa","bb","cccc"),column2=c(1,2,3),column3=c("plot_a","plot_b","plot_c"))
# Create Dropdown Menu
observe({
dropdown_choices <- paste(new_data_frame$column1," (",new_data_frame$column2,")",sep="")
updateSelectizeInput(
session,
"selection_dropdown",
choices=dropdown_choices,
server=TRUE,
)
})
# Create Output Plot (This doesn't really matter)
output$sample_plot <- renderPlot({
plot_selection <- gsub(" .*","",input$selection_dropdown)
plot_selection <- new_data_frame$column3[new_data_frame$column1==plot_selection]
plot(
x=NA,
y=NA,
xlim=c(0,100),
ylim=c(0,100)
)
text(x=50,y=50,plot_selection)
})
}
shinyApp(ui = ui, server = server)
In the dropdown menu I would like the letters to be left aligned within the dropdown and the numbers and brackets to be right aligned.
I can separate them by a tab but the numbers won't be in line with each other unfortunately.
Thanks in advance for your help.
How about this
We can use the counter trick from CSS so these numbers are automatically assigned based on the order they are displayed in the dropdown. It means you don't need to manually add the index. When it is selected, on the server, it returns the value without the index.
library(shiny)
# Define UI
ui <- fluidPage(
tags$style(
'
:root {counter-reset: mycounter;}
.selectize-dropdown-content .option::after {
counter-increment: mycounter;
content: "(" counter(mycounter) ")";
float: right;
}
'
),
# App title ----
titlePanel("Dropdown Problems"),
# Sidebar layout with input and output definitions
sidebarLayout(
# Sidebar panel for inputs
sidebarPanel(
# Define Dropdown Menu
selectizeInput("selection_dropdown", "Select Selection of Interest:",
choices=NULL,
options=list(
maxItems=1,
placeholder='Select Selection',
create=TRUE)
)
),
# Main panel for displaying outputs ----
mainPanel(
# Output:
plotOutput(outputId = "sample_plot")
)
)
)
server <- function(session,input, output) {
# Define New Data Frame
new_data_frame <- c("aaaaaaaa","bb","cccc")
# Create Dropdown Menu
observe({
updateSelectizeInput(
session,
"selection_dropdown",
choices=new_data_frame,
server=TRUE,
)
})
# Create Output Plot (This doesn't really matter)
output$sample_plot <- renderPlot({
plot_selection <- gsub(" .*","",input$selection_dropdown)
plot(
x=NA,
y=NA,
xlim=c(0,100),
ylim=c(0,100)
)
text(x=50,y=50,plot_selection)
})
}
shinyApp(ui = ui, server = server)
Updates:
If your indices are not ordered numbers, we can still do it.
I just assume your data is still sending options from the server, even though your demo data seems that it can be done purely from the UI. Imagine your indices are some random numbers. We can send these numbers as CSS style to UI and format the dropdown.
library(shiny)
library(glue)
library(magrittr)
# Define UI
ui <- fluidPage(
# App title ----
titlePanel("Dropdown Problems"),
# Sidebar layout with input and output definitions
sidebarLayout(
# Sidebar panel for inputs
sidebarPanel(
# Define Dropdown Menu
uiOutput("style"),
selectizeInput("selection_dropdown", "Select Selection of Interest:",
choices=NULL,
options=list(
maxItems=1,
placeholder='Select Selection',
create=TRUE)
)
),
# Main panel for displaying outputs ----
mainPanel(
# Output:
plotOutput(outputId = "sample_plot")
)
)
)
server <- function(session,input, output) {
# Define New Data Frame
new_data_frame <- c("aaaaaaaa","bb","cccc")
indices <- sample(999, 3)
output$style <- renderUI(
tags$style(glue(.open = '#{', .close = "}#",
'
.selectize-dropdown-content .option:nth-child(#{seq_along(indices)}#)::after {
content: "(#{indices}#)";
float: right;
}
'
) %>% glue_collapse("\n"))
)
# Create Dropdown Menu
observe({
updateSelectizeInput(
session,
"selection_dropdown",
choices=new_data_frame,
server=TRUE,
)
})
# Create Output Plot (This doesn't really matter)
output$sample_plot <- renderPlot({
plot_selection <- gsub(" .*","",input$selection_dropdown)
plot(
x=NA,
y=NA,
xlim=c(0,100),
ylim=c(0,100)
)
text(x=50,y=50,plot_selection)
})
}
shinyApp(ui = ui, server = server)
I've created a new column that combines column 1 and 2, then a little bit of Javascript is used to create HTML for each option.
It left aligns the value from column 1 and right aligns the value from column 2.
It can probably be done without creating the new column by passing the 2 columns to the Javascript function.
library(shiny)
# Define UI
ui <- fluidPage(
# App title ----
titlePanel("Dropdown Problems"),
# Sidebar layout with input and output definitions
sidebarLayout(
# Sidebar panel for inputs
sidebarPanel(
# Define Dropdown Menu
selectizeInput("selection_dropdown", "Select Selection of Interest:",
choices=NULL,
options=list(
maxItems=1,
placeholder='Select Selection',
create=TRUE)
)
),
# Main panel for displaying outputs ----
mainPanel(
# Output:
plotOutput(outputId = "sample_plot")
)
)
)
server <- function(session,input, output) {
# Define New Data Frame
new_data_frame <- data.frame(column1=c("aaaaaaaa","bb","cccc"),column2=c(1,2,3),column3=c("plot_a","plot_b","plot_c"))
new_data_frame$column4 <-paste0(new_data_frame$column1, " (", new_data_frame$column2, ")")
# Create Dropdown Menu
observe({
dropdown_choices <- new_data_frame$column4
updateSelectizeInput(
session,
"selection_dropdown",
choices=dropdown_choices,
options = list(render = I(
'{
option: function(item, escape) {
const x = item.value.split(" ");
return `<p style=\"text-align:left;\">
${x[0]}
<span style=\"float:right;\">
${x[1]}
</span>
</p>`
}
}')),
server=TRUE,
)
})
# Create Output Plot (This doesn't really matter)
output$sample_plot <- renderPlot({
plot_selection <- gsub(" .*","",input$selection_dropdown)
plot_selection <- new_data_frame$column3[new_data_frame$column1==plot_selection]
plot(
x=NA,
y=NA,
xlim=c(0,100),
ylim=c(0,100)
)
text(x=50,y=50,plot_selection)
})
}
shinyApp(ui = ui, server = server)

Unable to generate selected data details in tab created using shiny

I have started practicing shiny package for making dashboard, and i am still an amateur at R, please help me to display the data which will be selected using selectinput in the allocated tab which i have created for display of data.
I shall share my ui code as well as server code. Please assist how to display selected data in the data tab created.
ui.R code
library(shiny)
library(shinydashboard)
shinyUI(fluidPage(
titlePanel(h1("Test for application of all the tutorials completed till now")),
sidebarLayout(
sidebarPanel((h2("Information Panel Enter")),
selectInput("data", "Select the dataset for hist analysis",
choices = c("iris","pressure","USArrests", selected = "pressure")),
numericInput("obs", "Select the number of observations for the dataset", value = 5,min = 5,max = 30,step = 1 ),
sliderInput("bins", "Select the number of bins for histogram", value = 6, min = 6, max = 20, step = 1),
radioButtons("color", "selecct the color of histogram" , choices = c("black","purple","brown"))),
mainPanel((h3("Main Panel of all the information display")),
tabsetPanel(type = c("pills"),
tabPanel("Summary" , h4(textOutput("Mysumhead")) ,verbatimTextOutput("Mysum")),
tabPanel("Structure and Observation" , h4(textOutput("Mystrhead")), verbatimTextOutput("Mystr")),
tabPanel("Plot"),
tabPanel("Data" , verbatimTextOutput("Mydata"))))
)))
server.R code
library(shiny)
library(shinydashboard)
library(datasets)
shinyServer(function(input,output){
output$Mysum <- renderPrint({
summary(get(input$data))
})
output$Mysumhead <- renderText({
paste("Data Selected for checking summary is " , input$data)
})
output$Mystr <- renderPrint({
str(get(input$data))
})
output$Mystrhead <- renderText({
paste("Data selected for observing summary of the data is " , input$data)
})
output$Mydata <- renderTable({
data(input$data)
})
})
you are good in all point except one.
In the UI.R, in Data TAB just change to tableOutput("Mydata") and in Server.R change the code inside rendertable({}) change it to get(input$data).
It will be good to go. You should use tableOutput for displaying Table when you want to use renderTable in server side

Recomputing renderplot based on renderui user input in RShiny

I'm a bit of an RShiny and R novice. I'm trying to program an RShiny application. It would initially graphs a scatterplot matrix using the first three variables of the dataset by default. The user could then choose their own variable selections from a complete list of variables. Once variables are chosen, the user would click and action button and the graph would be recomputed using the newly selected variables.
I'm using selectinput rather than checkboxinput to accommodate datasets with many variables. I'm using the iris dataset. The code below produces the initial graph and allows the user to select the variables. I just can't figure out how to make it recompute the matrix plot. How do I do this? Thanks!
library(shiny)
runApp(list(
ui = fluidPage(
cols = colnames(iris),
headerPanel('Grow Clusters'),
tabsetPanel(
tabPanel("Plot",
sidebarPanel(
# uiOutput("varselect"),
selectInput("choose_vars", "Select variables to plot",
choices=colnames(iris), selected=iris[1:3], multiple=T),
actionButton("submitButton", "Produce Matrix Plot!")
),
mainPanel(
plotOutput('pairsplot')
)
),
tabPanel("Summary")
,
tabPanel("Table")
)
),
server = function(input, output) {
selectedData <- reactive({
cols = colnames(iris)
selectInput("choose_vars", "Select variables to plot",
choices=cols, selected=cols[1:3], multiple=T)
})
output$pairsplot <- renderPlot({
pairs(iris[1:3], pch = 21)
})
output$varselect <- renderUI({
iris[input$choose_vars]
plotOutput("pairsplot")
})
}
)
)
I think what you are looking for is quo function as in the Chris Beely blog: https://chrisbeeley.net/?p=1116
If you want users to pass arguments and then turn that character vector into objects r can read you need to use quo(input$choose_vars) and then in the plot you need to add !! before that passing variable. Notice you need to load dplyr.
library(shiny)
library(dplyr)
runApp(list(
ui = fluidPage(
cols = colnames(iris),
headerPanel('Grow Clusters'),
tabsetPanel(
tabPanel("Plot",
sidebarPanel(
# uiOutput("varselect"),
selectInput("choose_vars", "Select variables to plot",
choices=colnames(iris), selected=iris[1:3], multiple=T),
actionButton("submitButton", "Produce Matrix Plot!")
),
mainPanel(
plotOutput('pairsplot')
)
),
tabPanel("Summary")
,
tabPanel("Table")
)
),
server = function(input, output) {
selectedData <- reactive({
cols <- colnames(iris)
selectInput("choose_vars", "Select variables to plot",
choices=cols, selected=cols[1:3], multiple=T)
})
output$pairsplot <- renderPlot({
if(is.null(input$choose_vars) || length(input$choose_vars)<2){
pairs(iris[1:3], pch = 21)
} else {
var <- quo(input$choose_vars)
pairs(iris %>% select(!!var), pch = 21)
}
})
output$varselect <- renderUI({
iris[input$choose_vars]
plotOutput("pairsplot")
})
}
)
)

control two data table with single input in shiny web

As i have written code to control single data table and the code is
under ui:
library(shiny)
fluidPage(
titlePanel(title=h2(" Lucas Tvs",align="center")),
sidebarPanel(
conditionalPanel(condition="input.tabs1=='profitloss'",
selectInput("Operations","Select the desired ProfitLoss statement",choices = profitloss1$Operations)),
conditionalPanel(condition="input.tabs1=='Ratio'",
selectInput("Ratio","Select the desired Ratios",choices=ratios1$Ratios)),
conditionalPanel(condition="input.tabs1=='Balancesheet'",
selectInput("Particulars","Select the desired Balancesheet statement", choices = Balancesheet$Particulars))
),
mainPanel(
tabsetPanel(id="tabs1",
tabPanel("profitloss",column(5,tableOutput("profitloss")), column(7,plotOutput("plot"))),
tabPanel("Ratio",column(5,tableOutput("Ratio")),column(6,plotOutput("plot2"))),
tabPanel("Balancesheet",column(5,tableOutput("Balancesheet")),column(6,plotOutput("plot1")))
)
)
)
under sever:
library(shiny)
library(ggplot2)
shinyServer(function(input,output){
output$profitloss<-renderTable({
oporationfilter<-profitloss1[profitloss1$Operations==input$Operations,c("Years","Value")]
})
output$Ratio<-renderTable({
ratiofilter<-ratios1[ratios1$Ratios==input$Ratio,c("Years","Value")]
})
output$Balancesheet<-renderTable({
Balancesheetfilter<-Balancesheet[Balancesheet$Particulars==input$Particulars,c("Years","Value")]
})
output$plot<-renderPlot({
options(scipen = 999)
p<-ggplot(data = profitloss1[profitloss1$Operations==input$Operations,]
,aes(x=Years,y=Value))
p+geom_line()+xlab("Years")+ylab("Value in Lakhs")+ggtitle("Profitloss Plot")
})
output$plot2<-renderPlot({
q<-ggplot(data = ratios1[ratios1$Ratios==input$Ratio,]
,aes(x=Years,y=Value))
q+geom_line()+xlab("Years")+ylab("value in lakhs")+ggtitle("Ratios Plot ")
})
output$plot1<-renderPlot({
q<-ggplot(data = Balancesheet[Balancesheet$Particulars==input$Particulars,]
,aes(x=Years,y=Value))
q+geom_line()+xlab("Years")+ylab("value in lakhs")+ggtitle("Balance sheet Plot")
})
})
And now i want to add one more table in a main panel but control will be from a single input.
its mean if i click on operations of profitloss1 or ratios....then that will show me two table with a single click.

Not getting graph in Shiny App in R

I am trying to create small application using Shiny. Below is the data frame for which I am trying to create.
data<-data.frame(state=c('AZ','VA','CA','AZ','VA','CA'), city=c('Phoenix','Arlington','SantaClara','Mesa','Richmond','SF'),
avg=c(10,15,16,13,14,14), date=c('01/09/2017','01/10/2017','01/11/2017','02/09/2017','02/10/2017','02/10/2017'),stringsAsFactors = FALSE)
So, I am trying to create a graph between date(x-axis) and avg(y-axis). So this graph should change based on the selection from dropdown list of State.For example, for a particular selected state, it should show cities available(in other dropdown) in that state.
Below is my code:
library(shiny)
library(ggplot2)
library(plotly)
statelist<-as.list(data$state)
citylist<-as.list(data$city)
ui <- basicPage(
# plotOutput("plot1", click = "plot_click"),
# verbatimTextOutput("info")
sidebarPanel(
selectInput("plot1", label=h3("Select State"), choices = statelist),
selectInput("plot2", label=h3("Select City"), choices = citylist)
),
plotOutput(outputId="plot")
),
server <- function(input, output, session) {
observe(
{
state <- input$plot1
updateSelectInput(session, "plot2", choices = data$city[data$state == state])
}
),
output$plot<-renderPlot({
ggplot(data[data$city == input$plot2 &
data$state == input$plot1],aes(date,avg))
+geom_line()
})
}
shinyApp(ui, server)
Dropdown is working perfectly but not getting the graph.
Thanks in advance!!
I made some minor modifications to your code:
There were some commas in places where they should not be: after the ui constructor, and after the observe constructor.
There was a comma missing in data[data$city == input$plot2 &
data$state == input$plot1,]
I edited your observe to be an observeEvent
I modified the plot to show that it actually changes, since the sample data is quite limited.
Hope this helps!
library(shiny)
library(ggplot2)
library(plotly)
data<-data.frame(state=c('AZ','VA','CA','AZ','VA','CA'), city=c('Phoenix','Arlington','SantaClara','Mesa','Richmond','SF'),
avg=c(10,15,16,13,14,14), Date=c('01/09/2017','01/10/2017','01/11/2017','02/09/2017','02/10/2017','02/10/2017'),stringsAsFactors = FALSE)
statelist<-unique(data$state)
citylist<-unique(data$city)
ui <- basicPage(
# plotOutput("plot1", click = "plot_click"),
# verbatimTextOutput("info")
sidebarPanel(
selectInput("plot1", label=h3("Select State"), choices = statelist),
selectInput("plot2", label=h3("Select City"), choices = citylist)
),
plotOutput(outputId="plot")
)
server <- function(input, output, session) {
observeEvent(input$plot1,
{
state <- input$plot1
updateSelectInput(session, "plot2", choices = data$city[data$state == state])
}
)
output$plot<-renderPlot({
data = data[data$city == input$plot2 &
data$state == input$plot1,]
ggplot(data,aes(Date,avg)) + geom_point(size=5) + ggtitle(paste0(input$plot1," - ",input$plot2 ))
})
}
shinyApp(ui, server)

Resources