Display a very large grid table in R-Shiny - r

I want to display a large grid table in Shiny but I can't find a way to do it, as Shiny seems to always truncate my table. The reason why I use a grid table is the fact that it provides some features I need to implement into my table. I managed to display the right-left view but top-bottom view is always truncated. Here is my code :
ui:
library(shiny)
library(gridExtra)
library(grid)
library(gtable)
shinyUI(fluidPage(
mainPanel(
div(class="double-scroll",style='overflow-x:scroll;overflow-y:scroll;
height:1600px; width:1600px;',plotOutput("out"))
)
))
server:
shinyServer(function(input, output,session) {
mat <- matrix(8,nrow=50,ncol=50)
example <- tableGrob(mat,rows=NULL,cols=NULL)
output$out <- renderPlot({grid.draw(example)})
})
In this example, the 50 columns of "8" are shown but only 20 rows are displayed.

Try a code like that:
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
fileInput("file1", "Choose CSV File",
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv")
),
tags$hr(),
checkboxInput("header", "Header", TRUE)
),
mainPanel(
tableOutput("contents")
)
)
)
server <- function(input, output) {
output$contents <- renderTable({
inFile <- input$file1
if (is.null(inFile))
return(NULL)
df <- read.csv(inFile$datapath, header = input$header)
print(df[,c(1:16)]) # display only the first 16th columns of your dataset
})
}
shinyApp(ui, server)

Related

Using fluidpage and column layout for RShiny dashboard

I will like to use the code below to output a dashboard with 2 panels on the left (Top panel: input file; Bottom panel: action plot) and plot (main page) on the right. Currently, the code outputs the two panels at the top part of dashboard and plot at the bottom, and this is not what I want. I'm new to Rshiny and need help.
Code:
library(shiny)
library(pheatmap)
# Define UI for dataset viewer app ----
ui <- fluidPage(
# App title ----
titlePanel("plot"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel("Sidebar panel",
# Input: Selector for choosing dataset ----
fileInput("file1", "Choose CSV File",
accept = c(
"text/csv",
"text/comma-separated-values,text/plain",
".csv")
),
tags$hr(),
checkboxInput("header", "Header", TRUE)
),
# tags$hr(),
sidebarPanel('get heatmap',
actionButton('getHmap', 'get heatmap'))
),
# Main panel for displaying outputs ----
mainPanel("Plot",
#column(6,
plotOutput("themap"),
tableOutput("table.output"))
#)
)
server = function(input, output, session) {
a <- reactive({
inFile <- input$file1
if (is.null(inFile))
return(NULL)
tbl <- read.csv(inFile$datapath, header=input$header) #, sep=input$sep, dec = input$dec)
return(tbl)
})
output$table.output <- renderTable({
a()
})
plotdata <- eventReactive(input$getHmap, {
a <- as.matrix(a()[-1])
row.names(a) <- a()$ID
a[is.na(a)] <- 0
a
})
output$themap = renderPlot({
pheatmap(plotdata())
})
}
shinyApp(ui, server)
Does anyone know how to use Rshiny fluidpage and columns well and can help?
I'm not entirely sure what you want, but you do have two sidebarPanels in your UI, which isn't helping. How about:
ui <- fluidPage(
titlePanel("plot"),
sidebarLayout(
sidebarPanel("Sidebar panel",
# Input: Selector for choosing dataset ----
fileInput("file1", "Choose CSV File",
accept = c(
"text/csv",
"text/comma-separated-values,text/plain",
".csv")
),
tags$hr(),
checkboxInput("header", "Header", TRUE),
actionButton('getHmap', 'get heatmap')
),
mainPanel("Plot",
#column(6,
plotOutput("themap"),
tableOutput("table.output")
)
)
)
as your UI?
** Edit **
If this isn't what you want, as per your comment below, then please provide more information. Here's a screenshot of what this layout gives me:
which has your plot to the right at the top of the main panel and two controls in the panel to the left: the input file at the top and an action button below. That seems to match your spec in the question. (Although I wasn't sure what you meant by "action plot", so I assumed you meant "action button" as that's what your sample code had.) Note that the plot doesn't actually appear because I haven't installed the pheatmap package and you haven't provided any test data.

Shiny dashboard with two sidepanels and a plot

I'm trying to get a Rshiny code to display two panels on the left hand side (one for file input and the other to action plot) and a plot on the right hand side, but I got the following error:
Error in match.arg(position) : 'arg' must be NULL or a character vector
Here is the code:
library(shiny)
library(pheatmap)
# Define UI for dataset viewer app ----
ui <- fluidPage(
# App title ----
titlePanel("plot"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel("Sidebar panel",
# Input: Selector for choosing dataset ----
fileInput("file1", "Choose CSV File",
accept = c(
"text/csv",
"text/comma-separated-values,text/plain",
".csv")
),
tags$hr(),
checkboxInput("header", "Header", TRUE)
),
# tags$hr(),
sidebarPanel('get heatmap',
actionButton('getHmap', 'get heatmap')
),
# Main panel for displaying outputs ----
mainPanel("Plot",
#column(6,
plotOutput("themap"),
tableOutput("table.output"))
#)
)
)
server = function(input, output, session) {
a <- reactive({
inFile <- input$file1
if (is.null(inFile))
return(NULL)
tbl <- read.csv(inFile$datapath, header=input$header) #, sep=input$sep, dec = input$dec)
return(tbl)
})
output$table.output <- renderTable({
a()
})
plotdata <- eventReactive(input$getHmap, {
a <- as.matrix(a()[-1])
row.names(a) <- a()$ID
a[is.na(a)] <- 0
a
})
output$themap = renderPlot({
pheatmap(plotdata())
})
}
shinyApp(ui, server)
I'm new to Rshiny. Can someone help me find the cause of this problem please?

How to show example data and then update user's data in shiny?

