Image does not show in shiny app - r

I have a shiny app which works just fine. Now, I want to display a static image as a logo on top of the page. Problem is the logo does not show up when I run the app, it just shows a missing file icon..
Top of my ui function looks like this:
ui = fluidPage(
img(heigth = 100, width = 100, src = "logo.png", align = "right"),
pageWithSidebar(
headerPanel(title = "ABC", windowTitle = "ABC"),
sidebarPanel(...
I run the app as follows:
# --------------------------------------------------------------------------------------------------------------------------------------
# Settings:
dir = "C:/app/";
folder_code = "r/";
# --------------------------------------------------------------------------------------------------------------------------------------
# Main:
# Libraries:
library(shiny);
library(plotly);
# Includes:
source(file=paste(dir, folder_code, "analysis.r", sep=""));
source(file=paste(dir, folder_code, "plotlib.r", sep=""));
source(file=paste(dir, folder_code, "ui.r", sep=""));
source(file=paste(dir, folder_code, "server.r", sep=""));
# Run app:
shinyApp(ui = ui, server = server, options=list(launch.browser = TRUE))
The directory where the app code and image is placed looks like this:
|r
|www [<- my image is stored in this folder]
analysis.r
app.r
ui.r
server.r
plotlib.r
EDIT:
With the following changes, it now works:
I create a file app.R with just this line of code:
app = shinyApp(ui = ui, server = server)
In my main code displayed above, I replace
shinyApp(...)
by
shinyAppFile(appFile = paste(dir, folder_code, "app.r", sep=""),
options=list(launch.browser = TRUE))
No clue however why it works like that and not as I had it previously..

You might want to check the permissions on your www directory and image- the directory should be drwxrwxr-x and image -rw-rw-r--.

Related

Shiny app runs locally, error when trying to deploy it

I am getting an error
The application failed to start: exited unexpectedly with code 1
Error in enforcePackage(name, curVersion) :
The shiny package was not found in the library.
Calls: local ... eval -> eval -> eval -> -> enforcePackage
Execution halted
when I try to deploy shiny app. I found similar problem that claimed that having library(shiny) solves it, it didn't help. I also tried lib=("path/to/shiny"), no effect.
Here is my code
library(shiny)
library(DT)
library(rmarkdown)
###
ui <- fluidPage(fluidRow(column(10, div(dataTableOutput("dataTable")))))
server <- function(input, output, session) {
data <- reactiveFileReader(100, session, 'excel.csv', read.csv)
output$dataTable <- renderDT(
data(),
class = "display nowrap compact",
filter = "top",
options = list(
scrollX = TRUE,
searchCols = default_search_columns,
search = list(regex = FALSE, caseInsensitive = FALSE, search = default_search)
)
)
}
shinyApp(ui, server)
rsconnect::deployApp('C:/path//app1.Rmd')
Any help is appreciated.
The error indicates that Shiny is not installed in the environment you are deploying to.
If deploying to a server you own, first install Shiny Server, and confirm that the examples work as expected.
Then consult with the Administrator's Guide to set it up as you like it.

fileInput not working properly with Docker Windows system

I am quite new to Docker and need to host an R Shiny App on Docker. Any help would be appreciated. Please let me know if I need to change something in the DOckerFile.
R ShinyApp works perfectly fine on a local computer but it crashes while using Docker to host it.I suspect something wrong with the fileInput$datapath and Windows/Docker interaction. Do I need to specify the PATH in the DockerFile?
I have used rocker/verse image from Docker Hub,installed the libraries manually and stored the image locally on my computer as 'r_all_libraries_july2'
This is the image I have used in my DockerFile.
The Shiny Code works well in a Linux environment. But, crashes while running docker in Windows Environment. A temporary file is also getting created in the production environment when a file is input in the Shiny App using fileInput.
library(shiny)
library(DT)
library(dplyr)
library(shinycssloaders)
library(readxl)
library(shinyjs)
library(ggplot2)
library(png)
library(spatstat)
require(tibble)
require(magrittr)
require(dplyr)
require(multcomp)
require(emmeans)
require(readxl)
library(httr)
require(ggfortify)
library(shinyjs)
library(shinyBS)
ui <-navbarPage(title="RShinyApp", windowTitle = "Data Visualization", theme = shinythemes::shinytheme("cerulean"),selected = "Load Data",
tabPanel(title="Load Data", #3rd Tab Panel Start,
fluidPage(useShinyjs(),
sidebarLayout(
sidebarPanel(
wellPanel(checkboxGroupInput("filetype", "Choose filetype to upload:",
choices = c("CSV"="csv", "Excel"="excel"))),
conditionalPanel(condition = "(input.filetype=='csv')|(input.filetype=='excel')",
wellPanel(checkboxInput(inputId = 'header', label = 'Header', value = FALSE)),
fileInput(inputId = "file", label = "Upload File", accept = c(".csv",".xlsx"))
),#End of conditional panel
uiOutput("sheetnames")
#conditionalPanel(condition = "(input.filetype=='excel')&(!is.null(input.file))",uiOutput("sheetnames")),
),#sidebarpanel
mainPanel(
# h3("Data Table"),
withSpinner(tableOutput("contents"))
)
)#SideBarLayout
)#FluidPage End
) #3rd Tab Panel End
)#navbarpage
server <-function(input,output,session){
###########Load Data Tab#######################
rv<-reactiveValues(data=NULL,xlorcsv=NULL,head=FALSE,sheet=NULL,features=NULL)
observeEvent(input$filetype,{if(input$filetype=='csv'){rv$xlorcsv<-'csv'}
else if(input$filetype=='excel'){rv$xlorcsv<-'excel'}})
observeEvent(input$header, rv$head<-input$header)
observeEvent(input$sheetnames,rv$sheet<-input$sheetnames)
observeEvent(input$file,
{if((!is.null(rv$xlorcsv))&(!is.null(input$file))){
#####THIS IS WHERE THE SHINY APP IS CRASHING IN DOCKER--my guess is datapath ###########needs to be defined here
if(rv$xlorcsv=='csv'){rv$data<-read.csv(input$file$datapath, header = rv$head, na.strings = "")
rv$features<-colnames(rv$data)}
}
})
output$sheetnames<-renderUI({
if((is.null(rv$xlorcsv))|(is.null(input$file))){return(NULL)}
if((rv$xlorcsv=='excel')&(!is.null(input$file))){selectInput("sheetnames","Select sheet to load",choices = excel_sheets(path = input$file$datapath))}
})
output$contents<-renderTable({rv$data})
}
shinyApp(server=server, ui=ui)
DockerFile:
FROM r_all_libraries:latest
EXPOSE 80
COPY r_shiny_code_working11.R /home/rstudio/r_shiny_code_working11.R
CMD ["/home/rstudio/r_shiny_code_working11.R"]
This is the error on the console:
standard_init_linux.go:207: exec user process caused "no such file or directory"

download pdf with knitr in shiny in R

I try to create button for downloading with knitr package.
I have found some example which works a bit but I'm not able to change the name of the pdf. moreover it causes errors then.
enter image description here
second problem is that when I download the file , it is not a .pdf extension and I have to choose program to open it when I try to open. How can I download file as .pdf in default?
here are my codes (example from the internet):
server.R
library(knitr)
shinyServer(function(input, output) {
output$myreport = downloadHandler(
filename = "adads.pdf",
content = function(file) {
out = knit2pdf('input.Rnw', clean = TRUE )
file.rename(out, file) # move pdf to file for downloading
},
contentType = 'application/pdf'
)
})
ui.R
library(shiny)
shinyUI(basicPage(
textInput('firstname', 'First name', value = 'Jimmy'),
textInput('lastname', 'Last name', value = 'John'),
downloadButton('myreport')
))
and input.Rnw file
\documentclass{article}
\begin{document}
<<names>>=
input$firstname
input$lastname
#
\end{document}

How to remove tags in textOutput - RShiny

I am new to R and I am actually developing a page where a directory (string characters like "xx/xx") is given in the server and I want to take back this directory to include it in the source of my ui app.
UI:
library(shiny)
file<-textOutput("paramfile")
source(file(file), local = TRUE, encoding = 'UTF-8')
SERVER :
filedir<-renderText({
"entries/5429_param.R"
})
output$paramfile<-renderText({
filedir()
})
I then have an error :
"Warning in file(filename, "r", encoding = encoding) : cannot open
file '< div id="paramfile" class="shiny-text-output">< /div>':
Invalid argument
Error in file(filename, "r", encoding = encoding) :
cannot open the connection"
Do you know how can I remove those tags or if there is another function that can allow me to take a string in the server and to include it into a source.
Here is a basic example of a ShinyApp:
# Example of UI with fluidPage
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
textInput("dir", label = "Enter a directory")
),
mainPanel(
verbatimTextOutput("dirPrint"),
verbatimTextOutput("lsFiles")
)
)
)
# Server logic
server <- function(input, output) {
output$dirPrint <- renderPrint({
print(input$dir)
})
output$lsFiles <- renderPrint({
fls <- list.files(input$dir)
print(fls)
})
}
# Complete app with UI and server components
shinyApp(ui, server)
If you enter the path of a directory in the textinput, the second renderPrint function is showing all files, that are found at that path.
I would suggest you go over the Shiny-Tutorials, as there seem to be some syntax-problems in your code and I am not sure what exactly you want to achieve.

