Chart (rCharts) is not shown in the web page (Shiny) - r

I am trying to embed the interactive chart from rCharts package. To embed the chart I have used the example from here (Shiny app).
The example works well but my prototype works without the chart output (no errors have been reported). My script is as follows:
ui.r:
library(shiny)
require(rCharts)
shinyUI(fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)
),
# Show a plot of the generated distribution
mainPanel(
showOutput("myChart", "polycharts")
)
)
))
server.r:
library(shiny)
require(rCharts)
shinyServer(function(input, output) {
observeEvent(input$bins,{
df2 <<- data.frame(x=c(1:input$bins),y=c(1:input$bins))
})
output$myChart <- renderChart({
print(df2)
p1 <- rPlot(df2$x,df2$y, data = df2, color='green', type = 'point')
p1$addParams(dom = 'myChart')
return(p1)
})
})

I have reviewed your code and here are some pointers:
1) rPlot is taking data as x~y along with color argument
2) It is better if you use the eventReactive and assign it to df2() instead of observe and <<- global assignment operator
rm(list = ls())
library(shiny)
require(rCharts)
server <- function(input, output) {
df2 <- eventReactive(input$bins,{data.frame(x=c(1:input$bins),y=c(1:input$bins))})
output$myChart <- renderChart({
p1 <- rPlot(x~y, data = df2(), color='green', type = 'point', color = 'x')
p1$addParams(dom = 'myChart')
return(p1)
})
}
ui <- fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(sidebarPanel(sliderInput("bins","Number of bins:", min = 1,max = 50,value = 30)),
# Show a plot of the generated distribution
mainPanel(showOutput("myChart", "polycharts"))
)
)
shinyApp(ui, server)

Related

R Shiny: source command prints TRUE on app

I am building a shiny application that should have different options for different user categories. Therefore, I have different subfiles where the inputs are definied and the dependent on the role the respective file is loaded via the source command.
It works, however, the source command always prints a TRUE on my shiny app. I can't get rid of it no matter what option of the source command I am trying.
Here a screenshot of the problem
And a minimal example: app.R
library(shiny)
library(ggplot2)
# Define UI for app that draws a histogram ----
ui <- fluidPage(
titlePanel("Hello Shiny!"),
sidebarLayout(
sidebarPanel(
source("sub.R", echo = FALSE, print.eval = FALSE),
),
mainPanel(
plotOutput(outputId = "distPlot")
)
)
)
server <- function(input, output) {
output$distPlot <- renderPlot({
x <- faithful$waiting
ggplot(tibble(x), aes(x=x)) + geom_histogram( binwidth = input$bins)
})
}
shinyApp(ui = ui, server = server)
And the sourced file sub.R:
sliderInput(inputId = "bins",
label = "Number of bins:",
min = 0.1,
max = 5,
value = 1)
Thanks for any help in advance
I found the fix here: add a [1] at the end of the source(.) command:
library(shiny)
library(ggplot2)
# Define UI for app that draws a histogram ----
ui <- fluidPage(
titlePanel("Hello Shiny!"),
sidebarLayout(
sidebarPanel(
source("~/stackoverflow/17150062/sub.R", echo = FALSE, print.eval = FALSE)[1]
),
mainPanel(
plotOutput(outputId = "distPlot")
)
)
)
server <- function(input, output) {
output$distPlot <- renderPlot({
x <- faithful$waiting
ggplot(tibble(x), aes(x=x)) + geom_histogram( binwidth = input$bins)
})
}
shinyApp(ui = ui, server = server)

Shiny and R combine three graphs into 1

