How to print the summary of a variable in R shiny? - r

I would like that based on a selectInput() which the client can select, the summary of the selected variable will be print in a box. My code for the ui.R is:
box(
title = "Informed Investor",
status = "primary",
solidHeader = TRUE,
width = 6,
selectInput("informedDset", label="Select Category", choices = list("Informed Full" = "InformedFull", "Informed Fact" = "InformedFact", "Informed Fact Positive" = "InformedFact.Pos", "Informed Fact Negative" = "InformedFact.Neg", "Informed Emotions" = "InformedEmotions", "Informed Emotions Fact" = "InformedEmotionsFact"), selected = "Informed Full")
),
box(
title = "Data Table",
status = "warning",
solidHeader = TRUE,
width = 6,
height = 142,
verbatimTextOutput("summaryDset")
)
And my code for server.R:
output$summaryDset <- renderPrint({
summary(input$informedDset)
})

As indicated in the comments, summary returns Length Class Mode 1 character character because the input$informedDset is a character string.
If you want to extract the summary of one selected variable in a dataset you can find an reproducible example below with the iris dataset :
library(shiny)
library(shinydashboard)
ui=fluidPage(
box(title = "Informed Investor",
status = "primary",
solidHeader = TRUE,
width = 6,
selectInput("informedDset", label="Select Category",
choices = list("Sepal.Length"="Sepal.Length",
"Sepal.Width"="Sepal.Width",
"Petal.Length"="Petal.Length",
"Petal.Width"="Petal.Width",
"Species"="Species"), selected = "Sepal.Length")),
box(
title = "Data Table",
status = "warning",
solidHeader = TRUE,
width = 6,
height = 142,
verbatimTextOutput("summaryDset")))
server = function (input,output){
output$summaryDset <- renderPrint({
summary(iris[[input$informedDset]])
})}
shinyApp(ui, server)
Is that what you want to do ?

Related

R Shiny using navbarPage and DataTable -

