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.
Related
My shiny apps used to work well both locally and when deployed, but recently all fileInput()s stopped working on the shiny-server.
The following MWE (hopefully) accessible at http://186.155.29.169:3838/testApp/ works well on RStudio and in shinyapps.io, but not when deployed at my Linux Shiny-server.
library(shiny)
ui <- fluidPage(
tags$h2('Uploading of a CSV file:'), fileInput("file", "Upload file", accept = '.csv'),
tags$h3('File metadata:'), tableOutput("fileMetDat"),
tags$hr(), tags$h3('File data:'), tableOutput("fileData")
)
server <- function(input, output, session) {
output$fileMetDat <- renderTable({
if(is.null(input$file)){return ()}
input$file
})
output$fileData <- renderTable({
if(is.null(input$file)){return ()}
read.csv(input$file$datapath, sep = ';')
})
}
shinyApp(ui, server)
I checked the log files, and the fileInput() seems not to be reacting at all.
I think I might have messed up the Server permissions to access tmp folder... I'm not sure. I wonder if uninstalling and re-installing ShinyServer would get it solved. The error I'm getting shows itself in the widget's progress bar, but I do not understand it:
I already from tried several browsers on two different OS and the problem persist.
In the server tmp directory shiny creates directories (for each app) and a tracker-extract-files.996 file, but it seems that no files get into the new tempdirs.
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)).
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)).
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:
I'd like to unzip a compressed .mdb file in the www folder of my shiny app, query it for data, and then remove it. Unzip() works on my local machine, but when I deploy the app at shinyapps.io, it has issues unzipping the file. Because I'm not able to read.table() the resulting file (it's an .mdb) I don't think unz() will work.
This code works when run on my local machine
Server:
require(shiny)
shinyServer(function(input, output) {
observeEvent(input$run,{ #Run Button
dbName=unzip('www/test.zip', list=T)
output$name=renderText({
paste(dbName[1])
})
db=unzip('www/ttt.zip', exdir='www', unzip=getOption("unzip"))
test1=read.csv(db) #.csv for simplicity, but my problem uses a .mdb
file.remove(db)
output$testcount=renderText({
paste(sum(test1))
})
})#/Run Button
})#/SS
ui:
shinyUI(
sidebarLayout(
sidebarPanel(width=3,
h5('ZIP test'),
p(align="left",
shiny::actionButton("run", label = "Run!")
),
textOutput(outputId = "name"),
textOutput(outputId = "testcount")
),
mainPanel(width=9,
plotOutput(outputId = "probs",height = "550px")
)
)
)
But fails when uploaded to Shinyapps.io.
Any idea of what I'm doing wrong here? I've tried passing the file path directly, and messing with the unzip= options, but to no avail. If I remove the second call, It will tell me the name just fine, but if I try to unzip the file, it breaks.
Any help is appreciated!
EDIT
I was able to get it to work by removing exdir='www', unzip=getOption("unzip") and just looking for the file in the root directory: test1=read.csv('file1.csv')
I'm using unzip() on shiny-server and everytime the function is called the content of the .zip is saved in the root directory of the app. I asume thats a problem for shinyapps.io.
In the documentation you can only specify the location where the file is with 'exdir' from what I read.