I have developed a basic introductory shiny which downloads stock data and runs 3 technical indicators.
This is the code:
library(shiny)
library(quantmod)
library(dygraphs)
library(TTR)
ui <- shinyUI(fluidPage(
titlePanel("Simple Stock Charting App"),
sidebarLayout(
sidebarPanel(
textInput("symb", label = h3("Input a Valid Stock Ticker"), value = "GE")
),
selectInput("var", label = "bals", choices=list("RSI","Price","ADX")),
### uncomment for dygraphs chart
mainPanel(dygraphOutput("plot"),dygraphOutput("plot2"),dygraphOutput("plot3"))
)
))
server <- shinyServer(function(input, output) {
dataInput <- reactive({
prices <- getSymbols(input$symb, auto.assign = FALSE)
})
output$plot <- renderDygraph({renderPlot
dygraph(Ad(dataInput())) %>%dyRangeSelector()
})
output$plot2 <- renderDygraph({renderPlot
dygraph((RSI(Ad(dataInput()), n = 14))) %>%dyRangeSelector()
})
output$plot3 <- renderDygraph({renderPlot
dygraph((ADX(HLC(dataInput()),n = 14))) %>%dyRangeSelector()
})
})
shinyApp(ui,server)
I would like to know if it is possible the user to choose only of the three indicators each time. Currently, all three are shown but I know its possible to have one graph and based on the selection of the RSI, Value and ADX to change the graph.
You can use switch for what you want:
library(shiny)
library(quantmod)
library(dygraphs)
library(TTR)
ui <- shinyUI(fluidPage(
titlePanel("Simple Stock Charting App"),
sidebarLayout(
sidebarPanel(
textInput("symb", label = h3("Input a Valid Stock Ticker"), value = "GE")
),
selectInput("var", label = "bals", choices=list("RSI","Price","ADX"))
),
### uncomment for dygraphs chart
mainPanel(dygraphOutput("plot"))
))
server <- shinyServer(function(input, output) {
dataInput <- reactive({
getSymbols(input$symb, auto.assign = FALSE)
})
output$plot <- renderDygraph({
data <- switch(input$var,"RSI" = RSI(Ad(dataInput()), n = 14),
"Price" = Ad(dataInput()),
"ADX" = ADX(HLC(dataInput()),n = 14))
dygraph(data) %>%dyRangeSelector()
})
})
shinyApp(ui,server)

Embed reactively generated URL in shiny