I am trying to rendering the data table in Shiny using "DT" option in R. But, not sure, what is wrong with the patch. Tried everything but "the table is not getting displayed on the page". Any help appreciated.
Below is the code for that page -
navbarMenu("Data Tracker", icon = icon("database"),
tabPanel("A", fluid = TRUE,
sidebarLayout(
sidebarPanel(width = 3,
dateRangeInput("daterange1", "Date range:",
start = "2000-01-01",
end = "2010-12-31"),
#### Time Frame and Data Type Selector
selectInput(inputId = "TimeFrame",
choices = c("1","2"),
selected = "1",
width = "220px")
),
fluidRow(
column(12,
box(
title = "Data Secription - ",
closable = TRUE,
width = 12,
status = "primary",
solidHeader = TRUE,
collapsible = TRUE,
p("Box Content")
),
valueBox(10 * 2, "D"),
valueBox(10 * 2, "E"),
valueBox(10 * 2, "F?"),
))),
mainPanel(width = 12,
DT::dataTableOutput("table1")
))

Issue with UI side of Shiny app with data table

I am in the process of creating a shiny app for a process at work and am struggling to figure something out on the UI side of the app. I would like to display a data table next to a sidebar menu containing options for the app. The issue is that when I do so, the data table is pushed down below the sidebar panel instead of beside it (see the original data tab).
I found a work around as seen in the suggested tab, but that comes with its own issues. I need to be able to lock the column headers while scrolling through the app and when the data table is inside the box element, I am unable to find a way to do so.
Here is the code to a simplified version of the app.
library(shiny)
library(lubridate)
library(tidyverse)
library(DT)
library(shinydashboard)
library(shinythemes)
library(sortable)
library(reactlog)
ui<- dashboardPage(
#this gives you the name displayed on the tab
dashboardHeader(title = "HHS Resin Purchasing 0.99"),
#this gives you your sidebar (page) options
dashboardSidebar(
sidebarMenu(
menuItem("Original Data", tabName = "original"),
menuItem("Suggested", tabName = "suggested")
)
),
#this is the body of the webpages
dashboardBody(
#this gives you the body options that are displayed on every page
sidebarPanel(width = 2,
h2("Menu Options"),
h4(strong("Upload Data:")),
fileInput("file", "Data", buttonLabel = "Upload..."),
textInput("delim", "Delimiter (leave blank to guess)", ""),
numericInput("skip", "Rows to skip", 0, min = 0),
h4(strong("User Options:")),
selectInput("plant", "Select a Plant", choices =
c("All")),
dateInput("latest_date", "Select the latest W_LEAD date in the data",
value = Sys.Date()),
numericInput("avg_multiple", "Multiple of Daily Useage for Cuttoff",21, min = 1, max = 50),
h4(strong("Download Options:")),
actionButton("complete_orders", "Analysis for plant orders complete"),
actionButton("complete_checks", "Mid month check complete"),
downloadButton("downloadData1", label = "Download Suggested Orders...", class = "btn-block"),
downloadButton("downloadData2", label = "Download Flags...", class = "btn-block"),
downloadButton("downloadData3", label = "Download Full Suggested Orders Data...", class = "btn-block")
),
#This is the actual data that fills those page options listed above
tabItems(
tabItem(tabName = "original",
DT::dataTableOutput(outputId = "preview1")
),
tabItem(tabName = "suggested",
box(title = "Suggested Orders",width = 9, status = "primary", height = "auto",
solidHeader = T, dataTableOutput("preview2"), style = "max-height:800px; overflow-y: scroll;overflow-x: scroll;")
)
)
)
)
server <- function(input, output) {
output$preview1 <- renderDataTable({
DT::datatable(iris, options = list(searching = T, pageLength = 20, lengthMenu = c(5,10,15, 20))
})
output$preview2 <- renderDataTable({
DT::datatable(iris, options = list(searching = T, pageLength = 20, lengthMenu = c(5,10,15, 20))
})
}
shinyApp(ui, server)
Help in fixing either of the issues outlined above would be very appreciated! Thanks in advance.
I think using the column() function will support your first question of the datatable moving under the sidebar sidebarPanel. Please see example below.
I think the second request of freezing the row header in the datatable can be resolved with the advice found at Freezing header and first column using data.table in Shiny
library(shiny)
library(lubridate)
library(tidyverse)
library(DT)
library(shinydashboard)
library(shinythemes)
library(sortable)
library(reactlog)
ui<- dashboardPage(
#this gives you the name displayed on the tab
dashboardHeader(title = "HHS Resin Purchasing 0.99"),
#this gives you your sidebar (page) options
dashboardSidebar(
sidebarMenu(
menuItem("Original Data", tabName = "original"),
menuItem("Suggested", tabName = "suggested")
)
),
#this is the body of the webpages
dashboardBody(
#this gives you the body options that are displayed on every page
fluidRow(
column(width = 2,
sidebarPanel(width = 2,
h2("Menu Options"),
h4(strong("Upload Data:")),
fileInput("file", "Data", buttonLabel = "Upload..."),
textInput("delim", "Delimiter (leave blank to guess)", ""),
numericInput("skip", "Rows to skip", 0, min = 0),
h4(strong("User Options:")),
selectInput("plant", "Select a Plant", choices =
c("All")),
dateInput("latest_date", "Select the latest W_LEAD date in the data",
value = Sys.Date()),
numericInput("avg_multiple", "Multiple of Daily Useage for Cuttoff",21, min = 1, max = 50),
h4(strong("Download Options:")),
actionButton("complete_orders", "Analysis for plant orders complete"),
actionButton("complete_checks", "Mid month check complete"),
downloadButton("downloadData1", label = "Download Suggested Orders...", class = "btn-block"),
downloadButton("downloadData2", label = "Download Flags...", class = "btn-block"),
downloadButton("downloadData3", label = "Download Full Suggested Orders Data...", class = "btn-block")
)
),
#This is the actual data that fills those page options listed above
column(width = 6,
tabItems(
tabItem(
tabName = "original",
DT::dataTableOutput("preview1",
options = list(dom = 't',
scrollX = TRUE,
paging=FALSE,
fixedHeader=TRUE,
fixedColumns = list(leftColumns = 1, rightColumns = 0)))
),
tabItem(tabName = "suggested",
box(title = "Suggested Orders",width = 9, status = "primary", height = "auto",
solidHeader = T, dataTableOutput("preview2"), style = "max-height:800px; overflow-y: scroll;overflow-x: scroll;")
)
)
)
)
)
)
server <- function(input, output) {
output$preview1 <- renderDataTable({
DT::datatable(iris, options = list(searching = T, pageLength = 20, lengthMenu = c(5,10,15, 20)))
})
output$preview2 <- renderDataTable({
DT::datatable(iris, options = list(searching = T, pageLength = 20, lengthMenu = c(5,10,15, 20)))
})
}
shinyApp(ui, server)

Error in loading data for scatter plot in shinydashboard

I am trying to create scatter plot outputs in the shiny dashboard. I have similar datasets for several years, and I want to plot according to chosen variables and year. Datasets file name is Y96Total8.rda, Y97Total8.rda... Datasets name is Total (data.table).
Unfortunately, I can't load the dataset in a true way to plot the results, and I have the error "non-numeric argument to mathematical function" in plot tab.
If anyone has any suggestions on how to produce this plot using the shiny dashboard it would be much appreciated.
I have attached the code.
library(shiny)
library(shinydashboard)
library(data.table)
library(ggplot2)
library(plotly)
library(DT)
header <- dashboardPage(
skin = "green",
dashboardHeader(title = "TEST"),
dashboardSidebar(sidebarMenu(
dir = "ltr",
align = "right",
menuItem("Correlation", tabName = "Correlation", icon = icon("users"))
)),
dashboardBody(load(file = "data/Test.rda"),
dir = "ltr",
tabItems(
tabItem(tabName = "Correlation",
fluidRow(tabsetPanel(
tabPanel(
"Inputs",
box(
status = "danger",
solidHeader = TRUE,
width = 6,
title = "Food Expenditures Per",
sliderInput(
inputId = "Food_Expenditures_Per2",
label = "Food Expenditures",
min = 0,
max = 30000000,
value = c(1000000, 10000000)
)
),
box(
status = "danger",
solidHeader = TRUE,
title = "Total Expenditures Per",
width = 6,
sliderInput(
inputId = "Total_Exp_Month_Per2",
label = "Total Expenditures Per",
min = 0,
max = 100000000,
value = c(1000000, 30000000)
)
),
box(
status = "info",
solidHeader = TRUE,
title = "First Variable",
width = 6,
selectInput(
"Var1",
"First Variable",
list("FoodExpenditure_Per", "Total_Exp_Month_Per"),
selected =
"FoodExpenditure_Per"
)
),
box(
status = "info",
solidHeader = TRUE,
title = "Second Variable",
width = 6,
selectInput(
"Var2",
"Second Variable",
list("FoodExpenditure_Per", "Total_Exp_Month_Per"),
selected =
"Total_Exp_Month_Per"
)
),
box(
status = "info",
solidHeader = TRUE,
title = "Year",
width = 6,
selectInput(
inputId = "slcT2Year3",
label = "Year",
choices =
list(1390, 1391, 1392, 1393,
1394, 1395, 1396, 1397),
selected =
1396
)
),
box(
status = "info",
solidHeader = TRUE,
title = "Add line of best fit",
width = 6,
checkboxInput("fit", "Add line of best fit")
),
),
tabPanel(
"Plot"
,
box(
status = "info",
solidHeader = TRUE,
width = 700,
height = 450,
plotOutput("scatterplot", width =
600, height = 400)
,
downloadButton("downloadPlot3", "Download")
)
)
)))
))
)
app_server <- function(input, output, session) {
##################### Scatter Plot #########################
output$scatterplot <- renderPlot({
y <- input$slcT2Year3
fn3 <- paste0("data/Y", substr(y, 3, 4), "Total8.rda")
load(fn3)
Total <- subset(
Total,
FoodExpenditure_Per >= input$Food_Expenditures_Per2[1] &
FoodExpenditure_Per <= input$Food_Expenditures_Per2[2] &
Total_Exp_Month_Per >= input$Total_Exp_Month_Per2[1] &
Total_Exp_Month_Per <= input$Total_Exp_Month_Per2[2]
)
p <- ggplot(Total, aes(input$Var1, input$Var2)) +
geom_point() +
scale_x_log10()
if (input$fit == TRUE) {
p <- p + geom_smooth(method = "lm")
}
p
})
session$onSessionEnded(function() {
stopApp()
# q("no")
})
}
shinyApp(header, app_server)
Image of the error:
Your ggplot call should be changed to
p <- ggplot(Total, aes(Total[[input$Var1]], Total[[input$Var2]]))

shinydashboard column layout of box()

I have a shinydashboard that looks like this:
# Packages
library(shinydashboard)
library(tidyverse)
library(readxl)
library(scales)
theme_set(theme_light())
header <- dashboardHeader(
title = "Test App",
titleWidth = 215
)
sidebar <- dashboardSidebar(
sidebarMenu(
menuItem("Test Tab", tabName = "test_tab",
icon = icon("paper-plane"), startExpanded = TRUE)
)
)
body <- dashboardBody(
tabItems(
tabItem(tabName = "test_tab",
fluidRow(
column(width = 4,
h2("Column X"),
valueBoxOutput("first_value", width = NULL),
box(flexdashboard::gaugeOutput("second_value", width = "90%", height = "100px"),
title = "Second Value", status = "primary", solidHeader = TRUE,
collapsible = FALSE, width = NULL
)
),
column(width = 8,
h2("Column Y"),
box(
title = "#3", status = "primary", solidHeader = TRUE,
collapsible = FALSE, width = 4
),
box(
title = "#4", status = "primary", solidHeader = TRUE,
collapsible = FALSE, width = 4
)
)
),
fluidRow(
h2("Row A"),
column(width = 12,
box(title = "Third Value", status = "primary", solidHeader = TRUE,
width = 2.4),
box("Fourth Value", status = "primary", solidHeader = TRUE,
width = 2.4),
box("Fifth Value", status = "primary", solidHeader = TRUE,
width = 2.4),
box("Sixth Value", status = "primary", solidHeader = TRUE,
width = 2.4),
box("Seventh Value", status = "primary", solidHeader = TRUE,
width = 2.4)
)
)
)
)
)
# Put them together into a dashboardPage
ui <- dashboardPage(skin = "blue", header = header,
sidebar = sidebar,
body = body)
server <- function(input, output) {
output$first_value <- renderValueBox({
valueBox(
comma_format()(100000),
subtitle = "First Value",
icon = icon("list"), color = "purple"
)
})
output$second_value = flexdashboard::renderGauge({
flexdashboard::gauge(0.12 * 100,
symbol = '%',
min = 0,
max = 100)
})
}
shinyApp(ui, server)
I'm trying to get the box()es below RowA to line up in a five column format, like this:
Third Value | Fourth Value | Fifth Value | Sixth Value
I'm not sure how I'd do that here. I've tried to put 5 boxes inside column() inside a fluidRow() which should work, but unfortunately, the boxes keep on showing up horizontally...
Is there anyway to display the boxes in column format? If not, can you guide me to other functions that resemble what I have?
You've specified your column widths for each of these five boxes to be 2.4 (12/5 I guess).
Column widths should be whole numbers. If you substitute 2 for 2.4 it'll work fine.
Note that you will have an empty column to the right of box 5 corresponding to the 2 (12 - 2*5) positions left over.
This is based on bootstrap column layout. For more information see this tutorial.

Error in dynamically choosing a dataset in shiny dashboard

I am a newbie to shiny dashboard. I want to dynamically select a dataset among different datasets uploaded and use it to display the dataset.
I have written the below code but i am getting an error,
Warning: Error in DT::datatable: 'data' must be 2-dimensional (e.g. data frame or matrix)
ui
ui <- dashboardPage(skin = "yellow",
dashboardHeader(title = "Analytics Workbench 2.0", titleWidth = 250,
dropdownMenuOutput("msgs")),
dashboardSidebar(
sidebarMenu(
fileInput("Table1", "Train Data"),
fileInput("Table2", "Test Data"),
menuItem("Variable Analysis", icon = icon("edit"),
menuSubItem("Uni-Variate Analysis"),
menuSubItem("Multi-Variate Analysis"))
)
),
dashboardBody(
fluidPage(
fluidRow(
column(12, box(title = "Train Data", width = 6, solidHeader = TRUE, status = "primary",
collapsible = TRUE, DT::DTOutput("dtable1")),
box(title = "Test Data", width = 6, solidHeader = TRUE, status = "primary",
collapsible = TRUE, DT::DTOutput("dtable2")))),
fluidRow(
column(12, box(title = "Structure", width = 6, solidHeader = TRUE, status = "primary",
collapsible = TRUE, verbatimTextOutput("str1")),
box(title = "Structure", width = 6, solidHeader = TRUE, status = "primary",
collapsible = TRUE, verbatimTextOutput("str2"))))
)
)
)
server
server <- function(input, output) {
Train <- reactive({
if (is.null(input$Table1)) return(NULL)
read.table(input$Table1$datapath, fill = TRUE, header=T, sep=",", na.strings = c(""," ",NA))
})
Test <- reactive({
if (is.null(input$Table2)) return(NULL)
read.table(input$Table2$datapath, fill = TRUE, header=T, sep=",", na.strings = c(""," ",NA))
})
dataset_1 <- reactive({
switch(input$Datasets,
"Train" = Train,
"Test" = Test)
})
output$dtable2 <- DT::renderDT({
DT::datatable(dataset_1(), options = list(scrollX = TRUE))
}) }
Please help me solve this issue.
Thanks Balaji

Resources