shiny mainPanel width when including markdown - r

I'm trying to include a "reactive" .Rmd file within a shiny application. It would summarize the user's choices and various conditions that arise. For whatever reason, I find including shiny within .Rmd documented, but not including the ability to knit .Rmd and have the output included inside a shiny app.
I find these very hard to reproduce, so here's my attempt using the hello world example from Rstudio. Here's the app as-is in chromium:
Next, I add a uiOutput element to ui.R:
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot"),
uiOutput("mark")
)
I also add a corresponding output in server.R, taken from this sort of similar question:
output$mark <- renderUI({
HTML(markdown::markdownToHTML(knit("./test.Rmd", quiet = TRUE)))
})
The contents of test.Rmd are this:
## something to print
```{r}
head(mtcars, input$bins)
```
When I run it, I now get this (screenshot footprint is the same):
I'm assuming there's some css or something in the knit document that's goofing things up, but I don't know. I can force the widths wider, but the main panel stays sort of centered.
I've tried includeMarkdown but wasn't able to get it to update based on input. Keep in mind that's the eventual hope -- I need something that can regenerate whatever is displayed based on changed input values. I realize I could do renderText or something, but the Rmarkdown format is so convenient as this is sort of a guide. I can write some documentation, then show the user what they've selected and what happens as a result.
It seems close... I just can't figure out why the page goes wonky.
I also tried stuff like style = "width: 1200px" or style = "width: 100%" without being able to get the ratio between the sidebarPanel and mainPanel back to normal.
Ideally, the output would look just like the original, but the knit results would show up under the current footprint of the plot.

