Shiny ui not recognized - r

I'm using the normal template for shiny:
library(shiny)
ui <- fluidPage(
#a lot of inputs, graphs, also uiOutput()
)
server <- function(input, output){
#sourcing and running function to generate output, making graphs
}
shinyApp(ui = ui, server = server)
When using the "Run App"-Button, deploying to shinyapps.io or selecting all code and running it, ui is not read in properly. It does not appear in the environment and the error message Error in force(ui) : object 'ui' not found is printed.
When running the code line-by-line however it works fine.
I found this post, where someone had the same error message: https://community.rstudio.com/t/error-in-force-ui-object-ui-not-found-when-deploying-app-to-server/34027, but their solution (removing shinyApp(ui = ui, server = server)) did not work for me.

After I disabled all the complex code inside server and ui, to make it match the example the error still occurred. The issue was in the code above the actual app.
I had used this line before defining ui and server:
X<-function(){} #making an empty function so the one from another script is not underlined
When commenting this out the code worked.

Related

Run App gives no result R shiny app and crashes RStudio

I'm having an issue with the running of a R shiny app. Here's what I do:
open RStudio
load the shiny code (e.g. app.R)
set the wd
library(shiny)
press on "Run App"
Then nothing happens.
And if I try to terminate the execution, R does not answer anymore and says to force the closing of RStudio.
Here's one of my codes (I tried some, so I don't think this is the issue, but I report one anyway):
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
titlePanel("Un'applicazione con uno slider"),
sidebarLayout(
h1("Sposta lo Slider!"),
sliderInput("slider1", "Spostami!", 0, 100, 0)
),
mainPanel(
h3("Valore dello slider:"),
textOutput("text")
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$text <- renderText(input$slider1)
}
# Run the application
shinyApp(ui = ui, server = server)
Do you have any advice to give me in order to make anything happen? I have no errors shown, so I don't know what to do...
I just see "runApp(...mypath.../app)" and blank space after it.
Thank you in advance.
Edit 1:
I also tried this, but nothing happened (as before):
library(shiny)
runExample("01_hello")
Edit 2: Copy-pasting the code directly in the console doen not work either
I was experiencing the same issue with R 4.1 on Windows 10. Updating all packages seems to have solved it:
update.packages(ask = FALSE, checkBuilt = TRUE)

Getting a FALSE message at the bottom of my Rshiny output

