Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sidebar id is not in input inside a RMarkdown #407

Open
victordebuen opened this issue May 25, 2024 · 3 comments
Open

sidebar id is not in input inside a RMarkdown #407

victordebuen opened this issue May 25, 2024 · 3 comments

Comments

@victordebuen
Copy link

Hello

I am trying to create a dashboard that will allow me to know which menu is selected as shown here
https://rstudio.github.io/shinydashboard/behavior.html

If I make it as an app it works perfectly but if I embed a RMardown it doesn't work and input$tabs is NULL.

test_shinydashboard_sidebar_id_inside_RMarkdown.Rmd.txt

Sorry, I didn't know how to write the example code because it's RMarkdown and it didn't let me upload a file with extension .Rmd, so I added .txt at the end.

RMarkdown makes it quite easy for me to publish applications by simply copying the file to the shiny server directory. That's why I wanted to point out this small problem, although it's clearly not very serious because you can refactor the code as a shiny app. I also wanted to notify you in case this fact is hiding another more serious latent problem.

Thank you very much for your attention.

@YongbingDing

This comment was marked as off-topic.

@gadenbuie
Copy link
Member

You can include the example by placing it inside a code block delimited by four backticks ````. You can copy the template below and then paste in your Rmd contents.

````
---
title: My rmd
---

# My Rmd

```{r}
library(tidyverse)
```
````

@victordebuen
Copy link
Author

Thanks a lot, @gadenbuie

Here you can see how under RMarkdown there is no ìnput$tags object at all, which should respond to menu events.

Captura desde 2024-05-31 10-45-35

This is the RMarkdown code with which I can't see any value of input$tabs. I used textInput``` and updateTextInputinside an observe because thetextOutput text is white and not visible on the sideBar. I have added an ````invalidateLater to the ````observe`` so that it is clear that it does not react at all to menu item selection.

---
output: html_document
runtime: shiny
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(shiny)
library(shinydashboard)
library(tidyverse)
```

```{r ui, echo=FALSE, context="server"}
dashboardPage(
  dashboardHeader(), 
  dashboardSidebar(
    sidebarMenu(
      # Setting id makes input$tabs give the tabName of currently-selected tab
      id = "tabs",
      
      menuItem("Dashboard", tabName = "dashboard"),
      menuItem("Widgets", tabName = "widgets"),
      menuItem("Charts",
               menuSubItem("Sub-item 1", tabName = "subitem1"),
               menuSubItem("Sub-item 2", tabName = "subitem2")
      )
    ),
    textInput("res", label="You have selected", value = "You've selected: 'NONE'")
  ),
  dashboardBody(
    tabItems(
      tabItem("dashboard", "Dashboard tab content"),
      tabItem("widgets", "Widgets tab content"),
      tabItem("subitem1", "Sub-item 1 tab content"),
      tabItem("subitem2", "Sub-item 2 tab content") 
    )
  )
)
```

```{r server, echo=FALSE, context="server"}
  observe({
    invalidateLater(1000)
    list_of_inputs <- reactiveValuesToList(input)
    cat("TRACE Shiny inputs:\n"); print(names(list_of_inputs))
    if(is.null(input$tabs)) {
      tag = "NULL"
    } else {
      tag = paste0("'",input$tabs,"'")  
    }
    updateTextInput(session, "res", value = paste0("At '",strftime(Sys.time(),"%H:%M:%S"),"' tag: ",tag))
  })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants