Using Fileinput to upload files - r

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)

Related

Select column from dataframe using shiny widget and modify its name using textInput()

In the app below I want to select a column name from the selectInput() and then modify it using the textInput().The update will happen after the actionButton click.
library(shiny)
library(shinydashboard)
library(DT)
iris2 <- iris # make a copy, don't want mess up internal dataset
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
fileInput("file1", "Choose CSV File",
accept = c(
"text/csv",
"text/comma-separated-values,text/plain",
".csv")
),
uiOutput("column"),
textInput("text", label = "Set column name", placeholder = "Enter text..."),
actionButton("sub","submit")
),
dashboardBody(
dataTableOutput("process")
)
)
server <- function(input, output) {
raw<-reactive({
inFile <- input$file1
if (is.null(inFile))
return(NULL)
read.csv(inFile$datapath, header = T)
})
raw2<-reactive({
inFile <- input$file1
if (is.null(inFile))
return(NULL)
read.csv(inFile$datapath, header = T)
})
output$column<-renderUI({
selectInput("col","Pick a column to change its name",
choices = colnames(raw2()))
})
mydf <- reactiveValues(df = raw2(), names = names(raw2()))
observeEvent(input$sub, {
req(input$text)
mydf$names[mydf$names == input$col] <- input$text
names(mydf$df) <- mydf$names
updateSelectInput(inputId = "col", choices = mydf$names)
})
output$process<-renderDataTable({
mydf$df
})
}
shinyApp(ui, server)
Here is how to fix
library(shiny)
library(shinydashboard)
library(DT)
iris2 <- iris # make a copy, don't want mess up internal dataset
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
selectInput("col","Pick a column to change its name",
choices = colnames(iris2)),
textInput("text", label = "Set column name", placeholder = "Enter text..."),
actionButton("sub","submit")
),
dashboardBody(
tags$style(type="text/css",
".shiny-output-error { visibility: hidden; }",
".shiny-output-error:before { visibility: hidden; }"
),
dataTableOutput("process")
)
)
server <- function(input, output) {
mydf <- reactiveValues(df = iris2, names = names(iris2))
observeEvent(input$sub, {
req(input$text)
mydf$names[mydf$names == input$col] <- input$text
names(mydf$df) <- mydf$names
updateSelectInput(inputId = "col", choices = mydf$names)
})
output$process<-renderDataTable({
mydf$df
})
}
shinyApp(ui, server)

How can we add groupby taking inputs from the user from the table we have uploaded?

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
}
})

How to display the variables from the file and change the datatype

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.

Render variables from file input

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)

Error while uploading a file using file input

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)

Resources