It appears that the rmd code adds the following property to the body element:
max-width: 800px;
To remove this, add the following to your ui code (for example below sliderInput)
tags$head(tags$style(HTML("
body {
width: 100% !important;
max-width: 100% !important;
}
")))

Set fragment.only=TRUE in markdownToHTML:
output$mark <- renderUI({
HTML(markdown::markdownToHTML(knit("./test.Rmd", quiet = TRUE), fragment.only=TRUE))
})

Related

Possible incompatibilty "div" tags and updateSelectInput

I am working in a Shiny Flexdashboard and I having a issue with updateSelectInput, and CSS code...
I would like alternatives to solve the issue...
I wanted to lower a table size but I am new in html, CSS and related stuff, so I found the inspiration from here and here (and here specifically mentioned as implemented in flexdashboard) and coded the similar to the following:
selectInput("Indicator","select",choices=c(),selected="NONE")
observe({
Inds<-as.factor(mtcars[,2])%>%levels
updateSelectInput(session,inputId="Indicator",choices=Inds)
})
wellPanel(
div (dataTableOutput ("OrigData"), style = "font-size: 80%"),
dataTableOutput("OrigData"),
)
output$OrigData<-DT::renderDataTable(mtcars)
It makes the lines smaller in the data table rendered as I want, but, the problem is, the selectInput "Indicator" is not updated.
What works: The selectInput "Indicator" is adequately updated if I comment/exclude the line
div (dataTableOutput ("OrigData"), style = "font-size: 80%")
So, I am unable to make they work simultaneously...
The same happens even if I put the select input on the sidebar and the data table in another tab...
There is a kind of incompatibility between the CSS's "div" code and updateSelectInput? What can I do to work with style in flexdashboard (specifically dataTableOutput font-size) without blocking the updateSelectInput?
This is more a workaround, not exactly a solution. But I think it was a good idea to include how I worked around it... It's related to the answer indicated here and uses DT (as described here)
That's not exactly my initial intent, the rownames, title row remain with the original size, but a way to approach partially the issue.
```{r,echo=FALSE}
selectInput("Indicator","select",choices=c(),selected="NONE")
observe({
Inds<-as.factor(mtcars[,2])%>%levels
updateSelectInput(session,inputId="Indicator",choices=Inds)
})
wellPanel(
#div (dataTableOutput ("OrigData"), style = "font-size: 80%"),
dataTableOutput("OrigData"),
)
output$OrigData<-DT::renderDataTable(mtcars%>%
DT::datatable() %>%
DT::formatStyle(columns = colnames(mtcars), fontSize = '50%')
)
```

R Shiny downloadHandler does not work inside flexdashboard

I am working in a R Shiny Flexdashboard, and including a download button. To distribute the items on the screen I would like to use splitLayout. But the issue is, if I use downloadButton inside the SplitLayout, the downloadHandler throws a very strange behaviour... in RStudio, it tries to save an (apparently) empty .Rmd file. In the browser, it tries to save an (apparently) empty .htm file...
The code with a minimal example is the following:
splitLayout(cellWidths = c("30%", "70%"),
wellPanel(
downloadButton("downloadData")
),
dataTableOutput("OrigData")
)
output$downloadData<-downloadHandler(
filename = "OriginalData.csv" ,
content = function(file) {
write.csv(mtcars, file=file)
},
contentType="text/csv"
)
output$OrigData<-DT::renderDataTable(mtcars)
I would like to understand what is going on...
From the link apparently downloadHandler works just if one put it just after the download button code (also just if the last one wrapped inside a wellPanel)... But the code works well if isolated just this part.
Other parts of app are still just text/Markdown, and column and tab headers because it is in the draft state...
What is happening when the downloadHandler tries to save a .Rmd and/or html file?
This worked for me:
---
title: "app"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
runtime: shiny
---
```{r}
library(flexdashboard)
library(shiny)
library(DT)
splitLayout(cellWidths = c("30%", "70%"),
wellPanel(
downloadButton("downloadData")
),
dataTableOutput("OrigData")
)
output$downloadData<-downloadHandler(
filename = "OriginalData.csv" ,
content = function(file) {
write.csv(mtcars, file=file)
},
contentType="text/csv"
)
output$OrigData <- renderDataTable(mtcars)
```
I am including an answer because I tried to answer the comments and it disappeared...I believe I figured out the answer but it would be an intriguing flexdashboard+downloadHandler behaviour...
As mentioned by #Phil, the isolated code I posted here indeed works, I tried now (sorry for not doing it before)...
I went back to my original code and isolated the corresponding code (with my data). It also works (it downloads correctly the .csv)
The remaining codes were theoretically irrelevant for the problem... including just the remaining code keep the downloadhandler working fine...
The problem appears to be the titles (column, tabs titles - .data-navmenu, .storyboard)... It makes little sense, are them just titles… But I modified the original titles (there were some similar titles) and it also solved the issue… the app stopped trying to save .Rmd or .htm, and started to enable to download .csv…
I believe the downloadHandler() does not work when there are similar column/tab titles in the flexdashboard... Does it make sense? It sounds a bit strange to be the answer for this but worked...

R: Make part of cell bold in shiny table output

I am using the R shiny app and creating a table using renderTable and tableOutput. Is it possible to make one part of a cells contents bold whilst keeping the rest of it normal text.
E.g. one entry in a particular cell could be:
5.3% ~ 1% ~ 7
I tried hardcoding ** around the appropriate figure but it just outputted the asterisk.
Thanks
You can use the <strong></strong> HTML tag in your table if you want some bold text, here's an example:
library(shiny)
data<-data.frame(a=c("<strong>a</strong>","b"),val=c(1,2))
runApp(list(
ui = basicPage(
tableOutput('mytable')
),
server = function(input, output) {
output$mytable = renderTable({
data
},sanitize.text.function=function(x){x})
}
))
You need to change the sanitize.text.function to identity in order for the tags to be interpreted.
As an alternative, you can also use Datatables to render your table. You can also use the <strong> tag, but make sure you set the escape option to false in the renderDataTable part.

Accessing uiOutput Value On App Load

To simplify this example I've only included the necessary code to describe the issue I'm having. Should it be required, I will include a fully reproducible example, but I have a hunch that this issue can be solved through theory alone by someone with more experience using Shiny. Basically, I've programmed a Shiny app in R which looks something like this:
ui.R
plotOutput(outputId = 'heatmap1', height = "800px")
uiOutput('selectStrains')
uiOutput('selectRegions')
server.R
output$selectStrains = renderUI({
checkboxGroupInput(inputId='strains',
choices=sort(colnames(mousedata)),
selected=colnames(mousedata))
})
output$selectRegions = renderUI({
checkboxGroupInput(inputId='regions',
choices=sort(rownames(mousedata)),
selected=rownames(mousedata))
})
# more code
output$heatmap1 = renderPlot({
input$recalculate
mousedatamat = as.matrix(mousedata[isolate(input$strains), isolate(input$regions)])
heatmap.2(mousedatamat)
})
problem:
My problem is that in server.R, when the app is first loaded, input$strains and input$regions are both NULL. Therefore, mousedatamat in server.R will be a 0x0 matrix, and the heatmap will be empty on the front page of the app, which makes for a pretty poor user experience. I don't know how the ui.R and server.R files interact when an app is launched, so I'm finding it difficult to debug. To make the plot show up I either need to click a recalculate button (input$recalculate), or resize the window (but only in the horizontal dimension for some reason).
To add more mystery to the mix, I have the same heatmap figure on page 2 (I'm using a navbarPage layout), which shows up on the app load when I navigate to that tab! It is the very same code, only instead it is assigned to output$heatmap2. When I navigate back to page 1, output$heatmap1 still does not display.
I've tried placing the calls to uiOutput above plotOutput in ui.R, but the main reason I don't know how to solve this I think is because I don't know much about execution flow when an app is started. Any ideas or information on this topic?

R Shiny, Remove Within-Column Filters from DataTables

[also posted in Shiny Google Group]
I am encountering some (I believe) unexpected behavior when I attempt to display a dataTable. When I display the table, my goal is to remove the majority of the sort/pagination/filter/processing options. So far setting bSort=0, bProcessing=0, bPaginate=0, bInfo=0 appears to produce desired results. However when I set bFilter=0, only the "global" filter box in the upper right had corner is removed; the within-column filter boxes remain (I expected bFilter=0 to remove all filter boxes).
Can anyone help with code to remove the within-column filter boxes (please and thank-you). [Also, I am aware of the column-specific format options, but have so-far been unable to implement them successfully to eliminate the within-column formats]. I have included minimal code below to reproduce the problem:
shinyUI(pageWithSidebar(
#my code has a header panel;
headerPanel("Table Example"),
#my code has a sidebar panel;
sidebarPanel(helpText("Stuff Here")),
#table is displayed in the main panel;
mainPanel(dataTableOutput("myTable"))
))
shinyServer(function(input, output) {
#example dataTable that produces undesired result;
output$myTable <- renderDataTable({
as.data.frame(matrix(sample(1:10,100,replace=TRUE),nrow=20,ncol=10))
}, options = list(bFilter=0, bSort=0, bProcessing=0, bPaginate=0, bInfo=0))
})
[Behavior appears both running from server and locally. Shiny 0.7.0.99. Using Google Chrome]
Thanks-in-advance!
The solution was to simply edit the css associated with the myTable output object:
I.e. change:
mainPanel(dataTableOutput("myTable"))
to
mainPanel(
dataTableOutput("myTable"),
tags$style(type="text/css", '#myTable tfoot {display:none;}')
)

Resources