Dropdown menu with R outputs using Shiny in R Markdown - r

I am totally new to Shiny, so first, apologize my inexperience.
I am writing a dynamic report using R Markdown, and I have several plots that deserve being shown dynamically.
Let's work with the mtcars dataset, and let's create 4 plots:
plot1 <- plot(mtcars$mpg)
plot2 <- plot(mtcars$cyl)
plot3 <- plot(mtcars$hp)
plot4 <- plot(mtcars$qsec)
So, how can I create a dropdown menu with these four plots being displayed accordingly?

I mean your example is the simplest shiny example I have ever seen on stackoverflow. I suggest you take a bit of time doing the tutorial, then you try building your own app and if you're stuck come back here :)
To learn shiny I recommend Rstudio's tutorial. After learning and forgetting everything again I advise you to use the wonderful cheatsheet provided by Rstudio.
The app.r looks like this:
library(shiny)
library(plotly)
### ui.r
ui <- fluidPage(selectInput('my_dropdown','Choose Column',colnames(mtcars)),
plotOutput('my_plot')
)
### server.r
server <- function(input, output) {
output$my_plot <- renderPlot(
plot(mtcars[,input$my_dropdown],ylab=input$my_dropdown,xlab='value')
)
}
shinyApp(ui,server)
Just for the sake of completeness. This kind of graph could be created using plotly as well. The advantage is that you can use this in a standalone html. The disadvantage is that dropdowns with plotly are quite code-intensive. The tutorial for plotly is here: https://plot.ly/r/dropdowns/

Related

Is it possible to use the result from a renderPlot without shiny session?

I've created a dashboard which shows a plot, based on data that is updated every 8 hours. This data is updated via a cron-jobbed Python script, so R/shiny is only for plotting/dashboarding purposes. The plot is created via a couple of reactive functions, and finally a renderPlot. Is it possible to call or import these reactive functions without a shiny context, so that I can create the plot, without running the dashboard?
The final goal is to create the plot and save/send it every time the data is updated from Python, without having duplicate R code.
I think the best way to do this would be via creating a package.
For example, if you build an app through the {golem} framework, you can use the inner "business" functions outside of the app.
So the idea would be to:
Build the function that takes input and generate the plot
Use this function inside your renderPlot.
For example :
In plots.R
my_plot <- function(dataset){
ggplot(dataset, aes(Sepal.Length, Sepal.Width)) +
geom_point()
}
Then you can reuse this in an app:
library(shiny)
ui <- function(request){
plotOutput("plot")
}
server <- function(input, output, session){
r <- reactiveValues()
observe({
r$data <- iris
})
output$plot <- renderPlot({
my_plot(r$data)
})
}
shinyApp(ui, server)
The idea here is that if you have built everything as a package (for example named "{myapp}", you'll be able to access myapp::my_plot() outside of a shiny context.
So when launching the cron, you can add something like R -e 'myapp::my_plot(dataset)'.

display of mapview object in shiny

I’m trying to have two spatial plots side-by-side in shiny, and I was suggested a powerful function, sync of mapview. sync allows to have multiple maps for comparison, a great feature, but unable to figure out integrating or calling its output in shiny. I have gone through ‘mapview for shiny’ and other related links mapview/shiny. The former suggested using renderMapview and mapviewOutput, however it did not work, i.e., no map being displayed. Please see the reproducible code. Also, I tried using #map slot of mapview object in renderLeaflet and calling it via leafletOutput - did not work. In both cases, a shiny window pops up and does not display anything. However, do see the following message in the command window: Warning in spCheckObject(x) : Columns CCN_1 in attribute table contain only NA values and are dropped. - it is related to the data base and confirms that mapview command is being executed but does not provide any leads on absence of plots. Greatly appreciate suggestions or clues on displaying mapview geneated plots in shiny.
library(shiny)
library(mapview)
ui <- fluidPage(
mapviewOutput("samplemap"),
p()
)
server <- function(input, output, session) {
output$samplemap <- renderMapview({
mapview(gadmCHE)
})
}
shinyApp(ui, server)

googleVis (gvisTable) not rendering with shinydashboard app on R

I am currently building an application with shinydashboard and googleVis. gvisTable is in my opinion the best way to show my outputs. My problem is the following:
The application (and thus, googleVis) works perfectly with the R browser on my computer, but if I want to show the application on an other computer, the application starts and computes everything asked but the tables are not rendering with gvisTable.
Since it works on my computer, I don't think the problem comes from the code, but I show you the used packages and an example of how I use gvisTable.
I use the following packages:
library(shiny)
library(shinydashboard)
library(shinyBS)
library(shinyjs)
library(texreg)
library(googleVis)
In the file server.R, when I want to output a table I use code like
output$coefftable <- renderGvis({
ConfInv <- 0.05
model <- Arima_Model()
CoeffTable <- CoefficientsFunction(ConfInv,model)
gvisTable(CoeffTable,options(digits=4))
})
where the function CoefficientsFunction returns a table using data.frame.
For the output, I use in ui.R
htmlOutput("coefftable")
I don't understand where the problem comes from, it might be from the RStudio browser as suggested in gvisTables not rendering in Shiny apps.
Do you think I might use an other browser? If yes, which one would be the best?
Try assigning your table and then return it inside server.R like this:
output$coefftable <- renderGvis({
ConfInv <- 0.05
model <- Arima_Model()
CoeffTable <- CoefficientsFunction(ConfInv,model)
table <- gvisTable(CoeffTable,options(digits=4))
return(table)
})

gvisTables not rendering in Shiny apps

The actual issue I'm trying to solve: I'm creating a dashboard which will include data tables. I would like for numbers to be formatted with commas as thousands separators, but there is (apparently) an issue with the DT package when it's used with Shiny, in that the comma-separated formatting causes DT::renderDataTable to read in numbers as character, which affects how the numbers are sorted. (DT's number formatting functionality does not work with Shiny, it appears.)
Where I'm at so far: The only solution I've been able to find is to use googleVis instead of DT to create the tables. Now I'm running into a different issue (described below), but what I really care about is having data tables with comma-separated numbers that sort like numbers.
The GoogleVis issue: When I use gvisTable outside of Shiny apps, they render perfectly fine, but they do not render at all when using renderGvis and htmlOutput in Shiny. As an example, I'll borrow Example 4 from here.
Not using Shiny, my code looks like this:
library(datasets)
library(googleVis)
myOptions <- list(page='enable', pageSize=10, width=550)
Table <- gvisTable(Population,options=myOptions)
plot(Table)
Using Shiny, it's like this:
library(datasets)
library(googleVis)
library(shiny)
shinyApp(
ui = pageWithSidebar(
headerPanel("Example 4: pageable table"),
sidebarPanel(
checkboxInput(inputId = "pageable", label = "Pageable"),
conditionalPanel("input.pageable==true",
numericInput(inputId = "pagesize",
label = "Countries per page",10))
),
mainPanel(
htmlOutput("myTable")
)
),
server = function(input,output){
myOptions <- reactive({
list(
page=ifelse(input$pageable==TRUE,'enable','disable'),
pageSize=input$pagesize,
width=550
)
})
output$myTable <- renderGvis({
gvisTable(Population,options=myOptions())
})
}
)
Any help is much appreciated!
I solved my own problem. It turns out that RStudio's native browser has difficulty displaying googleVis exhibits through Shiny. All I needed to do was open it up in Firefox... I don't think I've ever felt so much "woot" and "ugh" at the same time before.