I'm very new to Rshiny. I've written this code:
ui=source('./www/uixx2.R')
server = function(input,output){}
shinyApp(ui = ui, server = server)
The uixx2.R file is placed in the subdirectory www and the code is:
ui=shinyUI("Why am i getting: ")
The output is:
Why am i getting: FALSE
I keep getting FALSE at the bottom of my output everytime I use the source function.
You are assigning to the ui variable twice. The line ui = source('./www/uixx2.R') is creating the error, since the ui becomes a list of the sourced code and FALSE, which then shiny displays. Check it in the variable viewer.
Generally the source("file.R") should not be used to assign to a variable since it has no defined return value. (or at least in the usual ? help it doesn't say anything)
This one works.
source('./www/uixx2.R')
server = function(input,output){}
shinyApp(ui = ui, server = server)
The uixx2.R file is placed in the subdirectory www and the code is:
ui=shinyUI("Why am i getting: ")
I would highly recommend the following: In RStudio, go to file -> New file -> Shiny Web App
That way, it will automatically set up a structure for you and make it hard to go wrong
But to answer the question, your ui can be a function, like so
ui <- source('./www/uixx2.R')[[1]] # This takes the first list item (i.e. the function - see below)
server = function(input,output){}
shinyApp(ui = ui, server = server)
# In uixx2.R, simply put:
function() {
shinyUI("Why am i getting: ")
# Anything else goes here ...
}

Shiny app that fetches data via URL works locally but not on shinyapps.io

I wrote a simple shiny app to illustrate a problem I am having fetching data from a website. Here is the UI code:
library(shiny)
shinyUI(fluidPage(
# Application title
titlePanel("Test App"),
# Sidebar with slider inputs
sidebarLayout(
sidebarPanel(
actionButton("button", "Fetch Data")
),
mainPanel(
tabsetPanel(
tabPanel("Data", textOutput("crimelist"))
)
)
)
)
)
And here is the Server code:
library(shiny)
# Define server logic
shinyServer(function(input, output, session) {
# Get a list of the different types of crime
observeEvent(input$button, {
url <- "https://phl.carto.com/api/v2/sql?format=csv&q=SELECT
distinct rtrim(text_general_code) as text_general_code
FROM incidents_part1_part2
order by text_general_code"
crimelist <- read.csv(url(url), stringsAsFactors = FALSE)$text_general_code
output$crimelist <- renderText({crimelist})
})
})
The data being fetched is described at https://cityofphiladelphia.github.io/carto-api-explorer/#incidents_part1_part2.
When I run this shiny app in my local environment, it works perfectly. When publish it to shinyapps.io and run it, the application fails when I click the button to fetch the data. In the shiny log, it reports the following:
2017-10-11T14:46:31.242058+00:00 shinyapps[224106]: Warning in
open.connection(file, "rt") :
2017-10-11T14:46:31.242061+00:00 shinyapps[224106]: URL 'https://phl.carto.com/api/v2/sql?format=csv&q=SELECT distinct
rtrim(text_general_code) as text_general_code
2017-10-11T14:46:31.242062+00:00 shinyapps[224106]: FROM incidents_part1_part2
2017-10-11T14:46:31.242063+00:00 shinyapps[224106]: order by text_general_code': status was 'URL using bad/illegal format or missing URL'
2017-10-11T14:46:31.243062+00:00 shinyapps[224106]: Warning: Error in open.connection: cannot open connection
I am really stumped - is this a shiny server issue of some kind? If anyone out there has a clue, I am all ears!
I posted this question on multiple boards, and Joshua Spiewak on the Shinyapps.io Users Google group solved my problem:
You need to encode the URL in order for it to be valid, e.g. https://phl.carto.com/api/v2/sql?format=csv&q=SELECT%20distinct%20rtrim(text_general_code)%20as%20text_general_code%20FROM%20incidents_part1_part2%20order%20by%20text_general_code
shinyapps.io runs on Linux, so it is likely that curl is being used to fetch this URL. My guess is that you are using Windows locally, which uses a less strict mechanism that is tolerant of the spaces and newlines you have in the URL.
After making his suggested change, it works as expected now. Thanks, Joshua!

Shiny app not updating the data

I have a shiny page looking like that:
library(all my required packages)
source("that file that contains a function to grab my data")
data <- function_from_sourced_file()
server <- function(input, output)
ui <- shinyUI()
shinyApp(ui = ui, server = server)
Now this works fine but I need the shiny app to reflect the changes on said data and to do so I have to either rerun the runApp() function or :w the app.R file in vim. Is it possible to ask the shiny server to rerun the entire file each it is accessed?
thanks
One of the quick ways to do so is using reactivePoll.
reactivePoll
Value
A reactive expression that returns the result of valueFunc, and invalidates when checkFunc changes.
Description
Used to create a reactive data source, which works by periodically polling a non-reactive data source.
Example
reactivePoll and reactiveFileReader

R Shiny app embedded in PowerPoint

Let's consider a simple r shiny application
# Global variables can go here
n <- 200
# Define the UI
ui <- bootstrapPage(
numericInput('n', 'Number of obs', n),
plotOutput('plot')
)
# Define the server code
server <- function(input, output) {
output$plot <- renderPlot({
hist(runif(input$n))
})
}
# Return a Shiny app object
shinyApp(ui = ui, server = server)
I would like to implement this application into my (Microsoft) PowerPoint presentation without losing its interactivity. I tried some things, but with little success:
ReporteRs package is promising, but I couldn’t find the way to directly include shiny application.
Another way is to install a gadget for live web pages into PowerPoint. In this case I can host my application on a web server and connect to that server with PowerPoint. But I don’t want that (there are sometimes problems with internet …).
Thirdly, I could create a presentation in R Markdown and later implement it into PowerPoint, but also didn’t find the way to do it.
So, are there any suggestions?

Resources