Download sidebar inputs in pdf format - r

Is there a practical way to download all your sidebar inputs as a pdf in a shiny app?Im trying to create a dataframe but it is not so clear and the one column name is not displayed. I use kable() but there may be a more practical way to display.
library(shiny)
server <- shinyServer(function(input, output, session) {
df<-reactive({
Report<-input$rep
Select<-input$sel
df<-as.data.frame(Report,Select)
})
output$downloadData = downloadHandler(
filename = function() {"sampleTable.pdf"},
content = function(file) {
x <- kable(
df(),
format = "latex", caption = "Prices(long)",booktabs=TRUE)%>%
kable_styling(latex_options="scale_down")
save_kable(x, file)
},
contentType = 'application/pdf'
)
})
ui_panel <-
tabPanel("Multi-Select Input Test",
sidebarLayout(
sidebarPanel(
radioButtons("rep","Choose report",c("kable","text")),
selectInput("sel","Select",c("choice1","choice2")),
downloadButton('downloadData', 'Download'),
br()
),
mainPanel(
tabsetPanel(tabPanel("Text")
)
)
))
ui <- shinyUI(navbarPage(" ",ui_panel))
runApp(list(ui=ui,server=server))

Related

Dataset downloaded as kable in pdf is not fully displayed due to its width

In the shiny app below I download as pdf two kable() tables one below the other. The issue is that one dataset mtcars has more columns and its wider than the iris dataset and as a result some of its columns are missing.
library(shiny)
library(dplyr)
library(shinydashboard)
library(kableExtra)
### user inter phase
ui <- fluidPage(
### App title ----
titlePanel(title=div(img(src="pics/IRP_NHSc.jpg", width="99%")))
,
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
### Input files ----
),
### Main panel for displaying outputs ----
mainPanel(
tabsetPanel(
tabPanel("Export report", uiOutput("tab7"))
)
)
)
)
#### Server
server <- function(input, output, session) {
output$tab7 <- renderUI({
tagList(
textInput("fn", "Report name", value = "NHSc_Report", width = 200),
downloadLink('downloadData', 'Download')
)
})
#And here the same reactive object is downloaded as a pdf
output$downloadData = downloadHandler(
filename = function() {"sampleTable.pdf"},
content = function(file) {
x <- kable(list(iris,mtcars), format = "latex", caption = "Report",booktabs=TRUE)%>%
kable_styling(latex_options="scale_down")
save_kable(x, file)
},
contentType = 'application/pdf'
)
}
shinyApp(ui = ui, server = server)

Inputting variable data in shiny

I'm looking to make a shiny app where users upload a text file and can input the terms they want filter (so sentences that have two particular words or phrases). then they can download the results. My script so far looks like:
ui <- fluidPage(
titlePanel("Download data"),
sidebarLayout(
sidebarPanel(
fileInput("file1", "Choose .txt file",
multiple = F,
accept = c(".txt")),
textInput('energy_co', 'Name of energy company'),
textInput('asset', 'name of Asset Manager'),
downloadButton("downloadData", "Download")
),
mainPanel(
tableOutput("table")
)
)
)
server <- function(input, output, session) {
options(shiny.maxRequestSize=30*1024^2)
output$table <- renderTable({
req(input$file1)
data <- read_lines(input$file1$datapath)
text_df <- as_data_frame(data)
company_data <- text_df %>%
filter(str_detect(terms, input$asset)) %>%
filter(str_detect(terms, input$energy_co)) %>%
distinct(.)
company_data
})
output$downloadData <- downloadHandler(
filename = function() {
paste(company_data, ".csv", sep = "")
},
content = function(file) {
write.csv(company_data, file1, row.names = FALSE)
}
)
}
shinyApp(ui, server)
I can upload the dataset (a .txt file) but nothing happens either when I try and render the table or when I try and download the results as a csv. I think the server script might need to be reactive? any help appreciated
Try this
ui <- fluidPage(
titlePanel("Download data"),
sidebarLayout(
sidebarPanel(
fileInput("file1", "Choose .txt file",
multiple = F,
accept = c(".txt")),
textInput('energy_co', 'Name of energy company'),
textInput('asset', 'name of Asset Manager'),
downloadButton("downloadData", "Download")
),
mainPanel(
tableOutput("table")
)
)
)
options(shiny.maxRequestSize=30*1024^2)
server <- function(input, output, session) {
company_data <- reactive({
req(input$file1,input$asset,input$energy_co)
data <- read_lines(input$file1$datapath)
text_df <- as_data_frame(data)
company_data <- text_df %>%
filter(str_detect(terms, input$asset)) %>%
filter(str_detect(terms, input$energy_co)) %>%
distinct(.)
company_data
})
output$table <- renderTable({
company_data()
})
output$downloadData <- downloadHandler(
filename = function() {
paste(company_data, ".csv", sep = "")
},
content = function(file) {
write.csv(company_data(), file1, row.names = FALSE)
}
)
}
shinyApp(ui, server)

downloadHandler not working with observeEvent

