Calling getwd() from within a shiny app - r

I am creating a Shiny app that needs to access a user's working directory. I assumed getwd() would work from within the Shiny app, however this always points to the directory of the Shiny app itself.
Example:
From the R console:
setwd("~/Documents")
getwd()
[1] "/Users/chris.harrison/Documents"
From the following Shiny app saved on the desktop:
library(shiny)
ui <- fluidPage(
verbatimTextOutput("wd")
)
server <- function(input, output){
output$wd <- renderText(getwd())
}
shinyApp(ui = ui, server = server)
And then calling:
setwd("~/Documents")
shiny::runApp("~/Desktop/app.R")
the output is:
/Users/chris.harrison/Desktop
Is there any way to call the user's working directory from within the app?
Many thanks,
Chris

Related

How would you get the file server's path for images from a call to the shinyApp(ui = ui, server = server) function? [duplicate]

I'm trying to put a static image into a Shiny app. I have created a folder called www in the working directory of my Shiny app and put a PNG file there. Using the following code should show the image:
library(shiny)
ui <- fluidPage(
tags$img(src='photo.png')
)
server <- function(input, output) {}
shinyApp(ui=ui, server=server)
But instead I have this:
Querying the image URL (http://127.0.0.1:7122/photo.png) directly shows a 404 status code.
The outcome is the same regardless of whether I start the Shiny app by running the code manually, clicking the "Run App" button in RStudio, or executing the file via Rscript app.R on the command line.
Here is the folder structure:
.
├── app.R
└── www
└── photo.png
Am I missing something?
I don't think this is a bug - there is some documentation missing.
The issue here is, that the default resource publishing via the www folder using the / prefix is implemented only for two-file (server.R and ui.R) and single-file (app.R) shiny apps - not for shiny app objects (the object returned by shinyApp()). This makes sense, as in contrast to e.g. the app.R file a shiny app object doesn't reside in a fixed directory.
The www folder to / prefix mapping only takes place if runApp's appDir parameter is provided with a directory or file path (string object). The character method can be seen here. Another relevant function (downstream) can be found here.
Once a shiny app object is passed to runApp's appDir argument we need to use addResourcePath, which is the case when running runApp(shinyApp(ui, server)).
Accordingly the following works:
library(shiny)
addResourcePath("prefix", "www")
ui <- fluidPage(
tags$img(src='prefix/photo.png')
)
server <- function(input, output) {}
appObj <- shinyApp(ui=ui, server=server)
runApp(appObj)
# print(appObj) # also works
It seems that this is a bug in Shiny.
Until this is fixed, there are two rough workaround strategies:
Convince Shiny that www should be mapped in a shinyApp object. To do this, we need to modify the shinyApp object:
library(shiny)
ui <- fluidPage(
tags$img(src = 'photo.png')
)
server <- function(input, output) {}
app <- shinyApp(ui = ui, server = server)
app$staticPaths <- list(
`/` = httpuv::staticPath(
file.path(getwd(), "www"), indexhtml = FALSE, fallthrough = TRUE
)
)
app
(Solution based on a RStudio Community discussion.)
Launch Shiny without using the shinyApp object directly:
by pressing the “Run App” button in RStudio, or
by executing runApp('.') or runApp('app.R').
Other ways of launching the app do not work! Notably, this also includes replacing the last line in the script with runApp(shinyApp(ui, server)).

Where would be the location of my project when running in R studio connect server

I have a concern here. Below is simple application. When I run this code, I get location of my project in my R console (since I have print(getwd()) at the last line). But when I deploy this in my Rstudio connect server or shinyapps.io. When can I see this location. In fact it will no more be my local system. So I have 2 questions here
What would be my location (getwd()) when I deploy this application to RStudio connect server or Shinyapps,io
Where can I see this print?
Can you please make me understand.
ui.R
library(shiny)
# Define UI for application that draws a histogram
shinyUI(fluidPage(
textInput("name", "Name: "),
textOutput("greeting"),
selectInput("Slider","Slider",choices = unique(iris$Species))
)
)
server.R
library(shiny)
shinyServer(function(input, output) {
output$greeting <- renderText({
paste0("Hello, ", input$name, "!")
})
print(getwd())
})

Error when using file.choose() in Shiny Web App (deployed online)

I am developing this shiny app that needs to push a local file to a FTP. I am having trouble with this.
I am using ftpUpload() to upload, and file.choose() to grab the file path:
ftpUpload(file.choose(new = FALSE), "ftp.com/Abc", userpwd)
This worked fine when I run the app in my local machine. However after I deploy it on the web, it doesn't work. It disconnects the serve.
I am thinking the issues are on file.choose() since the interactive file selection dialog wouldn't show up.
Does anyone know how to get the file.choose() working, or any other solutions?
Again I am trying to push a local file to an FTP server through an online Shiny App.
Update:
I have checked the log and I get this error:
Warning in file(what, "rb") : cannot open file 'xt': No such file or directory
Warning: Error in file: cannot open the connection
I am using a windows. and this error won't appear when I run the app locally from my RStudio
A minimal working solution with fileInput.
# ui.R
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
fileInput("myFile", "Choose your File")
)
),
mainPanel(
)
)
)
# server.R
server <- function(input, output, session) {
observeEvent(input$myFile,{
selectedFile <- input$myFile
if (is.null(selectedFile))
return(NULL)
# Your code
ftpUpload(selectedFile$datapath, "ftp.com/Abc", userpwd)
})
}
Hope this helps.

