I am trying to develop an application, where I have designed my dashboard with two menu sub items.
Load
Prep
In Load, I am using File input, to allow the user to upload the file.
2 .In prep, I want to display the headers from the file in the form of checkgroupbox.
For that, I have tried the below code. In the below code, I have following problems.
1. The selection of variables, I need in Prep, is available in Load.
Select variables, I want to display them in checkbox, so the user can select the variables they want.
Here is my code, Could anyone help me how I could achieve it?
ui<-dashboardPage(
dashboardHeader(title = "Model"),
dashboardSidebar(
sidebarMenu(id="tabs",
menuItem("Data", tabName = "data", icon = icon("table"),startExpanded = TRUE,
menuSubItem("Load", tabName = "data1"),
menuSubItem("Prep", tabName = "prep")
),
menuItem("Visualisation",icon=icon("bar-chart-o"), tabName = "vis"),
menuItem("Result", icon=icon("cog"), tabName = "result")
)
),
dashboardBody(
tags$style(type="text/css",
".shiny-output-error { visibility: hidden; }",
".shiny-output-error:before { visibility: hidden; }"
),
tabItems(
tabItem(tabName = "data1",
fluidPage(
fluidRow(
fileInput("file1","Choose CSV File",
accept = c("text/csv",
"text/comma-seperated-values, text/plain",
".csv")
),
tags$hr(),
checkboxInput("header", "Header", TRUE),
radioButtons("sep","Separator",
choices=c(Comma=",",
semicolon=";",
Tab="\t"),
selected = ";")
),
mainPanel(
uiOutput("tb")
)
)
)
),
tabItem(tabName = "prep",
fluidPage(
fluidRow(
h5("Select Varibales")
),
mainPanel(
uiOutput("Pre")
)
))
)
)
server <- shinyServer(function(input,output){
data <- reactive({
file1 <- input$file1
if(is.null(file1)){return()}
read.csv(file = file1$datapath, sep=input$sep)
})
output$filedf <- renderTable({
if(is.null(data())){return()}
input$file1
})
output$sum <- renderTable({
if(is.null(data())){return()}
summary(data())
})
output$table <- renderTable({
if(is.null(data())){return()}
data()
})
output$tb <- renderUI({
if(is.null(data())){return()}
tabsetPanel(tabPanel("About file", tableOutput("filedf")),tabPanel("Data", tableOutput("table")),tabPanel("Summary", tableOutput("sum")))
})
#----- Data Preparation------
output$Pre <- renderUI({checkboxGroupInput(inputId = "select_vars",
label="Select Variables",
choices = names(data))
})
data_sel <- reactive({
req(input$select_vars)
data_sel<- data()%>% select(input$select_var)
})
})
shinyApp(ui,server)
Related
I stuck in printing dynamic slider values. In the following code I tried to print the dynamic slider values but it's not possible.
library(shinydashboard)
library(DT)
ui <- dashboardPage(
dashboardHeader(title = "Dynamic slider"),
dashboardSidebar(
tags$head(
tags$style(HTML('.skin-blue .main-sidebar {
background-color: #666666;
}'))
),
sidebarMenu(
menuItem("Input data", tabName = 'input_data')
),
fileInput(
"file",
"Choose CSV File",
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv")
),
checkboxInput("header",
"Header",
value = TRUE),
radioButtons(
"disp",
"Display",
choices = c(Head = "head",
All = "all"),
selected = "head"
),
sliderInput(
inputId = 'slr',
label = 'Slider range',
min = 0,
max = 3,
value = c(0.5,3),
step = 0.5
),
selectInput(
inputId = 'var',
label = 'Variables',
'Names',
multiple = TRUE
),
uiOutput('sliders')
),
dashboardBody(tabItems(
tabItem(tabName = 'input_data',
fluidRow(
box(width = 12,
dataTableOutput('table'),
title = 'Raw data'),
box(width = 6,
verbatimTextOutput('slider1'),
title = 'slider range'),
box(width = 6,
verbatimTextOutput('slider2'),
title = 'dynamic slider value')
)
)
))
)
server <- function(input, output) {
dataset <- reactive({
req(input$file)
read.csv(input$file$datapath,header = input$header)
})
observe(
output$table <- DT::renderDataTable({
if (input$disp == 'head') {
head(dataset())
}
else{
dataset()
}
})
)
observe({
updateSelectInput(inputId = 'var',choices = c(' ',names(dataset())))
})
variables <- reactive({
input$var
})
sli <- reactive({
lapply(1:length(variables()), function(i){
inputName <- variables()[i]
sliderInput(inputName, inputName,
min = 0, max = 1, value = c(0.3,0.7))
})
})
output$sliders <- renderUI({
do.call(tagList,sli())
})
output$slider1 <- renderPrint({
input$slr
})
output$slider2 <- renderPrint({
sli()
})
}
shinyApp(ui = ui, server = server)
Any suggestions will be appreciated, Is there any other method to get dynamic sliders based on selected variables or How can we get the values of the dynamic slider here??
There may be better ways to structure your app, but here is a solution that follows your general approach. There are 4 modifications to what you already have:
There is no need to define the reactive variables when you can just use input$var directly. The proposed solution eliminates this reactive.
Using req(input$var) will prevent components dependent on that selectInput from trying to render when a selection has not been made.
Since input$var defines the id of the dynamic slider, you can use this to retrieve the slider's values (i.e., input[[input$var]]).
Since you have specified "multiple = TRUE", a few nested paste statements are used to create a single string representing the values of all (potentially multiple) dynamic sliders.
The below app includes these modifications, and I believe, achieves what you are trying to accomplish.
library(shinydashboard)
library(DT)
ui <- dashboardPage(
dashboardHeader(title = "Dynamic slider"),
dashboardSidebar(
tags$head(
tags$style(HTML('.skin-blue .main-sidebar {
background-color: #666666;
}'))
),
sidebarMenu(
menuItem("Input data", tabName = 'input_data')
),
fileInput(
"file",
"Choose CSV File",
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv")
),
checkboxInput("header",
"Header",
value = TRUE),
radioButtons(
"disp",
"Display",
choices = c(Head = "head",
All = "all"),
selected = "head"
),
sliderInput(
inputId = 'slr',
label = 'Slider range',
min = 0,
max = 3,
value = c(0.5,3),
step = 0.5
),
selectInput(
inputId = 'var',
label = 'Variables',
'Names',
multiple = TRUE
),
uiOutput('sliders')
),
dashboardBody(tabItems(
tabItem(tabName = 'input_data',
fluidRow(
box(width = 12,
dataTableOutput('table'),
title = 'Raw data'),
box(width = 6,
verbatimTextOutput('slider1'),
title = 'slider range'),
box(width = 6,
verbatimTextOutput('slider2'),
title = 'dynamic slider value')
)
)
))
)
server <- function(input, output) {
dataset <- reactive({
req(input$file)
read.csv(input$file$datapath,header = input$header)
})
observe(
output$table <- DT::renderDataTable({
if (input$disp == 'head') {
head(dataset())
}
else{
dataset()
}
})
)
observe({
updateSelectInput(inputId = 'var',choices = c(' ',names(dataset())))
})
sli <- reactive({
lapply(1:length(input$var), function(i){
inputName <- input$var[i]
sliderInput(inputName, inputName,
min = 0, max = 1, value = c(0.3,0.7))
})
})
output$sliders <- renderUI({
req(input$var)
do.call(tagList,sli())
})
output$slider1 <- renderPrint({
input$slr
})
output$slider2 <- renderPrint({
req(input$var)
paste(
sapply(
input$var,
function(x) {
paste(x, paste(input[[x]], collapse = ', '), sep = ': ')
}
),
collapse = '; '
)
})
}
shinyApp(ui = ui, server = server)
I am trying to take input from the user for group_by and count of the data on the columns selected by the user from the uploaded CSV file. In short, the user should select the columns he needs to group_by and getting the count of the data
I am able to upload the file and getting the summary in the load section, I have created a prep column for this group_by part.
library(shiny)
library(shinydashboard)
library(ggplot2)
library(DT)
ui<-dashboardPage(
dashboardHeader(title = "Model"),
dashboardSidebar(
sidebarMenu(id="tabs",
menuItem("Data", tabName = "data", icon = icon("table"),startExpanded = TRUE,
menuSubItem("Load", tabName = "data1"),
menuSubItem("Prep", tabName = "prep")
),
menuItem("Visualisation",icon=icon("bar-chart-o"), tabName = "vis"),
menuItem("Result", icon=icon("cog"), tabName = "result")
)
),
dashboardBody(
tags$style(type="text/css",
".shiny-output-error { visibility: hidden; }",
".shiny-output-error:before { visibility: hidden; }"
),
tabItems(
tabItem(tabName = "data1",
fluidPage(
fluidRow(
fileInput("file1","Choose CSV File",
accept = c("text/csv",
"text/comma-seperated-values, text/plain",
".csv")
),
tags$hr(),
checkboxInput("header", "Header", TRUE),
radioButtons("sep","Separator",
choices=c(Comma=",",
semicolon=";",
Tab="\t"),
selected = ";")
),
mainPanel(
uiOutput("tb")
)
)
)
),
tabItem(tabName = "prep",
fluidPage(
fluidRow(
mainPanel(
uiOutput("Pre")
)
)
))
)
)
server <- shinyServer(function(input,output){
data <- reactive({
file1 <- input$file1
if(is.null(file1)){return()}
read.csv(file = file1$datapath, sep=input$sep)
})
output$filedf <- renderTable({
if(is.null(data())){return()}
input$file1
})
output$sum <- renderTable({
if(is.null(data())){return()}
summary(data())
})
output$table <- renderTable({
if(is.null(data())){return()}
data()
})
output$tb <- renderUI({
if(is.null(data())){return()}
tabsetPanel(tabPanel("About file", tableOutput("filedf")),tabPanel("Data", tableOutput("table")),tabPanel("Summary", tableOutput("sum")))
})
#----- Data Preparation------
output$Pre <- renderUI({checkboxGroupInput(inputId = "select_vars",
label="Select Variables",
choices = names(filedf))
})
filedf_sel <- reactive({
req(input$select_vars)
filedf_sel<- data()%>% select(input$select_var)
})
})
shinyApp(ui,server)
the output should be the result of the group_by and count on the columns selected by the user
1) Create a place for user to choose the columns. As you're using user data for it, ?renderUI seems like an OK option for me. Something like this should do:
output$group_by_selection <- renderUI({
req(data())
selectizeInput(
'group_by_select', 'Group by', choices = colnames(data()), multiple = TRUE
),
It should be in the server.R file. Use uiOutput('group_by_selection') in ui.R to show it.
Now, you need to group and count the data after user is done selecting. You can do it by button press or whatever. It can look like this, using data.table library for easy grouping and counting:
grouped_data <- eventReactive(input$group_by_button, {
if (length(input$group_by_select) > 0 ) {
data()[, Count := .N, by = input$group_by_select]
} else {
NULL
}
})
I am creating a application, where I am uploading a file and displaying the variables from the uploaded file and changing their data type as desired from the user.
In most of the cases, they are either numeric/date.
Therefore, I tried the below piece of code. In the first half, I have created the code for file upload, and in second half, i tried to display the checkbox group input for the variables.
I have two problems with the below code.
In select variables, it is not displaying the Variable names, rather I find something with checkbox irrelevant.
On the other hand, i want them to be in my submenu item tab "prep", I have no idea why i find them in menu item Load.
Below is the screenshot, that helps you to figure out between the menu items.
Here is my code, i have tried so far.
library(shiny)
library(shinydashboard)
library(ggplot2)
library(DT)
ui<-dashboardPage(
dashboardHeader(title = "Model"),
dashboardSidebar(
sidebarMenu(id="tabs",
menuItem("Data", tabName = "data", icon = icon("table"),startExpanded = TRUE,
menuSubItem("Load", tabName = "data1"),
menuSubItem("Prep", tabName = "prep")
),
menuItem("Visualisation",icon=icon("bar-chart-o"), tabName = "vis"),
menuItem("Result", icon=icon("cog"), tabName = "result")
)
),
dashboardBody(
tags$style(type="text/css",
".shiny-output-error { visibility: hidden; }",
".shiny-output-error:before { visibility: hidden; }"
),
tabItems(
tabItem(tabName = "data1",
fluidPage(
fluidRow(
fileInput("file1","Choose CSV File",
accept = c("text/csv",
"text/comma-seperated-values, text/plain",
".csv")
),
tags$hr(),
checkboxInput("header", "Header", TRUE),
radioButtons("sep","Separator",
choices=c(Comma=",",
semicolon=";",
Tab="\t"),
selected = ";")
),
mainPanel(
uiOutput("tb")
)
)
)
),
tabItem(tabName = "prep",
fluidPage(
fluidRow(
mainPanel(
uiOutput("Pre")
)
)
))
)
)
server <- shinyServer(function(input,output){
data <- reactive({
file1 <- input$file1
if(is.null(file1)){return()}
read.csv(file = file1$datapath, sep=input$sep)
})
output$filedf <- renderTable({
if(is.null(data())){return()}
input$file1
})
output$sum <- renderTable({
if(is.null(data())){return()}
summary(data())
})
output$table <- renderTable({
if(is.null(data())){return()}
data()
})
output$tb <- renderUI({
if(is.null(data())){return()}
tabsetPanel(tabPanel("About file", tableOutput("filedf")),tabPanel("Data", tableOutput("table")),tabPanel("Summary", tableOutput("sum")))
})
#----- Data Preparation------
output$Pre <- renderUI({checkboxGroupInput(inputId = "select_vars",
label="Select Variables",
choices = names(filedf))
})
filedf_sel <- reactive({
req(input$select_vars)
filedf_sel<- data()%>% select(input$select_var)
})
})
shinyApp(ui,server)
Could anyone help me on how i can avoid those errors, and then structure out on how i could change their datatype
#----- Data Preparation------
output$Pre <- renderUI({checkboxGroupInput(inputId = "select_vars",
label="Select Variables",
choices = colnames(data())) #to retrive the data frame column names
})
filedf_sel <- reactive({
req(input$select_vars)
filedf_sel<- data()%>% select(input$select_vars) #possible typo
})
I'm not sure what you want, but I hope this is helpful in some way.
I am trying to develop an app with a dashboard design to display the summary of the table and display the data frame.
I am quite unsuccessful, in designing it. I have incorporated my own dashboard design for this.
I am missing an element in my server function and not able to see the dataframe of the uplaoded file.
Any lead would be helful.
Below is my code for the UI and Server function
UI
ui <- dashboardPage(
dashboardHeader(title = "Model"),
dashboardSidebar(sidebarMenu(
id = "tabs",
menuItem(
"Data",
tabName = "data",
icon = icon("table"),
startExpanded = TRUE,
menuSubItem("Load", tabName = "data1")
),
dashboardBody(
tags$style(
type = "text/css",
".shiny-output-error { visibility: hidden; }",
".shiny-output-error:before { visibility: hidden; }"
),
tabItems(tabItem(
tabName = "data1",
fluidPage(
fluidRow(
fileInput(
"file",
"Choose CSV File",
accept = c("text/csv",
"text/comma-seperated-values, text/plain",
".csv")
),
tags$hr(),
checkboxInput("header", "Header", TRUE),
radioButtons(
"sep",
"Separator",
choices = c(
Comma = ",",
semicolon = ";",
Tab = "\t"
),
selected = ";"
)
),
mainPanel(uiOutput("tb"))
)
))
)
)
SERVER CODE
server <- shinyServer(function(input,output){
data <- reactive({
file1 <- input$file
if(is.null(file1)){return()}
read.csv(file = file$datapath, sep=input$sep)
})
output$filedf <- renderTable({
if(is.null(data())){return()}
input$file
})
output$sum <- renderTable({
if(is.null(data())){return()}
summary(data())
})
output$table <- renderTable({
if(is.null(data())){return()}
data()
})
output$tb <- renderUI({
if(is.null(data())){return()}
tabPanel("About file", tableOutput("filedf")),tabPanel("Data", tableOutput("table")),tabPanel("Summary", tableOutput("sum"))
})
})
turns out there was a small typo in your code. You were referring to input$file and file$datapath, where that should have been input$file1 and file1$datapath. So a working version would be as shown below. Hope this helps!
library(shiny)
library(shinydashboard)
ui<-dashboardPage(
dashboardHeader(title = "Model"),
dashboardSidebar(
sidebarMenu(id="tabs",
menuItem("Data", tabName = "data", icon = icon("table"),startExpanded = TRUE,
menuSubItem("Load", tabName = "data1")
),
menuItem("Visualisation",icon=icon("bar-chart-o"), tabName = "vis"),
menuItem("Result", icon=icon("cog"), tabName = "result")
)
),
dashboardBody(
tags$style(type="text/css",
".shiny-output-error { visibility: hidden; }",
".shiny-output-error:before { visibility: hidden; }"
),
tabItems(
tabItem(tabName = "data1",
fluidPage(
fluidRow(
fileInput("file1","Choose CSV File",
accept = c("text/csv",
"text/comma-seperated-values, text/plain",
".csv")
),
tags$hr(),
checkboxInput("header", "Header", TRUE),
radioButtons("sep","Separator",
choices=c(Comma=",",
semicolon=";",
Tab="\t"),
selected = ";")
),
mainPanel(
uiOutput("tb")
)
)
)
)
)
)
server <- shinyServer(function(input,output){
data <- reactive({
file1 <- input$file1
if(is.null(file1)){return()}
read.csv(file = file1$datapath, sep=input$sep)
})
output$filedf <- renderTable({
if(is.null(data())){return()}
input$file1
})
output$sum <- renderTable({
if(is.null(data())){return()}
summary(data())
})
output$table <- renderTable({
if(is.null(data())){return()}
data()
})
output$tb <- renderUI({
if(is.null(data())){return()}
tabsetPanel(tabPanel("About file", tableOutput("filedf")),tabPanel("Data", tableOutput("table")),tabPanel("Summary", tableOutput("sum")))
})
})
shinyApp(ui,server)
I am trying to develop an application, that could help the users upload a file and display the summary statistics.
I am using the below UI and Server code to achieve this, once I have completed my file upload, i am unable to see the data frame and its summary statistics.
I am missing something with my code, but unable to guess.
ui<-dashboardPage(
dashboardHeader(title = "Claim Model"),
dashboardSidebar(
sidebarMenu(id="tabs",
menuItem("Data", tabName = "data", icon = icon("table"),startExpanded = TRUE,
menuSubItem("Load", tabName = "data1")
),
menuItem("Visualisation",icon=icon("bar-chart-o"), tabName = "vis"),
menuItem("Result", icon=icon("cog"), tabName = "result")
)
),
dashboardBody(
tags$style(type="text/css",
".shiny-output-error { visibility: hidden; }",
".shiny-output-error:before { visibility: hidden; }"
),
tabItems(
tabItem(tabName = "data1",
fluidPage(
fluidRow(
fileInput("file1","Choose CSV File",
accept = c("text/csv",
"text/comma-seperated-values, text/plain",
".csv")
),
tags$hr(),
checkboxInput("header", "Header", TRUE),
radioButtons("sep","Separator",
choices=c(Comma=",",
semicolon=";",
Tab="\t"),
selected = ";")
),
mainPanel(
tableOutput("contents")
)
)
)
)
)
)
Server Code.
server <- shinyServer(function(input,output){
output$contents <- renderTable({
req(input$file1)
df <- read.csv(input$file1$datapath,
header=input$header,
sep=input$sep)
})
})
Currently i dont have the code for displaying the statistics from data frame. any lead on how to start would be helpful
Here is an example: Inside dashboardBody(), you need a second (and third) tabItem() to "fill" the menuItem(tabName = "vis") and menuItem(tabName = "result"). And in the server, you need code to generate plots or tables or whatever you want to display. Furthermore, note how I assigned the output of read.csv() to a function (called DATA() here) so that it can be used at different places inside the server.
UI:
ui <- dashboardPage(
dashboardHeader(title = "Claim Model"),
dashboardSidebar(
sidebarMenu(id="tabs",
menuItem("Data", tabName = "data", icon = icon("table"),startExpanded = TRUE,
menuSubItem("Load", tabName = "data1")
),
menuItem("Visualisation",icon=icon("bar-chart-o"), tabName = "vis"),
menuItem("Result", icon=icon("cog"), tabName = "result")
)
),
dashboardBody(
tags$style(type="text/css",
".shiny-output-error { visibility: hidden; }",
".shiny-output-error:before { visibility: hidden; }"
),
tabItems(
tabItem(tabName = "data1",
fluidPage(
fluidRow(
fileInput("file1","Choose CSV File",
accept = c("text/csv",
"text/comma-seperated-values, text/plain",
".csv")
),
tags$hr(),
checkboxInput("header", "Header", TRUE),
radioButtons("sep","Separator",
choices=c(Comma=",",
semicolon=";",
Tab="\t"),
selected = ";")
),
mainPanel(
tableOutput("contents")
)
)
),
tabItem(tabName = "vis", h2("Two Plots"),
fluidRow(
box(
title = "Plot 1", solidHeader = TRUE, collapsible = TRUE,
plotOutput("hist1")
),
box(
title = "Plot 2", solidHeader = TRUE, collapsible = TRUE,
plotOutput("hist2")
)
)
)
)
)
)
server:
server <- function(input, output) {
DATA <- reactive({
req(input$file1)
df <- read.csv(input$file1$datapath,
header = input$header,
sep = input$sep)
return(df)
})
output$contents <- renderTable(DATA())
output$hist1 <- renderPlot({
tmp1 <- sample(which(apply(DATA(), 2, is.numeric)), 1)
hist(DATA()[, tmp1], breaks = 10, xlab = names(DATA())[tmp1], main = "")
})
output$hist2 <- renderPlot({
tmp1 <- sample(which(apply(DATA(), 2, is.numeric)), 1)
hist(DATA()[, tmp1], breaks = 20, xlab = names(DATA())[tmp1], main = "")
})
}
run:
library(shiny)
library(shinydashboard)
shinyApp(ui, server)