I would like to show in my shiny app a link that directs to the URL generated based on user's input. I don't want to show the full text of the URL. I know the a(href="",label="") function can be used if I know the URL beforehand, but in this case the URL depends on the user's input. The following doesn't work:
ui <- fluidPage(
titlePanel("Show map of a given state"),
sidebarLayout(
sidebarPanel(
textInput("state", label = "State", value = "CA", placeholder = "California or CA"),
actionButton("showU","Show map")
),
mainPanel(
conditionalPanel(
condition = "input.showU > 0",
htmlOutput("url"),
a(href=htmlOutput("url"),"Show in Google Map",target="_blank")
)
)
)
)
server <- function(input, output){
observeEvent(input$showU,{
output$url <-renderUI({paste("https://www.google.com/maps/place/", input$state, sep="")})
})
}
shinyApp(ui,server)
I hope I can click on the "Show in Google Map" and be directed to the URL generated on the fly. Please help me, thank you.
You need to use renderUI together with uiOutput to update UI reactively:
library(shiny)
ui <- fluidPage(
titlePanel("Show map of a given state"),
sidebarLayout(
sidebarPanel(
textInput("state", label = "State", value = "CA", placeholder = "California or CA"),
actionButton("showU","Show map")
),
mainPanel(
conditionalPanel(
condition = "input.showU > 0",
uiOutput("url")
)
)
)
)
server <- function(input, output){
observeEvent(input$showU,{
output$url <-renderUI(a(href=paste0('https://www.google.com/maps/place/', input$state),"Show in Google Map",target="_blank"))
})
}
shinyApp(ui,server)
If this questions is actually about creating reactive URL links, then HubertL's answer is the way to go.
If you want to keep the map and search funciton all self-contained in the shiny, rather than having to open a new link to Google Maps, you can use my googleway package to achieve the same task
library(shiny)
library(googleway)
ui <- fluidPage(
titlePanel("Show map of a given state"),
sidebarLayout(
sidebarPanel(
),
mainPanel(
google_mapOutput(outputId = "myMap", height = "600px")
)
)
)
server <- function(input, output){
## you need a valid API key from Google Maps
## https://developers.google.com/maps/
map_key <- "your_map_api_key"
output$myMap <- renderGoogle_map({
google_map(key = map_key, search_box = T)
})
}
shinyApp(ui,server)
I used a HTML button for which the url could be generated recursively
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("HTML button"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot"),
HTML(paste0(htmlOutput('url_test')))
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$distPlot <- renderPlot({
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
output$url_test = renderText({
paste('Go to Google')
})
cultivar_url = reactive({
print('https://www.google.com')
})
}
# Run the application
shinyApp(ui = ui, server = server)

R reactive histogram

I am learning the Shiny interface for R and working through the first example.
The histogram changes based on the slider input. How do I have it update continuously, while the slider is moving? Right now, it only updates when the slider stops moving.
ui.R
library(shiny)
# Define UI for application that draws a histogram
shinyUI(fluidPage(
# Application title
titlePanel("Hello Shiny!"),
# Sidebar with a slider input for the number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
))
server.R
library(shiny)
# Define server logic required to draw a histogram
shinyServer(function(input, output) {
# Expression that generates a histogram. The expression is
# wrapped in a call to renderPlot to indicate that:
#
# 1) It is "reactive" and therefore should re-execute automatically
# when inputs change
# 2) Its output type is a plot
output$distPlot <- renderPlot({
x <- faithful[, 2] # Old Faithful Geyser data
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
})
What I have tried:
#server.R
library(shiny)
shinyServer(function(input,output) {
x<-faithful[,2]
bins <- reactive({
seq(min(x),max(x),length.out=input$bins+1)
})
output$distPlot <- renderPlot({
hist(x,breaks=bins(),col='darkgray',border='white')})
})
Is there a way to make it react faster/real-time?
Thanks to Dean's response I investigated the shinyCustom package in github.
Here is my solution:
#Shiny UI
library(shiny)
library(shinyCustom)
# Define UI for application that draws a histogram
shinyUI(fluidPage(
# Application title
titlePanel("Hello Shiny!"),
# Sidebar with a slider input for the number of bins
sidebarLayout(
sidebarPanel(
useShinyCustom(slider_delay = "0"), #make a call to shinyCustom, where there is no delay
customSliderInput("bins", #Use customSliderInput instead of sliderInput
"Number of bins:",
min = 1,
max = 50,
value = 30)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
))
App now updates as you move the slider

Is it possible to use both absolute panel and sliderInput in same ui.R (shiny)?

This is my ui.R. This is an example provided in Shiny tutorial. I just edited it.
library(shiny)
library(markdown)
# Define UI for application that draws a histogram
shinyUI(fluidPage(
# Application title
titlePanel("Hello Shiny!"),
# Sidebar with a slider input for the number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot"),
absolutePanel(
bottom = 0, left=420, width = 800,
draggable = TRUE,
wellPanel(
em("This panel can be moved")
)
)
))
))
and my server. R
library(shiny)
# Define server logic required to draw a histogram
shinyServer(function(input, output) {
# Expression that generates a histogram. The expression is
# wrapped in a call to renderPlot to indicate that:
#
# 1) It is "reactive" and therefore should be automatically
# re-executed when inputs change
# 2) Its output type is a plot
output$distPlot <- renderPlot({
x <- faithful[, 2] # Old Faithful Geyser data
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
})**
In this case, sliderInput is not working. If i remove absolute panel, sliderInput is ok. What may be the problem? Many thanks
The absolutePanel uses the jqueryui javascript library. It has its own slider. This results in a conflict with sliderInput which uses jslider library. You can see this as follows:
library(shiny)
runApp(
list(ui = fluidPage(
titlePanel("Hello Shiny!"),
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)
),
mainPanel(
plotOutput("distPlot")
, tags$head(tags$script(src = "shared/jqueryui/1.10.3/jquery-ui.min.js"))
)
)
),
server = function(input, output) {
output$distPlot <- renderPlot({
x <- faithful[, 2] # Old Faithful Geyser data
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
}
)
)
EDIT: This has been fixed in the latest dev version of shiny. The slider component has been removed from the jqueryui inc. https://github.com/rstudio/shiny/commit/7e12a281f51e047336ba2c501fcac43af5253225

Resources