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.
Related
I have written a well functioning Shiny App, and I want to rewrite the code a little bit, to make it more elegant.
My probem: the App is loading for ages, and when it loads, it does not respond anymore. Strange thing that it is functioning well in the old version of the App.
This is what I have right now (after removing nearly everything):
ui.R:
library(shiny)
function(request){shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
),
mainPanel(
DT::dataTableOutput("table")
)
)
))}
server.R:
library(shiny)
library(DT)
library(crosstalk)
library(tidyverse)
data <- readRDS("report_summary2.rds")
shinyServer(function(input, output, session) {
computeTable <- function(){data %>% select("Year", "Month", "Date", "Transaction")}
output$table <- DT::renderDataTable({
DT::datatable(
data = computeTable(),
filter = "top",
rownames = FALSE,
options = list(
pageLength = 100
)
)
})
})
Unfortunately I cannot share report_summary.rds, as it contains sensitive information, it has like 500.000 rows and 9 columns in total, so dealing with that should not be the issue.
This was working in the old App version like a charm, but now it loads forever, and if I try to use the filters for dataTable (at the top), the App is not responding anymore.
I've been searching and looking, but could not find anything. Possibly anybody sees something?
You are right after > 400 thousand rows DT becomes slow being a single process (as well as Basic Shiny).
Here are some options:
To store and access data in RDBMS or noSQL.
Use Shiny Pro, which allows to split the load through multiple servers\applications.
Check as well architecture of your application (too much reactivity will slow it down).
And of course run a Profiler to find out where are your real bottle-necks. All other options are
somehow cosmetic: usage of global.R, split data table for
quick\small and bigger\slow parts etc.
I am trying to plot a simple histogram inside a shiny app. Outside the app (in R script), the graph gets plotted without any problems, (see here) but the same code produces an odd looking graph from inside the app (see the wrong graph here)
Could you help me figure out what's wrong? Link to dataset: https://drive.google.com/open?id=1ITK0lTWm_mkb9KonHLq4nuKDv-PBUDeR
library(ggplot2)
library(ggthemes)
library(scales)
library(shiny)
# read data
cso_corruption <- read.csv("cso_corruption.csv", header = T, sep = ",")
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
radioButtons(inputId = "x",label ="Measurement Type",choices =
c("freq_business", "freq_cso"), selected = NULL)
),
mainPanel(
plotOutput(outputId = "hist")
)
)
)
server <- function(input, output) {
output$hist <- renderPlot(ggplot(data = na.omit(cso_corruption), aes(x=input$x)) +
geom_histogram(fill="lightgreen", stat="count"))
}
shinyApp(ui = ui, server = server)
Possible reason is the data is not making it into the server.
I would put the csv into another file within the directory, so you can access it using
read.csv("./Data/projectdata.csv")
Therefore it does not get lost when you publish. Also make sure the data is checked when publishing. One last note to include the read.csv function in the server.
After many trial-and-errors, I have figured out a solution. The trick is, in the server, I used aes_string instead of aes. I haven't figured out why aes_string works here, since it is supposed to require the variables to be passed in quotes. But it works for some reason.
I've been stuck with this simple problem:
Shiny app uses several sliderInputs that are placed in different tabs in standard fluidPage. When one slider is moved, other sliders should move accordingly.
I've been working with updateSliderInput() function placed in observe()
Are there any other functions or procedures that can change values of selected sliders? I've also been experiencing that when I swich to other tab, the initial value of slider is set as in the beggining even though it should have been changed by updateSliderInput().
I'd be really thankful if someone could reference me to some function or algorithm that could help me.
Bonus points: I also need to do this for selectInput and radioButtons
I have a similar problem and followed an example from
R shiny bi-directional reactive widgets
It works for me. Please see the following:
R code
ui <- fluidPage(
titlePanel(""),
fluidRow(
column(3,
wellPanel(
uiOutput('slider1'),
uiOutput('text1')
))
))
server <- function(input, output) {
output$slider1<-renderUI({
text1.value<-input$slider1
default.slider1<-if(is.null(text1.value)) 15 else text1.value
textInput("text1", label="slider",
value = default.slider1)
})
output$text1<-renderUI({
slider1.value<-input$text1
default.text1<-if(is.null(slider1.value)) 15 else slider1.value
sliderInput("slider1", label = h5("text"),
min = -10, max = 10, value = default.text1,step = 1)
})}
Just realized this question is posted a long time ago. Still hope it helps!
I am trying to display from 1 to 5 tabPanels in a navbarPage in Shiny.
I have 5 plots my code generates, but I'd like a user to be able to select how many they want to have access to -- to be displayed one plot in each tabPanel, naturally.
I've got an external configuration file (config.txt) that via source('config.txt'), I have access to a number_of_pages variable.
For example, number_of_tabPages <- 3
How would I set this up in UI.R?
The number of tabPanels can't be hardcoded at all in the UI file, since it depends on a value that is specified by a user, not using a control.
I've searched around and found that most of the approaches to this kind of thing
involve using uiOutput and renderUI functions, such as this similar problem, but I don't want any special control in the UI to do any selecting.
This is where things get tricky, when we are building the UI depending on values that may change. My brain is trying to wrap itself around the best method for doing this type of thing -- I feel like it isn't exactly in line with how Shiny wants to communicate with itself using a UI <--> server environment.
Any advice is greatly appreciated.
My UI.R is easy to create when it isn't dynamic:
fluidRow(
column(12,
"",
navbarPage("",tabPanel("First Tab",
plotOutput("plot1")),
tabPanel("Second Tab",
plotOutput("plot2")),
tabPanel("Third Tab",
plotOutput("plot3")),
tabPanel("Fourth Tab",
plotOutput("plot4")),
tabPanel("Fifth Tab",
plotOutput("plot5"))
)
)
)
)
Thanks!
If you don't need the user to change the number of tabPanel interactively, but just load varying numbers of them when the app is started you can use the do.call function in the navBarPage:
library(dplyr)
library(shiny)
library(ggvis)
#number of tabs needed
number_of_tabPages <- 10
#make a list of all the arguments you want to pass to the navbarPage function
tabs<-list()
#first element will be the title, empty in your example
tabs[[1]]=""
#add all the tabPanels to the list
for (i in 2:(number_of_tabPages+1)){
tabs[[i]]=tabPanel(paste0("Tab",i-1),plotOutput(paste0("plot",i-1)))
}
#do.call will call the navbarPage function with the arguments in the tabs list
shinyUI(fluidRow(
column(12,
"",
do.call(navbarPage,tabs)
)
)
)
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.