Here is an example. What I wanted is to display the example data if user click Show example or will display the data uploaded by user.
df<-data.frame(x=rnorm(9),
y=rnorm(9),
z=rnorm(9))
write.table(df, "test.txt", quote=F)
library(shiny)
ui <- shinyUI(pageWithSidebar(
headerPanel("test"),
sidebarPanel(
actionButton("evReactiveButton", "Show example"),
fileInput("file1", "Upload File",
multiple = TRUE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv"))
),
mainPanel(
tableOutput("matrix")
)
))
server <- function(input, output, session) {
#if action button show example
datobj<-reactive({
dat<-matrix(1:100, nrow=10)
return(dat)
})
#if user upload
datobj <- reactive({
req(input$file1)
dat <- read.table(input$file1$datapath)
return(dat)
})
### matrix file
output$matrix <- renderTable({
return(datobj())
})
}
shinyApp(ui=ui,server=server)
Here's one way to do it using observeEvent. I added an actionButton to switch more easily between the example data (not reactive here) and the data the user imports. If you want to remove this button, don't forget to replace also the second observeEvent environment by observe.
df<-data.frame(x=rnorm(9),
y=rnorm(9),
z=rnorm(9))
write.table(df, "test.txt", quote=F)
library(shiny)
ui <- shinyUI(pageWithSidebar(
headerPanel("test"),
sidebarPanel(
actionButton("example", "Show example"),
fileInput("file1", "Upload File",
multiple = TRUE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv")),
actionButton("import", "Show data imported")
),
mainPanel(
tableOutput("matrix")
)
))
server <- function(input, output, session) {
#if action button show example
datobj<- mtcars
#if user upload
datobj2 <- reactive({
req(input$file1)
dat <- read.table(input$file1$datapath, sep = " ")
return(dat)
})
### matrix file
observeEvent(input$example, {
output$matrix <- renderTable({
datobj
})
})
observeEvent(input$import, {
if (!is.null(datobj2())){
output$matrix <- renderTable({
datobj2()
})
}
else {}
})
}
shinyApp(ui=ui,server=server)

How to use a data.frame in server.R to update a select menu in ui.R?

Here is my plan for the website:
I use file upload to read a csv file from my local computer using the following code:
datatable <- reactive({
inFile <- input$file1
if (is.null(inFile))
return(NULL)
read.csv(inFile$datapath, header=input$header, sep=input$sep, quote=input$quote)
})
in my ui.R, I will use a select menu to show all the column names of datatable. I will pick more names, like 2, from here.
According to what column names I pick, I will generate some plots.
So, here are my questions:
How can I get those column names in ui.R from the data.frame generated in server.R?
How can I transfer the picked names in ui.R back to server.R so that I can generate the plots?
I read some examples like http://shiny.rstudio.com/gallery/update-input-demo.html or something about renderUI, but I just do not know how to use them or it they are the right way to do this.
Here is my all code. In server.R:
library(shiny)
options(shiny.maxRequestSize=300*1024^2)
shinyServer(function(input, output) {
datatable <- reactive({
inFile <- input$file1
if (is.null(inFile))
return(NULL)
read.csv(inFile$datapath, header=input$header, sep=input$sep, quote=input$quote)
})
output$contents <- renderDataTable({
datatable()
}, options = list(orderClasses = TRUE))
output$summary <- renderPrint({
summary(datatable(),20)
aaa <- "afefagfaegar"
list(summary(datatable(),20),aaa)
})
})
Here is the code in ui.R
library(shiny)
shinyUI(navbarPage("",
tabPanel("Data Summary",
fluidPage(
titlePanel("Uploading Files"),
sidebarLayout(
sidebarPanel(
width=2,
fileInput('file1', 'Choose CSV File',
accept=c('text/csv','text/comma-separated-values,text/plain','.csv')),
tags$hr(),
checkboxInput('header', 'Header', TRUE),
radioButtons('sep', 'Separator',c(Comma=',',Semicolon=';',Tab='\t'),','),
radioButtons('quote', 'Quote',c(None='','Double Quote'='"','Single Quote'="'"),'"')
),
mainPanel(
tabsetPanel(type = "tabs",
tabPanel("Table", dataTableOutput('contents')),
tabPanel("Summary", verbatimTextOutput("summary"))
)
)
)
)
),
tabPanel("Single factor",
fluidPage(
sidebarLayout(
sidebarPanel(
***#I think I should put something here for the select menu***
),
mainPanel()
)
)
),
tabPanel("Multiple factors")
)
)
I think I figure out how to do this.
In server.R, I put:
output$singlefactor <- renderUI({
selectInput("sfactor", "Feature selection:", names(datatable()))
})
In ui.R, I do like:
uiOutput("singlefactor")
So far, everything is fine.

In Shiny, how to see which action happen later?

In my app, the user can upload their file as the input data (using file upload widget). If they don't have their data, I provide some demo data (user can choose demo data by clicking actionButton).
How do I make a variable = the upload OR the demo, whichever is later? Any help is appreciated.
server.R
library(shiny)
DemoData = data.frame('Col.1'=c('Demo','Demo'),
'Col.2'=c('Data','Data'))
shinyServer(function(input, output) {
# option 1: use demo data
getDemo = eventReactive(input$Demo,{
DemoData
})
# option 2: user upload data
getUpload = reactive({
inFile = input$file
if (is.null(inFile)) return(NULL)
read.csv(inFile$datapath)
})
# need getData() to be option 1 or 2, whichever happened later
# should respond to multiple times of changing between option 1 and 2
getData = # ??? getDemo() or getUpload(), whichever is later
# show the data
output$InputData = renderDataTable({
as.data.frame( getData() )
})
})
ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel(h2("Hello Shiny")),
sidebarLayout(
sidebarPanel(
fileInput('file',
'Choose CSV File (two columns: Town and State)',
accept=c('text/csv',
'text/comma-separated-values,text/plain',
'.csv')
),
actionButton('Demo', 'Use Demo Data')
),
mainPanel(
tabsetPanel(
tabPanel(title=h4('Data'),
column(5, tags$h3('Input Data'), dataTableOutput('InputData'))
)
)
)
)
))
UserData.R (maybe make it easier for you to test)
getwd()
setwe()
UserData = data.frame('Col.1'=c('User','User'),
'Col.2'=c('Data','Data'))
write.csv(UserData, file="UserData.csv", row.names=FALSE)
You can use observers, one to watch the demo button and one to watch for file uploads. Both update the same reactive data, so you see the effect of whichever happened last.
library(shiny)
DemoData <- data.frame('Col.1'=1:10,
'Col.2'=rnorm(10))
shinyApp(
shinyUI(fluidPage(
titlePanel(h2("Hello Shiny")),
sidebarLayout(
sidebarPanel(
fileInput('file',
'Choose CSV File (two columns: Town and State)',
accept=c('text/csv',
'text/comma-separated-values,text/plain',
'.csv')
),
actionButton('Demo', 'Use Demo Data')
),
mainPanel(
tabsetPanel(
tabPanel(title=h4('Data'),
column(5, tags$h3('Input Data'), tableOutput('InputData'))
)
)
)
)
)),
shinyServer(function(input, output) {
values <- reactiveValues() # store values to be changed by observers
values$data <- data.frame()
## Observer for uploaded file
observe({
inFile = input$file
if (is.null(inFile)) return(NULL)
values$data <- read.csv(inFile$datapath)
})
## Observer for demo data button
observe({
if (input$Demo > 0) # otherwise demo data shows on startup
values$data <- DemoData
})
## show the data
output$InputData = renderTable({
values$data
})
})
)

Resources