I would like to have my input panel as a sidebar on the left instead if below. I tried the sidebarLayout but did get it to work. Any suggestions?
Input
dput(df1.t)
structure(list(`hsa-let-7a-3p` = c(5.58182112427671, 5.21705272399953,
5.42864356356758, -1.1383057411356, 5.06203248358181), `hsa-let-7a-5p` = c(17.0260439263402,
15.2857710138151, 17.1420214989373, 15.1034766165351, 14.5449390552056
), `hsa-let-7b-3p` = c(4.28580929310353, 2.46805733598209, 5.15298557165018,
4.63298501632773, -0.398732335974934), `hsa-let-7b-5p` = c(13.0477955183433,
10.880357260433, 12.2652935281359, 11.1312184397251, 7.45844929748327
), `hsa-let-7c-3p` = c(-1.25421892418566, -1.27175388669896,
-1.33049811927568, -1.1383057411356, 2.24661989371122)), class = "data.frame", row.names = c("86",
"175", "217", "394", "444"))
dput(df2.t)
structure(list(A1BG = c(-1.19916952580483, -3.10305950914023,
-3.10305950914023, -3.10305950914023, -0.982321345582416), `A1BG-AS1` = c(0.102743641800478,
-2.24464338564074, -3.10305950914023, -1.92943473454654, -0.652692243977369
), A1CF = c(-3.10305950914023, -2.24464338564074, -2.18375470186949,
-1.92943473454654, -2.30044547836697), A2M = c(3.42115035042484,
-0.0469232989985426, 6.84141032645296, 5.78124672930663, 2.53353451576527
), `A2M-AS1` = c(-2.03481283871687, -3.10305950914023, -2.18375470186949,
-3.10305950914023, -2.64664301822906)), class = "data.frame", row.names = c("86",
"175", "217", "394", "444"))
Script
ui <- fluidPage(
titlePanel("MicroRNA-mRNA correlation plot"),
mainPanel(
plotOutput("plot")
),
selectInput(inputId ="data1",
label = "Choose Gene",
choices = names(df2.t),
selected = NULL
),
selectInput(inputId ="data2",
label = "Choose MicroRNA",
choices = names(df1.t),
selected = NULL
),
textOutput("result"))
server <- function(input,output){
data <- eventReactive(c(input$data1,input$data2),{
data <- data.frame(df1.t[[input$data2]], df2.t[[input$data1]])
colnames(data) <- c("col1", "col2")
data
})
output$plot <- renderPlot({
ggplot(data(),aes(x=col2,y=col1)) +
geom_point(colour='black') +
labs(x = paste(input$data1,"(cpm, log2)"), y = paste(input$data2,"(cpm, log2)")) +
theme_classic(base_size = 12) +
geom_smooth(method="lm",se = F) +
stat_cor()
}, height = 400, width = 600)
}
shinyApp(ui=ui,server=server)
Try this:
ui <- fluidPage(
titlePanel("MicroRNA-mRNA correlation plot"),
sidebarLayout(
sidebarPanel(
selectInput(inputId ="data1",
label = "Choose Gene",
choices = names(df2.t),
selected = NULL
),
selectInput(inputId ="data2",
label = "Choose MicroRNA",
choices = names(df1.t),
selected = NULL
)
),
mainPanel(
plotOutput("plot"),
textOutput("result")
)
)
)
Related
I've managed to filter a DataTable with two SelectInput in my code. But, when I try to do the same in my boxplot and histogram it doesn't seem to filter. Is there, by any chance, someone to help me with this? I'm new to shiny.
My code is something like this:
afastamentos <- readr::read_csv("base_afastamentos.csv", locale = locale(encoding = "latin1"))
colnames(afastamentos) <- c(
"Descrição do Cargo", "Nome do Órgão de Origem", "UF", "Cidade da Residência",
"Nível da Escolaridade", "Início do Afastamento", "Ano/Mês Referência",
"Valor do Rendimento Líquido", "Descrição do Afastamento", "Ano Início Afastamento",
"Mês Início Afastamento", "Rendimento Líquido Hora")
ui <- dashboardPage(
dashboardHeader(title = "COBRADI",
titleWidth = 500,
tags$li(class = "dropdown",
tags$a(href = "https://www.ipea.gov.br/portal/index.php?option=com_content&view=article&id=39285&Itemid=343",
icon("globe", lib = "glyphicon"),
"Site COBRADI",
target = "_blank"))
),
dashboardSidebar(
sidebarMenu(
id = "sidebar",
menuItem("Dataset",
tabName = "data",
icon = icon("database")),
menuItem("Visualização",
tabName = "viz",
icon = icon("chart-line")),
menuItem("Informações",
tabName = "info",
icon = icon("info-circle"))
)
),dashboardBody(
tabItems(
tabItem(tabName = "viz",
tabBox(id = "t2", width = 12,
tabPanel(title = "Distribuição Amostral",
icon = icon("fas fa-chart-area"),
value = "trends",
fluidRow(
column(width = 12,
box(title = "Filtros", width = "100%",
column(width = 6,
box(width = "100%",
selectizeInput(inputId = "select_UF",
label = "Estados:",
choices = c("TODOS", unique(afastamentos$UF)),
multiple = T,
selected = "TODOS"))
),
column(width = 6,
box(width = "100%",
selectizeInput(inputId = "descricao_2",
label = "Descrição do Afastamento:",
choices = c("TODOS", unique(afastamentos$`Descrição do Afastamento`)),
multiple = T, options = list(maxItems = 5),
selected = "TODOS"))),
)
)
),
fluidRow(
column(width = 12,
box(title = "BoxPlot - Valor do Rendimento Bruto Mensal",
status = "primary",
solidHeader = TRUE,
collapsible = TRUE,
width = "100%",
plotlyOutput("boxplot"))
),
column(width = 12,
box(title = "Histograma - Valor do Rendimento Bruto Mensal",
status = "primary",
solidHeader = TRUE,
collapsible = TRUE,
width = "100%",
plotlyOutput("histplot")))
)
)
)
)
)
)
)
And the server is:
server <- function(input, output, session){
meus_dados <- reactive({
## filtro UF
print(input)
if (! "TODOS" %in% input$select_UF){
a <- a |>
filter(`UF` %in% input$select_UF)
}
#Filtro Descricao
if(! "TODOS" %in% input$descricao_2){
a <- a |>
filter(`Descrição do Afastamento` %in% input$descricao_2)
return(a)
}
})
output$boxplot <- renderPlotly({
boxplot <- meus_dados()|>
plot_ly() |>
add_boxplot(~`Valor do Rendimento Líquido`) |>
layout(xaxis = list(title = "Valor do Rendimento Bruto"))
})
output$histplot <- renderPlotly({
hist <- meus_dados() |>
plot_ly() |>
add_histogram(~`Rendimento Líquido Hora`) |>
layout(xaxis = list(title = "Valor da Hora Técnica"))})
}
And I get the following error: First argument data must be a data frame or shared data.
Data is available here: https://www.dropbox.com/s/kjilkkskggi27vo/base_afastamentos.csv?dl=0
Your reactive object was the problem. This works fine for me using the original names.
ui <- dashboardPage(
dashboardHeader(title = "COBRADI",
titleWidth = 500,
tags$li(class = "dropdown",
tags$a(href = "https://www.ipea.gov.br/portal/index.php?option=com_content&view=article&id=39285&Itemid=343",
icon("globe", lib = "glyphicon"),
"Site COBRADI",
target = "_blank"))
),
dashboardSidebar(
sidebarMenu(
id = "sidebar",
menuItem("Dataset",
tabName = "data",
icon = icon("database")),
menuItem("Visualização",
tabName = "viz",
icon = icon("chart-line")),
menuItem("Informações",
tabName = "info",
icon = icon("info-circle"))
)
),dashboardBody(
tabItems(
tabItem(tabName = "viz",
tabBox(id = "t2", width = 12,
tabPanel(title = "Distribuição Amostral",
icon = icon("fas fa-chart-area"),
value = "trends",
fluidRow(
column(width = 12,
box(title = "Filtros", width = "100%",
column(width = 6,
box(width = "100%",
selectizeInput(inputId = "select_UF",
label = "Estados:",
choices = c("TODOS", unique(afastamentos$UF_da_UPAG_de_vinculacao)),
multiple = T,
selected = "TODOS"))
),
column(width = 6,
box(width = "100%",
selectizeInput(inputId = "descricao_2",
label = "Descrição do Afastamento:",
choices = c("TODOS", unique(afastamentos$Descricao_do_afastamento)),
multiple = T, options = list(maxItems = 5),
selected = "TODOS"))),
)
)
),
fluidRow(
column(width = 12,
box(title = "BoxPlot - Valor do Rendimento Bruto Mensal",
status = "primary",
solidHeader = TRUE,
collapsible = TRUE,
width = "100%",
plotlyOutput("boxplot"))
),
column(width = 12,
box(title = "Histograma - Valor do Rendimento Bruto Mensal",
status = "primary",
solidHeader = TRUE,
collapsible = TRUE,
width = "100%",
plotlyOutput("histplot")))
)
)
)
)
)
)
)
server <- function(input, output, session){
meus_dados <- reactive({
## filtro UF
print(input)
a <- afastamentos
if (! "TODOS" %in% input$select_UF){
a <- a |>
filter(UF_da_UPAG_de_vinculacao %in% input$select_UF)
}
#Filtro Descricao
if(! "TODOS" %in% input$descricao_2){
a <- a |>
filter(Descricao_do_afastamento %in% input$descricao_2)
}
return(a)
})
output$boxplot <- renderPlotly({
boxplot <- meus_dados()|>
plot_ly() |>
add_boxplot(~Valor_rendimento_liquido) |>
layout(xaxis = list(title = "Valor do Rendimento Bruto"))
})
output$histplot <- renderPlotly({
hist <- meus_dados() |>
plot_ly() |>
add_histogram(~Rendimento_Liquido_Hora) |>
layout(xaxis = list(title = "Valor da Hora Técnica"))})
}
shinyApp(ui = ui, server = server)
I want to fetch user choice from selectInput and store it as a string to be use as filename to save a plot. If user change selectInput choice, the string variable should also update to reflect change.
Here are my code so far and the xxx variable obviously is not a string. Can anyone assist?
pacman::p_load(dplyr, tidyverse, reshape, ggplot2, shiny, shinydashboard)
mtcars_colName <- colnames(mtcars)
x_coord <- mtcars_colName[c(1:2)]
y_coord <- mtcars_colName[c(3:7)]
#Put plots on shiny ui
ui <- dashboardPage(
dashboardHeader(title = 'mtcars data'),
dashboardSidebar(
sidebarMenu(
menuItem("mtcars data comparison", tabName = 'mtcars_data_comparison', icon = icon('dragon'))
)
),
dashboardBody(
tabItems(
tabItem('mtcars_data_comparison',
fluidPage(
downloadButton("downloadPlot", "Download mtcars plot"),
box(plotOutput('metrics_plot'), width = 8, height = '100%'),
box(selectInput('y_metrics', 'mtcars y-axis', choices = y_coord), width = 4),
box(selectInput('x_metrics', 'mtcars x-axis', choices = x_coord), width = 4)
),
)
)
)
)
server <- function(input, output, session){
mtcars_plot <- reactive({ggplot(mtcars, aes_string(x=input$x_metrics, y=input$y_metrics)) +
geom_jitter(width =0.05) +
scale_y_continuous(labels = scales::comma) +
theme(
axis.text.x = element_blank(),
axis.line = element_line(),
axis.ticks.x = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank())
})
output$metrics_plot <- renderPlot({
mtcars_plot()
})
rv <- reactiveValues(value_store = character())
observeEvent(input$y_metrics, {
rv$value_store <- input$y_metrics
})
output$download10XPlot <- downloadHandler(
file = paste(rv$value_store, '.pdf', sep=''),
content = function(file) {
sep <- switch(input$filetype, "csv" = ",", "tsv" = "\t")
# pdf(file = file, width = 11, height = 8.5)
pdf(file, sep = sep)
print(TenX_plot())
dev.off()}
)
}
shinyApp(ui, server)
We can try
output$download10XPlot <- downloadHandler(
file = function() {paste(isolate(input$y_seq_metrics), '.pdf', sep='')},
content = function(file) {
pdf(file = file, width = 11, height = 8.5)
print(TenX_plot())
dev.off()}
)
How can I use a download button instead of hc_exporting function to download a highchart in shiny?
library(shiny)
library(shinydashboard)
library(highcharter)
library(shinyWidgets)
RecruitmentFunneldb_struct <-
structure(list(
yyyy = c(2019L, 2019L, 2019L, 2019L, 2019L, 2019L),
stages = c(
"Phone Scrining",
"Interview",
"Offer",
"Pre-Onboarding",
"Post-Joining",
"Joined"
),
pop = c(8L, 25L, 23L, 32L, 8L, 4L)
),
row.names = c(NA,
6L),
class = "data.frame")
ui <-
dashboardPage(
dashboardHeader(
title = HTML("Analytic view - Recruitment"),
titleWidth = 280
),
dashboardSidebar(disable = T),
dashboardBody(fluidPage(fluidRow(
box(
title = fluidRow(
column(10, "Recruitment Funnel"),
column(
2,
align = "right",
downloadButton("download", label = NULL, class = "butt1"),
tags$head(
tags$style(
".butt1{display: inline-block;} .butt1{font-size: 20px;} .butt1{border: none;} .butt1{padding-top: 1px} .butt1{background-color: transperent .butt1{padding-right: 50px}}"
)
)
)
),
solidHeader = T,
width = 4,
collapsible = F,
highchartOutput("Recruitment_Funnel", height = "240px")
)
)))
)
server <- function(input, output, session) {
output$Recruitment_Funnel <- renderHighchart({
Reserve_Data <- RecruitmentFunneldb_struct %>% arrange(-pop)
Reserve_Data %>%
hchart("funnel", hcaes(x = stages, y = pop))
})
output$download <- downloadHandler(
filename = function() {
paste("Funnel", ".", "pdf")
},
content = function(file) {
pdf(file)
output$Recruitment_Funnel()
dev.off()
}
)
}
shinyApp(ui, server)
I wanted to create fluidPage as shown in the image above.
Here is my code for ui.R:
shinyUI(fluidPage(
fluidRow(
column(6,
selectInput(inputId="StoreName", label=h3("Choose Store"),choices = vStores),
),
column(6,
strong(h3("Latest Orders Status")),
DT::dataTableOutput('getLatestOrdStatus'),
style = "height:500px; overflow-y: scroll;"
)
),
fluidRow(
column(6,
selectInput(inputId="OrderType", label=h3("Choose Order Type"),choices = vOrdTypes)
)
),
fluidRow(
column(5, h4("Daily Orders Count By Order Type"),
dateRangeInput(inputId="daterange", label="Pick a Date Range:", start = Sys.Date()-30,
end = Sys.Date()),
plotOutput("OrdPlotByType")
)
)
)
)
The below code will give you similar layout. Further you can improve by exploring this link from shiny
library(shiny)
library(DT)
library(ggplot2)
data(mtcars)
ui <- fluidPage(
fluidRow(column(12, style = "background-color:#999999;",
fluidRow(column(6,
fluidRow( column(6, selectInput(inputId = "StoreName", label = h3("Select Input 1"),choices = c('a', 'b')))),
fluidRow(column(6, selectInput(inputId = "OrderType", label = h3("Select Input 2"),choices = c('a', 'b')))),
fluidRow(column(12, h4("Plot Output"), plotOutput('plot'))
)) ,
column(6, strong(h3("Table Output")),dataTableOutput('table')
)
)
)
)
)
server <- function(input, output) {
data <- data.frame(
name = c("A","B","C","D","E") ,
value = c(3,12,5,18,45) )
output$table <- renderDataTable(head(mtcars))
output$plot <- renderPlot(
ggplot(data, aes(x = name, y = value)) +
geom_bar(stat = "identity")
)
}
shinyApp(ui, server)
I am creating a Shinyapp in line of below template :
library(shinydashboard)
sidebar <- dashboardSidebar(
sidebarMenu(id="tabs",
menuItem("Tab1", tabName="Tab1", selected=TRUE),
menuItem("Tab2", tabName = "Tab2")
),
conditionalPanel("input.tabs=='Tab1'",
fluidRow()
),
conditionalPanel("input.tabs=='Tab2'",
fluidRow()
)
)
body <- dashboardBody(
tabItems(
tabItem(tabName = "Tab1",
fluidRow( sliderInput("aa", "aa", value = 0.9, min = 0, max = 2, step=0.1))
),
tabItem(tabName = "Tab2",
fluidRow(navbarPage(id = 'ab', title = "",
tabPanel(title = "aa1", value = 'aa1', fluidRow()),
tabPanel(title = "aa2", value = 'aa2', fluidRow())))
)))
ui = dashboardPage(
dashboardHeader(title = "My tab"),
sidebar,
body
)
server = function(input, output) {}
shinyApp(ui = ui, server = server)
Now what I want basically, a sliderInput should appear in the Side panel only when TabPanel = 'aa2' from tabItem = 'Tab2'. Therefore user should not see that sliderInput if TabPanel = 'aa1' is selected.
So far I have tried something like below :
sidebar <- dashboardSidebar(
sidebarMenu(id="tabs",
menuItem("Tab1", tabName="Tab1", selected=TRUE),
menuItem("Tab2", tabName = "Tab2")
),
conditionalPanel("input.tabs=='Tab1'",
fluidRow()
),
conditionalPanel("input.tabs=='Tab2'",
fluidRow(sliderInput("aa5", "aa", value = 0.9, min = 0, max = 2, step=0.1))
)
)
However with this the sliderInput is visible for both tabPanel = "aa1" & tabPanel = "aa2", which I do not want.
Any suggestion, how can I make sliderInput is visible only for tabPanel = "aa2".
Thanks for your pointer.
#SBista thanks for pointer. Below is updated code. Thanks,
library(shinydashboard)
library(shinyjs)
sidebar <- dashboardSidebar(
sidebarMenu(id="tabs",
menuItem("Tab1", tabName="Tab1", selected=TRUE),
menuItem("Tab2", tabName = "Tab2")
),
conditionalPanel("input.tabs=='Tab1'",
fluidRow()
),
conditionalPanel("input.tabs=='Tab2'",
fluidRow(useShinyjs(),
column(12, sliderInput("aa4", "aa", value = 0.9, min = 0, max = 2, step=0.1)))
)
)
body <- dashboardBody(
tabItems(
tabItem(tabName = "Tab1",
fluidRow( sliderInput("aa", "aa", value = 0.9, min = 0, max = 2, step=0.1))
),
tabItem(tabName = "Tab2",
fluidRow(navbarPage(id = 'ab', title = "",
tabPanel(title = "aa1", value = 'aa1', fluidRow()),
tabPanel(title = "aa2", value = 'aa2', fluidRow())))
)))
ui = dashboardPage(
dashboardHeader(title = "My tab"),
sidebar,
body
)
server = function(input, output) {
observe(toggle(id = "aa4", condition = ifelse(input$ab == 'aa2', TRUE, FALSE)))
}
shinyApp(ui = ui, server = server)