I am trying to download a file using downloadHandler with observeEvent shiny but I am not able to download the file,
library(shiny)
load(url("http://s3.amazonaws.com/assets.datacamp.com/production/course_4850/datasets/movies.Rdata"))
ui <- fluidPage(
sidebarLayout(
# Input
sidebarPanel(
# Numeric input for number of rows to show
numericInput(inputId = "n_rows",
label = "How many rows do you want to see?",
value = 10),
# Action button to show
actionButton(inputId = "button",
label = "Show")
),
# Output:
mainPanel(
tableOutput(outputId = "datatable")
)
)
)
server <- function(input, output, session) {
# creating a reactive expression
df <- eventReactive(input$button, {
movies %>% head(input$n_rows)
})
# download a csv everytime when user click on show button
observeEvent(input$button, {
output$button <- downloadHandler(
filename = function() {
paste("data-", Sys.Date(), ".csv", sep="")
},
content = function(file) {
write.csv(df(), file)
}
)
cat("done downloading file \n")
})
# displays the data on the web in tabular format, data comes from reactive event
output$datatable <- renderTable({
df()
})
}
# Create a Shiny app object
shinyApp(ui = ui, server = server)
I was able to execute above code without any errors, but csv file is not downloading, i want to display the data table and download the displayed data on same button click event , how can i achieve this, Am I missing something, any help would be appreciated
Try this:
library(shiny)
library(dplyr)
load(url("http://s3.amazonaws.com/assets.datacamp.com/production/course_4850/datasets/movies.Rdata"))
ui <- fluidPage(
sidebarLayout(
# Input
sidebarPanel(
# Numeric input for number of rows to show
numericInput(inputId = "n_rows",
label = "How many rows do you want to see?",
value = 10),
# Action button to show
downloadButton('downloadData', 'Download data')
),
# Output:
mainPanel(
tableOutput(outputId = "datatable")
)
)
)
server <- function(input, output, session) {
# Reactive value for selected dataset ----
df <- reactive({
movies %>% head(input$n_rows)
})
output$datatable <- renderTable({
df()
})
output$downloadData <- downloadHandler(
filename = function() {
paste("dataset-", ".csv", sep = "")
},
content = function(file) {
write.csv(df(), file, row.names = FALSE)
})
}
# Create a Shiny app object
shinyApp(ui = ui, server = server)

Download plots as PNG documents

I am fairly new to Shiny Apps and I wish to download the plots as png/pdf file. After publishing the app online, the downloaded filename is correct but it is an empty file. I applied print function in content for downloadHandler but it doesn seem to work. Can anyone help me out? Thanks
ui.r
library(shiny)
ui <- fluidPage(
titlePanel("My First Shiny Project"),
sidebarLayout(
sidebarPanel(
selectInput("select","Choose a Dataset",
choices = list("trees","pressure"),
selected = "pressure"),
selectInput("format","Choose file format",
choices = list("pdf","png"))
),
mainPanel(
plotOutput("graph")
)
),
downloadButton("download","Download Here")
)
server.r
library(shiny)
server <- function(input,output){
data <- function()({
switch(input$select,
"trees" = trees,
"pressure" = pressure)
})
output$graph <- renderPlot(
plot(data())
)
output$download <- downloadHandler(
filename = function(){
paste("data",input$select,input$format,sep = ".")
},
content = function(file){
if(input$format == "png")
png(file)
if(input$format == "pdf")
pdf(file)
print(plot(data()))
dev.off
}
)
}
It seems that the only issue was that you used dev.off instead of dev.off(), you also do not need the print() statement. A working version of your code is shown below, hope this helps!
library(shiny)
library(ggplot2movies)
library(dplyr)
ui <- fluidPage(
titlePanel("My First Shiny Project"),
sidebarLayout(
sidebarPanel(
selectInput("select","Choose a Dataset",
choices = list("trees","pressure"),
selected = "pressure"),
selectInput("format","Choose file format",
choices = list("pdf","png"))
),
mainPanel(
plotOutput("graph")
)
),
downloadButton("download","Download Here")
)
server <- function(input,output){
data <- function()({
switch(input$select,
"trees" = trees,
"pressure" = pressure)
})
output$graph <- renderPlot(
plot(data())
)
output$download <- downloadHandler(
filename = function(){
paste("data",input$select,input$format,sep = ".")
},
content = function(file){
if(input$format == "png")
png(file)
if(input$format == "pdf")
pdf(file)
plot(data())
dev.off()
}
)
}
shinyApp(ui,server)

Error: Not a graph object in R shiny

Does anyone how to fix this error when I'm trying to find the degree of each vertex from the input file? Here's the Pajek file i want to import in and export the degree into CSV.
When I tried using a smaller input file. The renderTable works but when I tried with my file(which is in the link) it somehow keeps showing that error message and does not display on the tab set.
Here's what I've done so far:
ui.R
shinyUI(fluidPage(
titlePanel("Finding most influential vertex in a network"),
sidebarLayout(
sidebarPanel(
fileInput("graph", label = h4("Pajek file")),
downloadButton('downloadData', 'Download')
),
mainPanel( tabsetPanel(type = "tabs",
tabPanel("Table", tableOutput("view"))
)
)
)
))
server.R
library(igraph)
options(shiny.maxRequestSize=-1)
shinyServer(
function(input, output) {
filedata <- reactive({
inFile = input$graph
if (!is.null(inFile))
read.graph(file=inFile$datapath, format="pajek")
})
Data <- reactive({
df <- filedata()
vorder <-order(degree(df), decreasing=TRUE)
DF <- data.frame(ID=as.numeric(V(df)[vorder]), degree=degree(df)[vorder])
})
output$view <- renderTable({
Data()
})
output$downloadData <- downloadHandler(
filename = function() {
paste("degree", '.csv', sep='')
},
content = function(file) {
write.csv(Data(), file)
}
)
})
Also, I'm also not sure how to write to csv file from the data frame I've output.

Resources