Static image in Shiny app not found (HTTP status 404)

I'm trying to put a static image into a Shiny app. I have created a folder called www in the working directory of my Shiny app and put a PNG file there. Using the following code should show the image:
library(shiny)
ui <- fluidPage(
tags$img(src='photo.png')
)
server <- function(input, output) {}
shinyApp(ui=ui, server=server)
But instead I have this:
Querying the image URL (http://127.0.0.1:7122/photo.png) directly shows a 404 status code.
The outcome is the same regardless of whether I start the Shiny app by running the code manually, clicking the "Run App" button in RStudio, or executing the file via Rscript app.R on the command line.
Here is the folder structure:
.
├── app.R
└── www
└── photo.png
Am I missing something?
I don't think this is a bug - there is some documentation missing.
The issue here is, that the default resource publishing via the www folder using the / prefix is implemented only for two-file (server.R and ui.R) and single-file (app.R) shiny apps - not for shiny app objects (the object returned by shinyApp()). This makes sense, as in contrast to e.g. the app.R file a shiny app object doesn't reside in a fixed directory.
The www folder to / prefix mapping only takes place if runApp's appDir parameter is provided with a directory or file path (string object). The character method can be seen here. Another relevant function (downstream) can be found here.
Once a shiny app object is passed to runApp's appDir argument we need to use addResourcePath, which is the case when running runApp(shinyApp(ui, server)).
Accordingly the following works:
library(shiny)
addResourcePath("prefix", "www")
ui <- fluidPage(
tags$img(src='prefix/photo.png')
)
server <- function(input, output) {}
appObj <- shinyApp(ui=ui, server=server)
runApp(appObj)
# print(appObj) # also works
It seems that this is a bug in Shiny.
Until this is fixed, there are two rough workaround strategies:
Convince Shiny that www should be mapped in a shinyApp object. To do this, we need to modify the shinyApp object:
library(shiny)
ui <- fluidPage(
tags$img(src = 'photo.png')
)
server <- function(input, output) {}
app <- shinyApp(ui = ui, server = server)
app$staticPaths <- list(
`/` = httpuv::staticPath(
file.path(getwd(), "www"), indexhtml = FALSE, fallthrough = TRUE
)
)
app
(Solution based on a RStudio Community discussion.)
Launch Shiny without using the shinyApp object directly:
by pressing the “Run App” button in RStudio, or
by executing runApp('.') or runApp('app.R').
Other ways of launching the app do not work! Notably, this also includes replacing the last line in the script with runApp(shinyApp(ui, server)).

How to properly host a shiny app on a shiny server

I installed a Shiny Server on a CentOs server, installation went smooth and I'm abble to run the demo app.
I now would like to host my app but I'm not sure about the right way to do it.
First I just have a ui.R file (I don't have ui.R and server.R, just ui.R) which is composed as follow:
packages importation
some R code
ui <- bootstrapPage(
some ui side code
)
server <- function(input, output, session) {
some server side code
}
shinyApp(ui = ui, server = server)
I moved this file and some needed csvs file first to
/opt/shiny-server/samples/novamente/
In this case I'm just getting a error
So I moved my files to
/srv/shiny-server/novamente/
But when I'm trying to access it through web i can only see a list of my files:

Resources