rCharts plot won't appear in shiny app using dimple.js

I came along with this problem, that rCharts plot won't show in my shiny app. I found this example which perfectly suits my needs. Even though this chart works perfectly while just plotting in R, in shiny it is an blank page.
I am not sure what is wrong with it. Firstly, I am not sure if I am choosing right library in showOuput(), but I didn't find any better solution.
I am trying to plot it more sophisticated app, however, I reproducing my simple app code below of server:
server.R
library(rCharts)
library(reshape2)
options(RCHART_WIDTH = 1700)
meansconferences <-read.csv("https://raw.github.com/patilv/ESPNBball/master/meansconferences.csv")
shinyServer(function(input, output) {
output$test <- renderChart({
meltmeansconferences=melt(meansconferences[-c(1,10:14)], id.vars=c("Conference","Year"))
d1=dPlot(y="Year", x="value",data=meltmeansconferences, groups="variable",type="bar")
d1$yAxis(type="addCategoryAxis", orderRule="Year")
d1$xAxis(type="addPctAxis")
return(d1)
})
}
)
And ui:
ui.R
options(RCHART_LIB = 'dimple')
shinyUI(pageWithSidebar(
headerPanel("rCharts and shiny"),
sidebarPanel(),
mainPanel(
h4("Graph here"),
showOutput("test","dimple")
)
))
EDITED: I am using R version 3.0.2 and rCharts 0.4.2 shiny 0.8.0.99
If you have any ideas, just let me know.
Thank you in advance!
Posting my comment as an answer so that this question can be closed
Switch to renderChart2 or explicitly set d1$set(dom = "test") if you are using renderChart. I would recommend renderChart2, since I hope to combing the two functions in the next version.

Resources