shiny sourced function stdout to logfile to UI issue

I made a portable shiny app with portable chrome and R. I launch rscript with vbs script in which stdout is redirected to a logfile with:
RScriptFile & " 1> " & logfile & " 2>&1"
What I want to do is to read in the log file so that the user has feedback about the calculation progress. The following reactivePoll works if the "main" shiny script is writing to the console, but it does not if a sourced function writes. Note that both are displayed in the log file, therefore the fault is surely not in the redirection, but somewhere in my logic or in shiny.
If I set bool_for_testing = T the test button works, on the other hand if I set it False not even the first "Go!" is printed on the console until the script is finished, but both are displayed real time in the logfile:
library(shiny)
bool_for_testing = F # note its use below!
logfile_name = "some_log_file_that_exists" # this is created by the vbs (command line) script!
ui = shinyUI(fluidPage(
actionButton("btn_test", "push meh!"),
fluidRow(
column(12, wellPanel(style = "overflow-y:scroll; max-height: 600px",
tags$b(paste("log file is this one:", logfile_name)),
verbatimTextOutput("logText")
))
)
)
) # end UI
server = shinyServer(function(input, output, session) {
observe({
if(input$btn_test > 0){
if(bool_for_testing) {
# this works!
print("Go!")
} else {
# not a letter on log here
print("Go!")
source("some_barely_interesting_function.R")
some_barely_interesting_function()
}
}
})
logFile <- reactivePoll(1000, session,
checkFunc = function() {
if (file.exists(logfile_name))
# should always refresh
runif(1) # Sys.time() does not work either
else
""
},
valueFunc = function() {
readLines(logfile_name)
}
)
# # this does not work either
# fileReaderData <- reactiveFileReader(500, session,
# logfile_name, readLines)
output$logText <- renderText({
text <- logFile()
paste(text, collapse = '\n')
})
session$onSessionEnded(function() {
# for portable R and chrome application
stopApp()
q("no")
})
})
shinyApp(ui = ui, server = server)
If I manually open the log file it shows the progress continuously, therefore I think the GUI thread is held up until the sourced calculation is finished (if I open a new shiny window only for reading the log file, it will work well, too). Is this truly the case, and is there a workaround for this?

Resources